Matt
Matt

Reputation: 15

How to reshow an alert dialog after it's dimissed

I have been beating my head against the desk for about a week. Hopefully someone can help.

Here is what I am looking to do.

It sounds straight forward but I just can't wrap my head around it. My code looks junky I know I've been cutting and pasting stuff in and out.

I orignially was calling the getpasswd method oncreate and after each incorrect password. Unfortuantley on orientation change my window leaked. So I tried this oncreatedialog stuff and I don't get anymore leaks but I can't persist my edittext textbox between rotations and I cannot reshow the dialog after the "ok" button is pressed even if the input is incorrect. Any pointers would be greatly appreciated.

Thanks.

package com.mstamp.dreamhostpal; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class GetLoginPassword extends Activity { public static String MY_PREFS = "Settings"; private static final String TAG = "MyActivity"; String value; String crypto; String text; boolean setup; String cleartxt; boolean cancel_pushed; private static final int ALERT_DIALOG1 = 1; Dialog dialog; //final EditText input = new EditText(this); boolean dismissed = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.setup_password); loadPreferences(); // showDialog(ALERT_DIALOG1); // getPasswd(); } @Override public void onPostCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); showDialog(ALERT_DIALOG1); //getPasswd(); } protected void onPause() { super.onPause(); //dialog.dismiss(); if (cancel_pushed == false) { //EXIT(); } } private void EXIT() { this.finish(); } public void loadPreferences() { int mode = Activity.MODE_PRIVATE; SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode); crypto = mySharedPreferences.getString("cryptedAPIKey", null); setup = mySharedPreferences.getBoolean("setup", false); } @Override protected Dialog onCreateDialog(int id) { Dialog dialog; switch(id) { case ALERT_DIALOG1: dialog= getPasswd(); break; default: dialog = null; } return dialog; } private Dialog getPasswd() { Dialog dialog; AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setCancelable(false); alert.setTitle("Login"); // Set an EditText view to get user input final EditText input = new EditText(this); //final EditText editTextPasswordFirst= (EditText)d.findViewById(R.id.EditTextPasswordFirst); input.setHint("Password"); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { value = input.getText().toString(); // Do something with value! if (value != null && value.trim().length() == 0) { Context context = getApplicationContext(); CharSequence text = "Please enter a password."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); getPasswd(); } else if (value.trim().length() < 5 && value.trim().length() > 0) { Context context = getApplicationContext(); CharSequence text = "The password must be 5 characters or greater."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); //getPasswd(); dismissed = true; } else { try { if (setup) { cleartxt = CryptoHelper.decrypt(value, crypto); Intent MainCommandsList = new Intent(); MainCommandsList.setClassName("com.mstamp.dreamhostpal", "com.mstamp.dreamhostpal.MainCommandsList"); MainCommandsList.putExtra("cleartxtAPIKey", cleartxt); MainCommandsList.putExtra("cleartxtpassword", value); startActivity(MainCommandsList); } if (!setup) { cleartxt = CryptoHelper.decrypt(value, crypto); Intent GetCommandsMakeDatabase = new Intent(); GetCommandsMakeDatabase.setClassName("com.mstamp.dreamhostpal", "com.mstamp.dreamhostpal.GetCommandsMakeDatabase"); GetCommandsMakeDatabase.putExtra("cleartxtAPIKey", cleartxt); GetCommandsMakeDatabase.putExtra("cleartxtpassword", value); startActivity(GetCommandsMakeDatabase); EXIT(); } } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); Context context = getApplicationContext(); CharSequence text = "That password was incorrect."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); getPasswd(); } } } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. cancel_pushed = true; EXIT(); } }); dialog = alert.create(); return dialog; //alert.show(); }

Upvotes: 0

Views: 1774

Answers (2)

J. Maes
J. Maes

Reputation: 6892

You can do it like this:

    private void launchLoginDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter your username and password");

LayoutInflater factory = LayoutInflater.from(this);
View varianceDialogView = factory.inflate(R.layout.loginDialog,null);

alert.setView(loginDialogView);
alert.setTitle(R.string.loginDialogTitle);

alert.setPositiveButton("login", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {
    if (IsLogincheckOk == false){
        launchLoginDialog();
    }
    else{
        doWhatYouWant();
    }
    }
});

alert.show();
   }

Upvotes: 2

Bryan
Bryan

Reputation: 3667

With how you are doing the password Dialog, you probably need to reload the Activity.

So, if the password is wrong, you would just create an intent, and call the activity again.

Where you check for the correct password, if it is incorrect, try this:

Intent i = new Intent(this, GetLoginPassword.class);

startActivity(i);

However, I haven't tried calling an activity using an intent from within that same activity, so you'll have to try it out, but I think it might work.

The way I implemented handling passwords is a bit complicated.

I called my own custom Dialog class from within the onStart method of my PasswordPrompt Activity, like this:

myPWRslt = new OnReadyListener();
objPwPrompt = new PasswordDialog(SecurityPrompt.this, myPWRslt);
objPwPrompt.show();

In my custom PasswordDialog Dialog class, I declared an defined my own custom interface for handling the result of the password entered. I set a global variable that is used to check if a successful password was given or not.

protected interface ReadyListener {
  abstract void ready(int iResultCode);

Then, after the password is entered, I defined the ReadyListener interface back in my PasswordPrompt Activity class. If the wrong password is pressed, the dialog is shown again. If cancel is pressed, the application exits.

private class OnReadyListener implements PasswordDialog.ReadyListener {
    @Override
    public void ready(int iResultCd) {
      try {
        switch (iResultCd) {
        case PasswordDialog.RESULT_LOGIN_SUCCESS_CODE:
          PasswordPrompt.this.setBlPasswordOK(true);
          Intent i = new Intent(PasswordPrompt.this, myMainActivity.class);
          startActivity(i);

          PasswordPrompt.this.finish();
          break;

        case PasswordDialog.RESULT_LOGIN_INCORRECT_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          objPWClass.showDialog(iResultCd);
          break;

        case PasswordDialog.RESULT_EXCEEDED_ATTEMPTS_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          objPWClass.showDialog(iResultCd);
          break;

        case PasswordDialog.RESULT_LOGIN_CANCELLED_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          // exit application
          System.gc();
          PasswordPrompt.this.finish();
          break;

        default:
          break;
        }// end switch
      }// end try
      catch (Exception error) {
        //handle the error
      }// end try/catch (Exception error)
    }// end public void result(boolean success)

  }// end OnReadyListener

Here is the XML for the layout of my PasswordDialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout01" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >


  <EditText 
        android:id="@+id/txtPwEntry" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text=""
        android:inputType="textPassword"
        android:selectAllOnFocus="true"
        android:hint="Enter your Password"
        android:nextFocusDown="@+id/btnPwSubmit" 
        android:maxLength="25" >
  </EditText>

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:stretchColumns="*"
        android:background="#000000"
        >
        <TableRow android:background="#000000" android:layout_margin="2dp">
           <LinearLayout
              android:id="@+id/myWidget248"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginBottom="5dp"
              >
           <Button 
               android:id="@+id/btnPwSubmit"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:layout_weight="0.5"
               android:textStyle="bold"
               android:textSize="14sp"
               android:text="Submit" >
           </Button>
           <Button 
               android:id="@+id/btnPwCancel"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:layout_weight="0.5" 
               android:textSize="14sp"
               android:text="Cancel" >
           </Button>
         </LinearLayout>
        </TableRow>
  </TableLayout>
</LinearLayout>

Upvotes: 0

Related Questions