How to call a method with a delay in Android (Java)
Hello everyone! I'm a beginner in Android development, writing in Java. I encountered a task: after clicking a button, a Toast message should appear on the screen, but not immediately, after 3 seconds. I tried using Thread.sleep(3000) in the UI thread, but the app freezes and the Toast doesn't appear. I read that this is not allowed. \n\nHere's my code:\n\n```java\npublic void onClick(View v) {\n try {\n Thread.sleep(3000);\n } catch (InterruptedException e) {\n e.printStackTrace();\n }\n Toast.makeText(this, "Hello after 3 seconds!", Toast.LENGTH_SHORT).show();\n}\n```\n\nIt's clear that Thread.sleep blocks the main thread, and the UI doesn't update. How can I properly implement a delay so the app doesn't freeze? I've seen mentions of Handler and Timer, but I don't understand how to use them correctly.