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("");
}
});
Related
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);
}
My situation is: I have a EditText, and under it I have a button, called "select all". It's purpose is to let the user select all the text by pressing it.
When the button is clicked, I invoke selectAll() on the EditText, but instead of selecting all text, in some cases (generally, when the cursor is already positioned within the text somewhere), the cursor is moved to position 0 (start of text) and text remains unselected. Second click on the button will then select all. But of course it should happen on first click.
From this issue: Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only it seems that this is a bug in android 4.0 and above. (Found no mention in Google issue tracker).
Does anyone know about a way to overcome this problem? It seems that the probelm is also in other selection methods and not only selectAll().
(p.s. This question is sort of duplicate of the issue I mentioned above. But I opened another question, because the author of that issue was satisfied and selected a partial answer (of setting android:selectAllOnFocus="true"), while in my case and scenario it does not help me and does not solve the problem.
Thanks.
Problem caused by IME. If showed cursor drag pointer then selection must be zero width.
You need cancel drag pointer. It can be doned by change text. For example replace:
Editable text = edit.getText();
if (text.length() > 0) {
text.replace(0, 1, text.subSequence(0, 1), 0, 1);
edit.selectAll();
}
We replace first symbol of text with same symbol. It cause cancel drag pointer and allow make selection without bug.
I was having a similar issue. I solved it (using Xamarin in C#, but the equivalent Java will work) with the following code:
private void InitFocus()
{
Java.Lang.Runnable rable = new Java.Lang.Runnable(()=>
{
EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
maleCount.RequestFocus();
maleCount.SelectAll();
});
new Handler().Post(rable);
}
I have 2 controls: One is a dropdown that lets you select a chicken house and then a set of buttons for each day of the week. Whenever you change the day of week or the chicken house, I want to set focus in the txtMaleCount EditText and then I want the value in that box selected (since it's a number and they're presumably going to replace it).
Clearly, the non-intuitive part was the need to Post it. Doing it directly (on the UI thread) didn't seem to have any effect.
Try this:
yourEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});
Or This:
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});
Hope this helps .. :)
When a user enters information in an EditText, and moves to the next EditText, the information is highlighted as shown below:
The code for this:
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
//v.setBackgroundColor(Color.LTGRAY); //also works like this
((EditText) v).setBackgroundColor(Color.LTGRAY);
((EditText) v).setTextColor(Color.BLACK);
}
}
});
Which is called in the onCreate method like this:
edittext = (EditText) findViewById(R.id.editText1);
However, It would be much better if the background color only applied to the text itself, rather than the view, like this (from the gmail app):
Does anybody have any suggestions on how to apply the background color to the text only (not the whole EditText view) as above?
Thanks.
You can achieve what you want by using a BackgroundColorSpan. You can find more information here:
http://developer.android.com/reference/android/text/style/BackgroundColorSpan.html
To use spans you need to build a SpannableString which you can do using a SpannableStringBuilder:
http://developer.android.com/reference/android/text/SpannableStringBuilder.html
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);
I have a simple app with 2 edittext boxes. When input is typed into the 1st box one set of calculations are performed. If input is put into the other instead, a different set of calc's occurs.
So, if a number is typed into the first box etBox1, and the user leaves the box, the data from the first box is used to calculate a reesult and put it into the second box. If a number is typed into the second box etBox2, the data is used to calculate a value for the first box.
I tried:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1)
final EditText etBox2 = (EditText) findViewById(R.id.etBox2)
etBox1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#override
public void onFocusChange(View v, boolean lostfocus)
{
if (lostFocus == true)
{ //do my calculations....}
This fires when the focus given to etBox1 instead of waiting for the box to loose focus. This crashes the app because the user hasn't had the chance to input a number into the box. Any ideas why this behaves as a "hasFocus" instead of a "lostFocus"? There is no documentation available on lostFocus at Android's site.
In your question, you have this.
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
You are using the same ID when mapping the text boxes. I guess it should look like this:
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox2 = (EditText) findViewById(R.id.etBox2);
Not to mention that you should also get a compilation error with your code, as you define etBox1 two times...
(Unless this is only a typo in your question, and your code actually looks different...)
According to http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
onFocusChange is defined as public abstract void onFocusChange (View v, boolean hasFocus), so your boolean called lostFocus is named backwards and so confusing you, I'd recommend to change it to something like hasFocus.
now you should see that for if statement is the wrong way round, you should be checking if == false