0

I have Created a dialogue using android.support.v7.app.alertdialog class. here is my code

   final AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this,R.style.AppCompatAlertDialogStyle);
    myAlertDialog.setTitle("Enter Text");
    final EditText input = new EditText(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    myAlertDialog.setView(input);
    myAlertDialog.setCancelable(false);
    myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            if (input.getText().toString().length() != 0) {
                myAlertDialog.setCancelable(true);
                startActivity(new Intent(MainActivity.this,SelectCatDialogue.class).putExtra("text",input.getText().toString()));
            } else {
                myAlertDialog.setCancelable(false);
                Toast.makeText(getApplicationContext(),"Please Enter Text",Toast.LENGTH_SHORT).show();
            }
        }
    });

    myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            myAlertDialog.setCancelable(true);
        }
    });
    myAlertDialog.show();

now this dialog auto cancelled when i click on either positive or negative button but i need it should only cancelled on negative button not on positive button

  • make `setCancelable` false on `setPositiveButton` and true on `setNegativeButton` – Iamat8 Feb 04 '16 at 06:49
  • i have edit my code according to your suggestion but still not work @Mohit – Nauman Ali Shah Feb 04 '16 at 06:53
  • Possible duplicate of [How to prevent a dialog from closing when a button is clicked](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Mike M. Feb 04 '16 at 07:10
  • @MikeM. dear i am talking about AlertDialog of android.support.v7.app.alertdialog class dialoog not simple app.alertdialog – Nauman Ali Shah Feb 04 '16 at 07:15
  • It's the same method for the appcompat Dialog. – Mike M. Feb 04 '16 at 07:24
  • @MikeM. android.support.v7.app.alertdialog class object don't have dismiss and cancel methods . i tried alot here is the official documentaion http://developer.android.com/reference/android/app/AlertDialog.Builder.html – Nauman Ali Shah Feb 04 '16 at 07:34
  • "android.support.v7.app.alertdialog class object don't have dismiss and cancel methods ." - Yes it does. It's a descendent of the [`Dialog`](http://developer.android.com/reference/android/app/Dialog.html) class, which has both methods. Also, you've linked the old `AlertDialog.Builder` class, which, as you pointed out, you're not using. Look [here](http://developer.android.com/reference/android/support/v7/app/AlertDialog.html), under "Inherited Methods". – Mike M. Feb 04 '16 at 07:43
  • @MikeM. i have imported import android.support.v7.app.AlertDialog; but i don't know where i have linked with old AlertDialog.Builder and how to get out from this problem – Nauman Ali Shah Feb 04 '16 at 07:55
  • I meant you linked to the old class's documentation in your last comment. If you have the v7 class imported, then you're good to go. Just follow that example in the duplicate. – Mike M. Feb 04 '16 at 08:02
  • oh yes actually the link was http://developer.android.com/reference/android/support/v7/app/AlertDialog.html – Nauman Ali Shah Feb 04 '16 at 09:11

1 Answers1

0

Check this. it's run well in my case.May be your problem will solve using it.

 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
    final EditText input = new EditText(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    alertDialogBuilder.setView(input);
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            if (input.getText().toString().length() != 0) {
                alertDialogBuilder.setCancelable(true);
              startActivity(new Intent(MainActivity.this,SelectCatDialogue.class).putExtra("text",input.getText().toString()));
            } else {
                alertDialogBuilder.setCancelable(false);
                Toast.makeText(getApplicationContext(),"Please Enter Text",Toast.LENGTH_SHORT).show();
            }

        }
    });

    alertDialogBuilder.setNegativeButton("Cancle",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50