Does the Yes button not appear in the confirmation window in the Android application?

Hi friends, I set up a dialog box with a Yes and No button, but only NO is displayed YES The button is not displayed, please suggest me

This is the code that I used in advance 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();
                 }
             }); 
+4
source share
3 answers

I think you should follow the example of the Builder template 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 the android to place the buttons in the correct order in accordance with the platform in which the application is running.

+3
source

instead

alertDialog.setButton 

using

alertDialog.setPositiveButton
alertDialog.setNegativeButton
+2
source

setButton()

setPositiveButton() "" setNegativeButton() ""

+1
source

Source: https://habr.com/ru/post/1526171/


All Articles