Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause the other instructions until the dialog message is closed

Hi I have a question on AlertDialog / Toast . When a user clicks on a button , just a message showing "You are right " should be displayed for 1 seconds. When I implement this using toast/alertdialog it executes this but starts the next instruction in the onclick method before the dailog is turned off. How do I fix this?

like image 322
Nikhil Avatar asked Dec 04 '25 01:12

Nikhil


1 Answers

Here is how I did it using Handler object ---

public class ToastActivity extends Activity{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    Handler mHandler = new Handler();
    for(int i=10;i>=1;i--)
    {
        Toast.makeText(this, ""+i, Toast.LENGTH_SHORT).show();
    }
    mHandler.postDelayed(new Runnable(){public void run(){
    LinearLayout t=(LinearLayout)findViewById(R.id.l);   //This task is delayed by 21 sec.
            t.setBackgroundColor(Color.CYAN);
        }
    }, 21000);           
}

}

Statements after toast are executed after toast has disappeared.

like image 174
Vikrant Avatar answered Dec 06 '25 16:12

Vikrant