Prevent dialog dismiss when positive button clicked

Put your problem here if it does not fit any of the other categories.

Prevent dialog dismiss when positive button clicked

Postby papaiatis » Tue Mar 15, 2011 4:10 pm

Hi all,

I've extended the base Builder class to create a custom Dialog to get some user input. I'd like to validate the user's name whether the length of it is between 3 or 13 characters. If the input is either valid or not pressing the positive button will dismiss the dialog.
How can I prevent dismissing the dialog when user input is invalid?

Here's the custom dialog class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class EnterNameDialog extends Builder {
  2.  
  3.         public final EditText   textPlayerName;
  4.  
  5.         public EnterNameDialog(final Context context, final int currentScore, final int currentLevel) {
  6.                 super(context);
  7.  
  8.                 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  9.                 final View layout = inflater.inflate(R.layout.player_name_input_dialog, (ViewGroup) ((Activity) context).findViewById(R.id.layout_root));
  10.                 ((TextView) layout.findViewById(R.id.label_enter_your_name)).setText(R.string.EnterNameDialog_TextEnterYourName);
  11.                 textPlayerName = (EditText) layout.findViewById(R.id.text_player_name);
  12.  
  13.                 this.setView(layout);
  14.                 this.setTitle(R.string.EnterNameDialog_Title);
  15.  
  16.                 // SAVE BUTTON
  17.                 this.setPositiveButton(R.string.EnterNameDialog_ButtonSave, new DialogInterface.OnClickListener() {
  18.                         public void onClick(final DialogInterface dialog, int which) {
  19.                                 if (textPlayerName.getText().length() < 3 || textPlayerName.getText().length() > 13){
  20.                                         Toast.makeText(context, context.getString(R.string.EnterNameDialog_WrongPlayerName), Toast.LENGTH_LONG).show();
  21.                                 }
  22.                                 else {
  23.                                         // some operation ...
  24.                                 }
  25.                         }
  26.                 });
  27.  
  28.                 // CANCEL BUTTON
  29.                 this.setNegativeButton(R.string.EnterNameDialog_ButtonCancel, new DialogInterface.OnClickListener() {
  30.                         public void onClick(DialogInterface dialog, int which) {
  31.                                 dialog.dismiss();
  32.                                 ((GameActivity) context).finish();
  33.                         }
  34.                 });
  35.         }
  36. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
papaiatis
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 15, 2011 4:03 pm

Top

Re: Prevent dialog dismiss when positive button clicked

Postby ruelblapid » Tue Mar 15, 2011 11:35 pm

Hi Papaiatis,

When using the getText() function always use toString() to get the data typed by end-user otherwise this will return Editable Interface.

See code below
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (textPlayerName.getText().length() < 3 || textPlayerName.getText().length() > 13){ //Replace this line of code
  2.  
  3. if (textPlayerName.getText().toString().length() < 3 || textPlayerName.getText().toString().length() < 13){ //Use this one
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Best,
Last edited by ruelblapid on Wed Mar 16, 2011 4:53 am, edited 1 time in total.
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Prevent dialog dismiss when positive button clicked

Postby vhalexxs » Wed Mar 16, 2011 2:29 am

Hi papaiatis,

check this url http://stackoverflow.com/questions/4016 ... k-is-fired. This will give you example on how to make an AlertDialog not being dismiss after you clicked on the positve button.

Best,
Alex Escobar
Senior Android Engineer
-----------------------
Airpush.com: The Push Notification Ad Network
User avatar
vhalexxs
Developer
Developer
 
Posts: 28
Joined: Mon Mar 14, 2011 3:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests