0

In my Android app there is a password editText than normally displays the dots instead of letters, under it there is a check box that is labeled Show Password. When the check box is checked for the first time the password displays but if unchecked it does not re-hide. I switched the state from hide at start to show at start and the first uncheck of the check box hides but then won't un-hide on subsequent clicks. The only code involved with this is:

public void chkShowKey_click ( View v ) {
if ( showKey.isChecked ( ) ) {
  txtPassKey.setInputType ( 144 );
} else {
  txtPassKey.setInputType ( 128 );
}

}

What is wrong? Is the isChecked() not changing before the if statement checks the value?

When I try:

YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { //NOW USE THE BOOLEAN TO CHECK IF CHECKED } } );

I get enter image description here

and enter image description here

Further testing with this code

chkShowKey.setOnCheckedChangeListener ( new CompoundButton.OnCheckedChangeListener ( ) {
                @Override
                public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked ) {
                    if ( isChecked ) {
                        txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_NORMAL );
                        FancyToast.makeText(MainActivity.this,"Show",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();    
                      } else {
                        txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );
                          FancyToast.makeText(MainActivity.this,"Hide",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
                      }
                      //FancyToast.makeText(MainActivity.this,"Checkbox changed!",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();

                  }
              }
          );

Shows that the event is firing and the isChecked is changing, everything is working EXCEPT txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );

Karæthon
  • 33
  • 8

2 Answers2

2

Try Below code according to your Requirement. It will be work just change according to your Checkbox id.

Event for Checkbox

public void onCheckboxClicked(View view) {

    boolean checked = ((CheckBox) view).isChecked();

    switch(view.getId()) {
        case R.id.chk1:
            if (checked)
            {System.out.println("if Part);}
              else
           {System.out.println("Else Part);}

            break;
        Perform your logic
    }
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Rahul Patel
  • 326
  • 2
  • 10
  • This failed to do anything. I added it to my code, set the Id, added in toast to make sure it's being called. The toast never appeared. – Karæthon Jan 25 '19 at 15:43
0

You can simply set an setOnCheckedChangeListener and Then check if Checked inside the Click Event and Deal with editText Accordingly

YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//NOW USE THE BOOLEAN TO CHECK IF CHECKED
   }
}
);

If you r trying to show adn Hide password Follow below

To show or hide dots instead of the password set the PasswordTransformationMethod:

yourEditText.setTransformationMethod(new PasswordTransformationMethod());

of course you can set this by default in your edittext element in the xml layout with

android:password

To re-show the readable password, just pass null as transformation method:

 yourEditText.setTransformationMethod(null);
skryshtafovych
  • 572
  • 4
  • 16
  • Ok, I tried to put that into my code and it's going crazy saying unexpected end and missing brackets.... Is there an import I need to make it happy? – Karæthon Jan 24 '19 at 19:20
  • Yes import the CompoundButton its part of Widget import and make sure you have set your YourCheckBox with findViewById(YOURID);....import android.widget.*; – skryshtafovych Jan 24 '19 at 19:41
  • Hmm, I have import android.widget.*; and chkShowKey = findViewById( R.id.chkShowKey); but it is still unhappy... – Karæthon Jan 24 '19 at 19:46
  • Why are you editing on an Android Phone. You probably downloaded an app that said you can compile JAVA or run Android on phone. You need to download Android Studio and deploy to your phone this seems very confusing what your trying to do please clarify your approach. – skryshtafovych Jan 24 '19 at 20:00
  • I don't have a computer so I am using AIDE. It works almost exactly like studio but with some differences. – Karæthon Jan 24 '19 at 20:03
  • I just downloaded the AIDE and was able to compile sample app with what your trying to accomplish check out the gist there are 2 files inside the XML and the JAVA hope this help https://gist.github.com/skryshtafovych/ec1135d9a2b708315a29c663bd014d2c – skryshtafovych Jan 24 '19 at 20:26
  • Ok. I went to that link and imediately saw what my problem was. I was putting it OUTSIDE onCreate as if it was a method all on it's own. I moved it in and it had no errors... But it still doesn't re-hide... – Karæthon Jan 25 '19 at 00:12
  • YES!!!!!!!!!!!! That finally works! .setEditTransformation was EXACTLY what was needed. One point though, you didn't say that an import aandroid.text.method.PasswordTransformation was needed. It wasn't working at first so i went to developer.android.com to see how it's used and i noticed it was a different import area. – Karæthon Jan 25 '19 at 22:20