tags:

views:

40

answers:

2

Hi,

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..

+2  A: 

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();
blindstuff
Thanks, so dialogs, unless you specify a custom layout will automatically close in an onclick?
Dave
I do belive so, i might be mistaken, but I found it easier to do a custom dialog instead of trying to get another one to work and I had total control over what it looks like and how it behaves.Plz mark the answer as accepted if it works for you.
blindstuff
+1  A: 

You can just continue using the dialog you already have, just put an if clause in the onClick() saying

if(pin_check_method){  //pin_check_method should be a boolean returned method
     //close the Dialog, then continue
     }
   else{
     //dont put the dialog.dismiss() in here, put instead
    Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show();
}

Now, to use this code, simply invoke text.setText(""); and put in the text you want here common error is that when you type in:

TextView text = (TextView) findViewById(R.id.dialog);

you miss that it needs to actually be

dialog.findViewById

and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout_root" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                >

    <TextView android:id="@+id/text"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              android:layout_centerHorizontal="true"
              android:layout_width="wrap_content"/>



    <Button android:text="Continue" 
            android:id="@+id/Button01" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" android:layout_below="@+id/text">
             </Button>

</RelativeLayout>
Samuel
This is what I was doing however the dialog automatically closes even without the dismiss() call.
Dave
Okay, after looking into it, it looks like custom dialog is going to be your best bet.. I'll edit my answer to one that you'll find useful. I'm actually using it in my application. dont forget to mark as accepted if you use my answer :)
Samuel