Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Yes' button is not shown in confirmation box in Android app?

hi friends i set a dialog box with yes and no button but only NO is visible YES button is not visible please suggest me

This is the code i used thanks in advance

     alertDialog.setButton("YES", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int which) {

                 // Write your code here to invoke YES event
                 Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                 }
             });

             // Setting Negative "NO" Button
    alertDialog.setButton("NO bad", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
                 // Write your code here to invoke NO event
                 Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                 dialog.cancel();
                 }
             }); 
like image 862
Vji Avatar asked Feb 10 '14 13:02

Vji


2 Answers

I think you should follow this Builder pattern example given below.

AlertDialog.Builder builder = new AlertDialog.Builder(context)
        .setCancelable(true)
        .setTitle(titleResourceId)
        .setMessage(messageResourceId)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeToast(mContext, "Yes", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeToast(mContext, "No", Toast.LENGTH_SHORT).show();
            }
        });
    builder.show();

Using setPositiveButton() and setNegativeButton() allows android to place the buttons in the correct order according to the platform the app runs in.

like image 198
midhunhk Avatar answered Sep 22 '22 11:09

midhunhk


instead of

alertDialog.setButton 

use

alertDialog.setPositiveButton
alertDialog.setNegativeButton
like image 38
Rajan Avatar answered Sep 19 '22 11:09

Rajan