Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep an alertdialog open after button onclick is fired? [duplicate]

Tags:

android

The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.

Thanks..

like image 337
David Brown Avatar asked Oct 25 '10 15:10

David Brown


People also ask

How do you prevent a dialog from closing when a button is clicked?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.


2 Answers

You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:

    /*      * Prepare the alert with a Builder.      */     AlertDialog.Builder b = new AlertDialog.Builder(this);      b.setNegativeButton("Button", new DialogInterface.OnClickListener() {         @Override         public void onClick(DialogInterface dialog, int which) {}     });     this.alert = b.create();      /*      * Add an OnShowListener to change the OnClickListener on the      * first time the alert is shown. Calling getButton() before      * the alert is shown will return null. Then use a regular      * View.OnClickListener for the button, which will not       * dismiss the AlertDialog after it has been called.      */      this.alertReady = false;     alert.setOnShowListener(new DialogInterface.OnShowListener() {         @Override         public void onShow(DialogInterface dialog) {             if (alertReady == false) {                 Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE);                 button.setOnClickListener(new View.OnClickListener() {                     @Override                     public void onClick(View v) {                         //do something                     }                 });                 alertReady = true;             }         }     }); 

Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#

like image 129
zenperttu Avatar answered Sep 29 '22 07:09

zenperttu


Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:orientation="vertical">      <EditText          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:minWidth="180dip"          android:digits="1234567890"          android:maxLength="4"          android:password="true"/>      <LinearLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:orientation="horizontal">          <Button              android:id="@+id/Accept"              android:layout_width="fill_parent"              android:layout_height="wrap_content"              android:text="Accept"/>      </LinearLayout>  </LinearLayout>  

Then when you want it to pop up:

final Dialog dialog = new Dialog(RealizarPago.this); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("PIN number:"); dialog.setCancelable(true);  Button button = (Button) dialog.findViewById(R.id.Accept); button.setOnClickListener(new OnClickListener() { @Override     public void onClick(View v) {         if(password_wrong){            // showToast         } else{           dialog.dismiss();           // other stuff to do         }     } });   dialog.show();   
like image 31
blindstuff Avatar answered Sep 29 '22 07:09

blindstuff