-4

i want delay a toast message for 20 seconds so this is my code

 b1=(Button)findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"Welcome to my first project in android",Toast.LENGTH_LONG).show();
    }
}); 

layout

    <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/button"
    android:layout_below="@+id/button"
    android:layout_marginTop="67dp"
    android:text="About" />

so how can i do it ?

Sulaiman
  • 35
  • 1
  • 1
  • 8
  • 5
    Possible duplicate of [Android: Adding a delay to the display of toast?](http://stackoverflow.com/questions/25461791/android-adding-a-delay-to-the-display-of-toast) – Manav Dubey Apr 27 '17 at 00:27

2 Answers2

1

To do that, you can use a Handler and the postDelayed() method:

final Handler handler = new Handler();
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     handler.postDelayed(new Runnable() {
        @Override
        public void run() {
  Toast.makeText(MainActivity.this,"Welcome to my first project in android",Toast.LENGTH_LONG).show();

}, 20000); //in milliseconds

}); 
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
0

It can be done by using Handler method.

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            //your code here
        }
    },5000); // will trigger your code after 5 seconds
ZeroOne
  • 8,996
  • 4
  • 27
  • 45