I have an activity in which I get from the user credit card's serial number.
It contains four editTexts - each one for 4 digits.
I've used an array for the editTexts -
EditText[] editSerial = new EditText[4];
and I've restricted the input's length in each editText with
android:maxLength="4"
I want the focus to move to the next editText once the user have entered 4 digits in the current one.
I've seen this answer - How to automatically move to the next edit text in android:
et1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count)
{
// TODO Auto-generated method stub
if(et1.getText().toString().length()==size) //size as per your requirement
{
et2.requestFocus();
}
}
Is there any BETTER solution than repeat this code 3 times?
Kind of. You need a TextWatcher on each one but you can extract it as a proper class so that you can pass in parameters indicating the View to focus on next.
Then it'll be like
et1.addTextChangedListener(new FocusSwitchingTextWatcher(et2));
et2.addTextChangedListener(new FocusSwitchingTextWatcher(et3));
et3.addTextChangedListener(new FocusSwitchingTextWatcher(et4));
The class:
private static class FocusSwitchingTextWatcher implements TextWatcher {
private final View nextViewToFocus;
TextWatcher(View nextViewToFocus) {
this.nextViewToFocus = nextViewToFocus;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length > size) {
nextViewToFocus.requestFocus();
}
}
... // the other textwatcher methods
}
Related
I have EditText which is connected with TextWatcher I'm monitoring when user presses # letter. That will make a Listview appear with names of commentators on particular post. When user chooses one of the users from ListView, name is append to EditText and ListView is hidden.
But the problem is when user continues typing ListView will appear again because afterTextChanged(Editable s) monitors the whole inputted text which already contains letter #.
Is there a way to monitor only what user is actually typing not the whole inputed text? Or somehow escape last inputed word in TextWatcher? Or any other suggestions how to solve this.
I was researching but didn't find anything useful.
Thanks in advance
There can be many ways to achieve this. This is one of them. You can check weather your list is already filled or not.
final boolean isListSet = false;
public static final String textToFind = "#";
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (!isListSet && s.toString().contains(textToFind)) {
// set your list here
isListSet = true;
}
if (!s.toString().contains(textToFind)) {
// remove your list
isListSet = false;
}
}
});
My app has seven editTexts and one button below them. I want the button to change its background when the user filled all editText with numeric values (int or double, except zero).
I think I should use OnTouchChangeListener but I am not sure how to do it, any advice?
Add addTextChangedListener to your EditText.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Add your logic here
}
#Override
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(textWatcher);
// Remember to remove when whatever actions required have completed.
editText.removeTextChangedListener(textWatcher);
Assuming all your EditText views require the same logic, create a single TextWatcher object and bind the listener to each as above. You can also implement this in your class:
class MyClass implements TextWatcher
And add the override methods into the class and write the code in the onTextChanged() method.
Put a line inside your
android:digits="123456789"
And in java file check for the length of edittext.if the length of all editext is greater than 1 then change the color of the button.
If worked ...check my answer
I have this application in which I create multiple EditTexts dynamically. The amount depends on user input. I am trying to allow the focus to change to the next edittext after two characters have been entered. I have this so far:
for(EditText editText : editTextList ) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().length() == 2) {
//code to change focus to next edit text goes here
}
}
});
}
The first problem is that this could potentially create many instances of TextWatcher if the user enters a large number (average in this context would probably be 100-600 EditText fields).
The second problem is how would I go about changing focus in the afterTextChanged method because I would want to change the focus to the next EditText away from the one that is currently being represented inside the loop.
Should I not be concerned about the potential performance issues of multiple TextWatcher objects? Should I scrap this whole implementation and focus elsewhere? Any suggestions will be greatly appreciated.
As you may have a lot of EditTexts at runtime i would suggest you to use maxlength attribute of EditText in this way you will restrict user to enter only 2 characters.
like this
EditText editText = new EditText(this);
int maxLength = 2;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
Now add next button in your keyboard like this
yourEditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
It is the default behavior of next button to change focus to the immediate next view which takes input being in the same parent layout.
I have an application where user inputs text into EditText field. After user clicks OK (in keyboard input mode), a correct value is in the EditText (lets say "Smile").
if (answers.get(counter).getText().equals(opponentAnswers.get(counter)))
But this if statement fails, because the same EditText has the values that were suggested by T9 option, when user was inputing his answer (for example values of EditText would be "Smile Smiling Smiled"), while it should only have a value "Smile".
Any ideas how to solve this issue?
That's really weird. These are kind of guesses, but this is what I'd try next if I were you:
A. Instead of doing an equals against getText(), try doing a toString on getText(), so:
if (answers.get(counter).getText().toString().equals(opponentAnswers.get(counter)))
B. If that doesn't work then you could try adding a TextWatcher using addTextChangedListener on the EditText, and getting the value from that. Calling toString() on the editable returned in afterTextChanged might give you the value you want.
private class SearchTextWatcher implements TextWatcher {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Get the text the user sees
String textShownToUser = s.toString();
}
}
Hope this helps! Best of luck!
I have three editboxes which has retrict to enter only one numeric digit on each box.when i enter the value of first box the focus should be moved from 1st box to 2nd.After entering value to 2nd box.,its focus should automatically moved to 2nd to 3rd like that i need to do.Could anybody help me regarding this?
You can use requestFocus() API to shift the focus from the code,
Keep listening for the text, using textWatcher, once specified limit is reached, call EditTextreference.requestFocus() to shift the focus.
This code moves focus from one EditText to another when EditText length greater than 1.
EditText et1,et2;
et1 = (EditText)findViewById(R.id.id1);
et2 = (EditText)findViewById(R.id.id2);
et1.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
Integer textlength = et1.getText().length();
if(textlength>=1){ //If text length greater than 1 move to next EditText
et2.requestFocus();
}
}
});