How to make a toast message when a button is pressed

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

I have researched and read a lot of forums, but it seems that I do not understand and do not satisfy my needs. Can someone help? thanks in advance!

Bye, here is the code. But that does not work. Please correct me which code is wrong. Many thanks.

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;  
           }
      }
    });
+5
source share
3 answers

, . , onclicklistener :

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();   
    }
});
+4

, implements Activity OnClickListener. .

public class Deals extends Activity implements OnClickListener {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.yourxml);
        //declare ur buttons here
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu: {
                // Do your stuff here
                // call method
                if (isright) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Your answer wrong!", Toast.LENGTH_SHORT ).show();
                }
            }
        }
    }
}
+2

If you have many conditions in which you have to show toasts. Then it would be better to make a way that shows this.

private void showToast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

And call the above method in onClick()or wherever you want to show.

 btn1.setOnClickListener(new OnClickListener() {
   public void onClick(View v)  {
       ..................
       ..........................

       case R.id.btnopt1_a:  
           if (btn1.isPressed()) {
               showToast("Your answer is correct!");
           } else {
               showToast("Your answer is wrong!, The correct answer is: Frog");
           }

           break;
       });
0
source

All Articles