Question

I displayed two buttons, "GPS Setting" and "Cancel" and i want when Cancel button is pressed , it will show another dialog which has a "OK" Button . what to do in this case I am using the following code..

 if (!isGPSEnabled) {
        // no GPS provider is enabled
        // creating alertdialog
        AlertDialog.Builder builder = new AlertDialog.Builder(c);

        builder.setTitle("Settings");
        builder.setMessage("Enable GPS for the Application");

        builder.setPositiveButton("GPS Setting",

        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                dialog.dismiss();
            }

        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("How to use Application")
                                .setMessage(
                                        "You must enable the GPS in order to use this application. Press Activate and then press Power Button twice in order to send the alert message to the selected contacts")
                                .setNeutralButton(
                                        "OK",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                // do something

                                            /*  Intent inmainact = new Intent(
                                                        getApplicationContext(),
                                                        MainActivity.class);
                                                startActivity(inmainact);   */
                                                 dialog.cancel();

                                            }
                                        }).show();
                        dialog.dismiss();

                    }
                });

        builder.show();
Was it helpful?

Solution

Add the following code to Cancel button click

new AlertDialog.Builder(MainActivity.this)
        .setTitle("")
        .setMessage("")
        .setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // do something

            }
        }).show();

OTHER TIPS

 AlertDialog builder = new AlertDialog.Builder(YourActivity.this).create();
                    builder.setTitle("Save as Favourites");
                    builder.setMessage("Your message");

                    builder.setButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int which) {
                       // your task
                        builder.dismiss();           

                        }
                    });


                    builder.setButton2("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                 // your task           
                          builder.dismiss();     

                        }
                    });
                    builder.show();

Check this code:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

                            builder.setTitle("Settings");
                            builder.setMessage("Enable GPS for the Application");

                            builder.setPositiveButton("GPS Settings",

                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    //do Something
                                }

                            });

                            builder.setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                              dialog.dismiss();
                                            AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
                                            builder2.setMessage("Look at this dialog!")
                                                   .setCancelable(false)
                                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                       public void onClick(DialogInterface dialog, int id) {
                                                            //do things
                                                            Toast.makeText(getApplicationContext(), 
                                                                    "OnClickListener : " + "OK Clicked",
                                                                    Toast.LENGTH_SHORT).show();
dialog.dismiss();
                                                       }
                                                   });
                                            AlertDialog alert2 = builder2.create();
                                            alert2.show();

                                        }
                                    });

                            AlertDialog alert = builder.create();
                            alert.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top