0

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();
bhanu kaushik
  • 391
  • 1
  • 7
  • 30

3 Answers3

2

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();
Asha Soman
  • 1,846
  • 1
  • 18
  • 28
  • you are such a life saviour dear . thank you very much . One more thing. When clicked "ok" i want the application to return me to the mainactivity . For this i am using intent on the onClick method of the "OK"button. But its failing . AM i doing it wrong What else i have to do to return to my main activity ? – bhanu kaushik Jan 27 '14 at 12:24
  • bhanu kaushik, you show these dialogs from which activity?What's happening while clicking OK button in your case? – Asha Soman Jan 28 '14 at 04:52
  • here, i've again edited the post and when i click "ok" it again shows me the previous dialog i.e dialog with "GPS Setting" and "cancel" buttons – bhanu kaushik Jan 28 '14 at 05:09
  • Try this,add MainActivity.this.finish(); while launching this new activity and inside Ok button click dialog.cancel(); – Asha Soman Jan 28 '14 at 05:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46191/discussion-between-asha-soman-and-bhanu-kaushik) – Asha Soman Jan 28 '14 at 05:26
0
 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();
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0

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();
Amritesh
  • 96
  • 7
  • its working absolutely fine ..But i want to ask when clicked "ok" i want the application to return me to the mainactivity . For this i am using intent on the onClick method of the "OK"button. But its failing . AM i doing it wrong What else i have to do to return to my main activity ? – bhanu kaushik Jan 27 '14 at 12:28
  • add dialog.dismiss(); in "onclick" method of OK button, where Toast is getting displayed. – Amritesh Jan 27 '14 at 13:20