Search code examples
javaandroidandroid-edittextandroid-appwidgetandroid-inputtype

EditText input type text password not changing


I want to change the input type of an edit text to visible when a switch is checked.

Switch a =(Switch) findViewById(R.id.switch1);
    a.setText("visible");
    a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }else{      
                EditText pass = (EditText) findViewById(R.id.pass);
                pass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        }
    });

It works when I turn on the switch and makes the password visible but when I turn it off it remains visible and doesn't change.


Solution

  • pass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
    

    Try this in your else statement for showing the password type variation. The reason is because it needs to specify that the input class is of the type text.