2

This code is correct when the inputType is text but i want the same for password type number.On click of the checkbox i want the password to be visible.

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); } else { password.setInputType(129); } } }); 
2
  • try this android:inputType="textPassword" Commented Oct 29, 2012 at 10:33
  • setInputType() did not work for me when I wanted to reset the field to hidden text - but passwordEditView.setTransformationMethod(..) did - from this post: stackoverflow.com/a/19123514/2162226 Commented Apr 23, 2015 at 18:21

3 Answers 3

1

You can use:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { password.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD); } else { password.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL); } } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried like this :getEditText().setInputType( InputType.TYPE_CLASS_NUMBER | (((CheckBox) view).isChecked() ? InputType.TYPE_TEXT_VARIATION_PASSWORD: InputType.TYPE_NUMBER_VARIATION_NORMAL)); But it did not worked.
@user1744952 Change it to: getEditText().setInputType( InputType.TYPE_CLASS_NUMBER | (((CheckBox) view).isChecked() ? InputType.TYPE_NUMBER_VARIATION_PASSWORD: InputType.TYPE_NUMBER_VARIATION_NORMAL));
@user1744952 I think you are using | (OR) in your setinputType, that's why it may be taking 1st option as the input type of your edittext
1

The Password Visibility Toggle feature has been added to support library version 24.2.0 enabling you to toggle the password straight from the EditText without the need for a CheckBox.

You can make that work basically by first updating your support library version to 24.2.0 and then setting an inputType of numberPassword on the TextInputEditText. This works for input types textPassword, numberPassword and textWebPassword.

Here's how to do that:

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="numberPassword"/> </android.support.design.widget.TextInputLayout> 

You can get more information about the new feature on the developer documentation for TextInputLayout.

Comments

0

For numbers use:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { password.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD); } else { password.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED); } } }); 

1 Comment

No it did not work I tried like this: getEditText().setInputType( InputType.TYPE_CLASS_NUMBER | (((CheckBox) view).isChecked() ? InputType.TYPE_TEXT_VARIATION_PASSWORD: But it did not worked. InputType.TYPE_NUMBER_FLAG_SIGNED));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.