• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get password through alert Dialog

AQSAANUM

Member
Jun 1, 2017
55
13
Hey Everyone,

I have programmed an alert dialog which asks for password, please tell me what to do when a user enters wrong password


Here is the code


final AlertDialog.Builder alert = new AlertDialog.Builder(this);


alert.setTitle("Admin Login");
alert.setMessage("Enter your password");

// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);

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

public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if (!value.equals("admin")){

//HERE I AM STUCK!!!

}else{}
// Do something with value!


}
});


alert.show();
 
  • Like
Reactions: Rihanna Matt
Well presumably the Activity which creates and shows the AlertDialog wants to know the result, and do something accordingly.
You could get the onClick() method to set the value of a boolean class variable like this

Code:
class MainActivity {
  ...
  boolean validPassword;
  ...

  public void onClick(DialogInterface dialog, int whichButton) {
  String value = input.getText().toString();
  if (!value.equals("admin")) {
    validPassword = true;
  }else {
    validPassword = false;
  }
});

Your MainActivity code can now examine the validPassword variable and take appropriate action.
 
  • Like
Reactions: Phalon4
Upvote 0
Actually that's not going to work is it? Because AlertDialog operates asynchronously to the parent Activity. You need to set up a listener notification on the MainActivity. See here for details:

https://developer.android.com/guide/topics/ui/dialogs.html
Yes this is not working.
What I am trying to do that, is I am making registration interface and getting all values from fields (name, email etc) and putting on the other xml file by intent.putExtra. And for accessing that xml file I want to prompt the admin to enter his password so he can be redirected to the xml file where I have putted values (email, name etc) by intent.putExtra and can accept or reject the request.

Is there any other method to do this ? plz Share
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones