I want to add a toast say after 30 seconds when a button is clicked. Can you please help me out.
You can use a Handler with postDelayed(). You can find the documentation here
For example:
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
             // Put your Toast here
        }
}, 30 * 1000);
You have to watch out which Thread your Handler is running on. If you want to make UI modifications (like the Toast), you have to attach the Handler on your UI-Thread.
Something like that:
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, "Hello!", Toast.LENGTH_LONG).show();
            }
        }, 30000);
    }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With