Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make toast message when button is clicked

I'm creating a quiz. I have 3 buttons (options) in the activity and its corresponding questions. Now, my problem is I want to show the toast message, when the user chose the correct answer the toast message will appear in a few seconds, but when the user choose the wrong answer it will again display the toast message. I do not know how to do that.

I have done many research and read forums, but seems I don't understand and it don't meet my needs. Can someone please help? thanks in advance!

So far, here is the code. But it doesn't work.
Please correct me which code is wrong. Thanks deeply.

btn1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Button btn1 = (Button) findViewById(R.id.btnopt1_a);
    btn1.isClickable();
    switch(arg0.getId()){  
      case R.id.btnopt1_a:  
          if(btn1.isPressed()){  
             Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();  
           }  
           else btn1.setText("Your answer is wrong!, The correct answer is: Frog");  
             break;  
           }
      }
    });
like image 620
Mary Grace Avatar asked Jan 31 '13 05:01

Mary Grace


1 Answers

To show toast message when you click button, use the following code. If you want to do some validations then use the code in your onclicklistener of button:

Button btn1 = (Button) findViewById(R.id.btnopt1_a);
btn1.setOnClickListener(new OnClickListener() { 
    public void onClick(View v)  {
        Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();   
    }
});
like image 101
Ram kiran Pachigolla Avatar answered Sep 29 '22 16:09

Ram kiran Pachigolla