1. Are you ready for the Galaxy S20? Here is everything we know so far!

Get password through alert Dialog

Discussion in 'Android Development' started by AQSAANUM, Jul 13, 2017.


    AQSAANUM Member
    Thread Starter

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

    // Do something with value!



    Rihanna Matt likes this.

    1. Download the Forums for Android™ app!


  2. Deleted User

    Deleted User Guest

    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 (Text):
    2. class MainActivity {
    3.   ...
    4.   boolean validPassword;
    5.   ...
    7.   public void onClick(DialogInterface dialog, int whichButton) {
    8.   String value = input.getText().toString();
    9.   if (!value.equals("admin")) {
    10.     validPassword = true;
    11.   }else {
    12.     validPassword = false;
    13.   }
    14. });
    Your MainActivity code can now examine the validPassword variable and take appropriate action.
    Phalon4 likes this.
  3. Deleted User

    Deleted User Guest


    AQSAANUM Member
    Thread Starter

    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

    AQSAANUM Member
    Thread Starter

    Well Now I have solved this problem by another approach (By starting new login activity with Intent).
    Deleted User likes this.

Share This Page