some EditTexts with only one TextWatcher - android

I'm trying to user TextWatcher interface in order to detect which EditText was changed.
I have an activity with 10 EditTexts, and it looks weird to use 10 TextWatchers for each one of them.
There is any way to use only one TextWatcher and to use switch statement on the Editable in the functions afterTextChanged?

What I would do is to create a class that extends EditText and create a TextWatcher in that class. You can then implement those EditTexts in your XML or create them programmatically in Java with the TextWatcher listening for each EditText.
Don't know if this will work for you but you can give it a try.

I have never tried this before, but it should work if you check if the EditText is in focus. There are a few ways to go about this and the most straightforward one is to check the focus of the EditText inside your TextWatcher methods. You'll need to do something like this:
if(mEdit1.hasFocus()) {
...
} else if(mEdit2.hasFocus()) {
...
} else if(mEdit3.hasFocus()) {
...
}
A different approach would be to use an OnGlobalFocusChangeListener on your root view and set a variable indicating with EditText currently has focus. It would still require a lot of if statements to check for which EditText has the focus, but may be a more reusable solution.

Related

Update text watcher based on other events

In one of my screen i am using text watcher to check value against a range and for invalid range color of text is being changed.
Now there is one more requirement.
Along with EditText i need to use CheckBox to set completely different range.
The problem i am facing is that changing the checkbox value requires to call afterTextChanged in which i have put all validations for both set of ranges.
So basically my requirement is to update textWatcher anyhow so that afterTextChanged get called after i change value of checkbox.
I get a strange feeling that i am forgetting something very simple here,if so please let me know.
Thanks in advance.
Perhaps move your code from the callback in the textWatcher and put it into another function ie
private void checkRange() {
// do checks here
String color = text.getEditableText().toString();
}
and call this method from your textWatcher and from the listener on the checkbox.

How to check the edittext Listener is remove or not

I am using the the two editext and add the seperate textWatcher classes object
when some one give the focus then second one textListener is remove.
Then
My Question is how to check the textListener is add or remove
thnax in advance
AFAIK, There is no method to get the current assigne TextWatcher instance of the EditText object.
Rather you just can do like,
mEdiText.addTextChangedListener(null);
before you assign any TextWatcher and then assign the Watcher object mEdiText.addTextChangedListener(new MyTextWatcher()); to the EditText in any event or any condition.
Assigning null will remove the previous TextWatcher.
And to know is it really worked or not, just run your app and test it once.

EditText: how to enable/disable input?

I have a 7x6 grid of EditText views. I want all of them disabled when the application starts, ie they should behave like normal TextViews and not to be editable. Then the user taps one cell in the grid, it changes its background and performs something visual. If the user clicks on the cell one more time it should allow editing. I'm struggling with OnClick() and OnFocusChange() listeners, but I can't accomplish such a basic interaction.
Playing with setEnabled() and setFocusable() doesn't help. I wonder why even a simple task like this has been made so difficult on Android
I finally found a solution. It's a matter of calling
setFocusableInTouchMode(boolean)
setFocusable(boolean)
when the EditText is first created, so it can intercept the clicks. Then one can set those flags back again to make the EditText editable, request the focus, and manually show/hide the soft keyboard with InputMethodManager methods
Try using this setFocusableOnTouch() instead of setFocusable() method.
Firstly write these line in your xml in EditText:
android:enabled="false"
And than use the code in java as shown below:
Boolean check = true;
yourEditText.setEnabled(check);
check=!check;
Setting input type to null is not enough, since it only suppress soft keyboard and if device has hardware keyboard, there will be input. So in order to suppress any editing you should do following:
editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(new InputFilter[]
{
new InputFilter()
{
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend)
{
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
That will guarantee that EditText content won't be changed
Since you are using gridview to achieve your concern you can do the following.
setOnItemClicklistener on gridview
Extend Edittext to make your own edittext view
The extedned class will contain boolean property named editable using this property in onItemclicklisterner of gridviewyou can call setEditable or setFocusabel or both for a editetext.
If you share your code i can elaborate more on this issue.
According the Android guide line please use LongKeyPress for the Question you have" If the user clicks on the cell one more time it should allow editing"
You can do the follwoing:
If you want to make edittext not editable then use following method
edittext.setInputtype(Null);
If you want to make edittext editable then use the same method and set the proper inputype
visit the following link for more info

Android: How to enable my button back if EditText is not empty?

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.

Copy String from an android from EditText into String variable on Android

I have a class that creates a view to gather data via a function getView() that provides a view with an EditText.
This class has also has variable answer.
When the user chances the EditText I want to store the content of the EditText in answer.
If I would use an onKeyListener I fear that the answer will probably get stored before the last letter is entered.
Is there a good way to handle this in the getView() function via some other listener?
You should addTextChangedListener to your EditText and implement in your class TextWatcher
Then you will just take the text from the methods and store in your answer

Categories

Resources