Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set editable true/false EditText in Android programmatically?

We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method!

If EditText is not Enabled [ by setEnabled(false)] it still Editable!

like image 610
Vaibhav Jani Avatar asked Jul 02 '11 06:07

Vaibhav Jani


People also ask

How do you set editable false for EditText in android programmatically?

In your xml code set focusable="false" , android:clickable="false" and android:cursorVisible="false" and this will make your EditText treat like non editable.

How do you make text not editable on android?

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead. Alternatively, if you want to do it in the code you could do this : EditText mEdit = (EditText) findViewById(R. id.

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

How do I turn an editable into a string?

Simply use toString() on the Editable instance to get String.


3 Answers

This may help:

if (cbProhibitEditPW.isChecked()) { // disable editing password
       editTextPassword.setFocusable(false);
       editTextPassword.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
       editTextPassword.setClickable(false); // user navigates with wheel and selects widget
       isProhibitEditPassword= true;
} else { // enable editing of password
       editTextPassword.setFocusable(true);
       editTextPassword.setFocusableInTouchMode(true);
       editTextPassword.setClickable(true);
       isProhibitEditPassword= false;
}
like image 51
JAL Avatar answered Oct 07 '22 23:10

JAL


I did it in a easier way , setEditable and setFocusable false. but you should check this.

How to replicate android:editable="false" in code?

like image 25
sat Avatar answered Oct 08 '22 00:10

sat


Fetch the KeyListener value of EditText by editText.getKeyListener() and store in the KeyListener type variable, which will contain the Editable property value:

KeyListener variable;
variable = editText.getKeyListener(); 

Set the Editable property of EditText to false as:

 edittext.setKeyListener(null);

Now set Editable property of EditText to true as:

editText.setKeyListener(variable);  

Note: In XML the default Editable property of EditText should be true.

like image 33
Sumit Sonone Avatar answered Oct 07 '22 23:10

Sumit Sonone