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.
Related
I need a small help in checking the text in an edittext, so in start of activity i get data from API and set in edittext and i want to check if user made and changes to that edittext, I tried using addTextChangedListener function but this made my Boolean variable true when i set data to editttext from API response.
So is there any other way to check if user did any changes to edittext. and also i have a form in my activity with many edittext, I can compare API response with edittext but it will be very lengthy. If there is no other option ill have to go with that.
Thanks.
I tried finding this question on stackoverflow but didnt find the solution.
Declare a boolean variable in your Activity: Boolean fromApi = false;.
Then before you set the text to the EditText: fromApi = true;
In your textwatcher add at the beginning:
if (fromApi) {
fromApi = false;
return;
}
I want to get an in-line autocomplete with an EditText, not a result list but the best suggestion directly in EditText.
Something like this : In-line auto-complete (near the bottom of the page).
Is it possible in Android ?
Thank you.
I have no eclipse now so I will try to give you some hints.
To create a custom autocomplete I would do something like this.
First
In the view layout add the EditText and an OutputText (this with visibility=hidden)
Second
In the activity create a TextWatcher and implement the method afterTextChanged.
Inside this method call a service with the input text and then update the content of the outputText.
Something like:
afterTextChanged(Editable s){
// you know your input is an EditText
final EditText input= (EditText) s;
// TODO make this call async
String suggestedText= someService.getSuggestion(input.getString());
outputText.setText(suggestedText);
outputText.setVisibility(View.VISIBLE);
// to avoid infinite loops
if(suggestedText!=null && !"".equals(suggestedText) && !suggestedText.equals(input.getString())
{
// add a onclick control to update the input
outputText.setOnClickListener(new View.OnClickListener(){
editText.setText(suggestedText);
});
}
}
Third
Implement the suggestion service.
Android has an AutoCompleteTextView which should do the job you want.
According to the official Android docs
"AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold."
For an example code snippet refer to AutoCompleteTextView
For autocomplete you must another type of EditText called AutocompleteTextView or MultiAutocompleteTextView. Here you can find simple example for that option.
P.S. if you want to create your own type of List Filtration, your Adapter class must implement Filterable interface
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.
I have a screen with textviews now i want to make this editable on click of that
i tried one solution using edittext making it as transparent background but initially it will show cursor and the click is not recognizing properly,if i set focusbaleintouchmode to false in xml it is not getting focus.but some how the click is not working properly as expected.first is this correct approach?
expected result is textview should be there once user clicks on it it should be editable once user clicks outside it it should be not editable. any sample code will helps me a lot.sorry for my english
Thanks in advance
finally i got one solution using below code
in xml edit text i gave foucasbletouchmode to false which makes click works properly after that with in onclick
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
to lose focus
et.setFocusable(false);
et.setClickable(true);
et.clearFocus();
You can use the below code :
private makeEditable(boolean isEditable,EditText et){
if(isEditable){
et.setBackgroundDrawable("Give the textbox background here");//You can store it in some variable and use it over here while making non editable.
et.setFocusable(true);
et.setEnabled(true);
et.setClickable(true);
et.setFocusableInTouchMode(true);
et.setKeyListener("Set edit text key listener here"); //You can store it in some variable and use it over here while making non editable.
}else{
et.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
et.setFocusable(false);
et.setClickable(false);
et.setFocusableInTouchMode(false);
et.setEnabled(false);
et.setKeyListener(null);
}
}
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.