Cannot change EditText back to being editable - android

I want to make my EditText not Editable through java code, and then get it back to being editable, but it seems that it cannot be changed back.
noteDetailsview.setClickable(false);
noteDetailsview.setFocusable(false);
noteDetailsview.setClickable(true);
noteDetailsview.setFocusable(true);
Now it is still not editable. How can I make it editable again.

Try with this to make eddittext not editable:
noteDetailsview.setEnabled(false);
and this to make it editable again:
noteDetailsview.setEnabled(true);

use noteDetailview.setEnable(true/false) to enable/disable the edittext.

you can use these methods:
public void getFocous(EditText edit) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
edit.requestFocusFromTouch();
}
public void loseFocous(EditText edit) {
edit.clearFocus();
edit.setFocusable(false);
}

Related

Clearing edit text data

I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.
But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.
I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.
I also tried using finish() and everything works except I have to go through all the activities again to enter the data.
Try this one also.
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid=editText1.getText().toString();
String pwd=editText2.getText().toString();
editText1.setText("");
editText2.setText("");
}
});
}
Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// As soon as button is clicked, set is as empty
editText.setText("");
}
Try and see if it makes a difference.
you can use this library to clear text by a clear icon
http://droidparts.org/widgets.html#clearableedittext
Use this:
editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or else
write this in XML file
EditText
android:hint="Enter Name" />
i did this way.try
You can use:
myEditText.setText(null);

Edit Text key listener

I have an edittext and a button in my layout and in my code I'm setting keyListener of the edittext as null
editText.setKeyListener(null);
so that I cannot type into my edittext. Now on my button click I should be able to type into my ediitext. How can I do that. It's a simple problem, but I'm not able to find any solution. Any help would be much appreciated.
I'm probably late now but, this is the way I do it:
public class MyActivity extends Activity
{
private KeyListener listener;
private EditText editText;
public void onCreate(...)
{
editText = ... // Get EditText from somewhere
listener = editText.getKeyListener(); // Save the default KeyListener!!!
editText.setKeyListener(null); // Disable input
}
// When you click your button, restore the default KeyListener
public void buttonClickHandler(...)
{
editText.setKeyListener(listener);
}
}
Basically, you first save the EditText's default KeyListener before you call setKeyListener(null). Then, when you click your button, you call setKeyListener again, passing the default listener you previously saved.
You can use this :
// When you click your button, restore the default KeyListener
public void buttonClickHandler(...)
{
editText.setKeyListener(new EditText(getApplicationContext()).getKeyListener());
}
its bug in android See here Bugs.
But in xml file you can do it.Using android:editable="false"
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false" <<<<<<<
</EditText>

setHint doesnt work with setInputType

I build EditText dynamically. Among other things, I set 2 properties: hint(.setHint) and inputType(.setInputType). My problem: when I invoke setInputType, setHint has no effect: blank edittexts remain blank with no hint. Once I comment out setInputType, I see all hints. I need both input type and hint. What to do? My code:
private EditText buildTextBox(Property property)
{
EditText control = new EditText(this);
control.setInputType(getInputTypeByPropertyInputType(property.getType()));// android.text.InputType.
control.setHint(property.getDisplayName());
return control;
}
private int getInputTypeByPropertyInputType(String type)
{
if (type.equals("integer"))
{
return android.text.InputType.TYPE_CLASS_NUMBER;
}
else
{
return android.text.InputType.TYPE_CLASS_TEXT;
}
}
#Eugene
Ensure you call control.SetHint() just before you call the control.setGravity() and control.setInputType(); and it works for me verrry much!
column1 = new EditText(this);
column1.setId(i);
column1.setHint("Value");
column1.setInputType(InputType.TYPE_CLASS_NUMBER);
column1.setGravity(Gravity.RIGHT);
I agree with Eugene.
Remove the gravity(just don't use CENTER) and the hint texts will come back as normal.
Nice find!

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!
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;
}
I did it in a easier way , setEditable and setFocusable false. but you should check this.
How to replicate android:editable="false" in code?
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.
How to do it programatically :
To enable EditText use:
et.setEnabled(true);
To disable EditText use:
et.setEnabled(false);
hope this one helps you out:
edittext1.setKeyListener(null);
edittext1.setCursorVisible(false);
edittext1.setPressed(false);
edittext1.setFocusable(false);
editText.setInputType(InputType.TYPE_NULL);
Once focus of edit text is removed, it would not allow you to type even if you set it to focusable again.
Here is a way around it
if (someCondition)
editTextField.setFocusable(false);
else
editTextField.setFocusableInTouchMode(true);
Setting it true in setFocusableInTouchMode() seems to do the trick.
try this,
EditText editText=(EditText)findViewById(R.id.editText1);
editText.setKeyListener(null);
It works fine...
Try this it is working fine for me..
EditText.setInputType(0);
EditText.setFilters(new InputFilter[] {new InputFilter()
{
#Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend)
{
return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
}
}
});
editText.setFocusable(false);
editText.setClickable(false);
Since setEditable(false) is deprecated, use textView.setKeyListener(null); to make editText non-clickable.
Since the setEditable(false) is deprecated and we can't use it programmatically, we can use another way to solve it with setInputType(InputType.TYPE_NULL)
It means we change the input type of edit text. We set it to NULL so it becomes not editable.
Here's the sample that might be useful (I code this on my onCreateView method Fragment):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourfragment, container, false);
EditText sample = view.findViewById(R.id.youredittext);
sample.setInputType(InputType.TYPE_NULL);
Hope this will answer the problem
An easy and safe method:
editText.clearFocus();
editText.setFocusable(false);

Clearing a multiline EditText

I am trying to clear a multiline EditText field inside the OnEditorActionListener.onEditorAction method.
But using any of the obvious ways i.e.
((EditText) view).getEditableText().clear();
((EditText) view).getEditableText().clearSpans();
((EditText) view).setText("");
only clears the visible characters - leaving the the newlines in the field (which then have to be manually deleted).
Is there way to 'completely' clear a multiline EditText field ? (or at least - does anybody know why the above don't work ?)
Solved (in a minute after a good night's sleep) - the newline was being added after clearing the text because the onEditorAction method implementation was returning false (for other reasons).
Returning true indicates that the 'enter' has been processed/consumed and the clear() behaves as expected:
edittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view,int actionId,KeyEvent event) {
post(view.getText().toString());
((EditText) view).getEditableText().clear();
return true;
}
});
I don't have an IDE here to test, but you could give it a try:
((EditText) view).clearComposingText()
It's a inherit method from TextView
Not so elegant but maybe functional: setSingleLine = true and then false again. Maybe useful until someone can provide something better...
There is a way with setMaxLines:
yourEditText.getEditableText().clear();
yourEditText.setMaxLines(1);
Maybe I'm feeling a bit too lucky but:
((EditText) view).setText(null);
I used this when I had a clear button on my app
Button clearButton = (Button)findViewById(R.id.clear);
clearButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
number = (EditText) findViewById(R.id.text_reading);
number.setText("");
}
});

Categories

Resources