TextWatcher monitor only current input - android

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;
}
}
});

Related

How to save input from TextEdit field into a string?

I'm making a simple app, where there is one TextInput field and a button next to it.
The TextInput field loads #string/duration_time from strings.xml.
The button next to it will display a simple message ("hello" + the string duration_time). However, it will always display the original information from string duration_time.
How do I set up a TextChangedListener to update the string/duration_time to reflect the input from the user? How do I access the information from TextInput and use it? Because as in the example below, under "public void afterTextChanged" I am trying to save the information, but I don't know where it's stored.
Kind regards
button2.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) {
String duration_input= WHAT_Do_I_Put_Here;
}
});
Suppose you are using editText and button as Android Wigets.
Now if you want to get the the text inside editText on click of button.It can be done like this
String textEdit = "";
button.setOnClickListener(->view{
textEdit = editText.getText().toString();
});
Now your textEdit will contain the text which you entered in your editText.

Move focus to next EditText in an array

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
}

How to change the color of a particular string in edit text on the fly??-Android

In my app, I want the users to give their answers in the form of text through edit text. So for the correct answer I want the letters to turn green (or red for incorrect) on the fly while typing.
For example, if the answer is DOG, I want the the text to turn green if the user types DOG dynamically. Even if the the first letter he types is D then I want the text color to be green. Only when the user's input text is not correct do I want it to be red. The text color should change on the fly while typing.
Create EditText and call addTextChangedListener for it supplying custom TextWatcher where you mostly need to override its onTextChanged.
In this method change your text color according to your logic.
Snapshot :
mEditBox = (EditText) findViewById(R.id.my_edit_box_id);
mEditBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String currentText = mEditBox.getText().toString();
// highligt correct answer in green
if ("DOG".startsWith(currentText)) { // user starts typing "DOG"
mEditBox.setTextColor(Color.GREEN);
} else {
mEditBox.setTextColor(Color.RED); // incorrect input
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Text watcher in edit text is making the edittext slow

In my tablet app I have used fragments and one fragment has multiple Edittexts, and I have a linear layout which will add a sublayout as many times the user wishes to add, in that fragment
This sublayout has two edittext, both this edittext is having
addtextchangelistener(Textwatcher) and
onfocuschangelistner
every time the text is changed 3 conditions are checked in both the edittext
every time the focus is changed 2 conditions are checked in both the edittext
After doing all this condition check, the problem I'm facing is, the edittext typing is too slow, its like i type an email and the whole email gets completely typed after 5 secs or more,
This is the code for 1 edit text in the sublayout:
receiverName.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View paramView, boolean hasFocus) {
receivernameFocus = hasFocus;
if(hasFocus)
{
if(receiverName.getText().toString().length()>0)
ReceiverName_btn_cancel.setVisibility(View.VISIBLE);
else
ReceiverName_btn_cancel.setVisibility(View.INVISIBLE);
}
else
ReceiverName_btn_cancel.setVisibility(View.INVISIBLE);
}
});
receiverName.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence paramCharSequence, int paramInt1,int paramInt2, int paramInt3) {
if(receivernameFocus)
{
if(receiverName.getText().toString().length()>0)
{
receiverNamePresent = true;
ReceiverName_btn_cancel.setVisibility(View.VISIBLE);
}
else
{
receiverNamePresent = false;
ReceiverName_btn_cancel.setVisibility(View.INVISIBLE);
}
}
else
ReceiverName_btn_cancel.setVisibility(View.INVISIBLE);
if(receiverEmailPresent && receiverNamePresent)
addReceiver.setBackgroundResource(R.drawable.plus_receiver);
else
addReceiver.setBackgroundResource(R.drawable.plus_deselect_receiver);
}
#Override
public void beforeTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
}
#Override
public void afterTextChanged(Editable paramEditable) {
}
});
same conditions are present for the other edittext, and everytime the user inflate another view, same set of edittext will be created for the new view too.
I can't remove the conditions, all of them are necessary, and you can see its just some button visibility or setting background resource
How to optimize this code, or how to speed up the edittext typing speed for android tablet?
EDIT: If I'm typing 10 letters persecond its showing only 1 letter per second in the edittext(so all the 10 letters will be visible in the edittext after 10 seconds), which I believe is happening because of multiple condition checking within onTextChanged method, the delay in showing the text is too much for user experience.
How to make the edittext show the text as fast as I'm typing it
Thanks
Here public void onTextChanged(CharSequence paramCharSequence, int paramInt1,int paramInt2, int paramInt3) it's generally in this format:
public void onTextChanged(CharSequence s, int start, int before, int count)
So here you will need to do some operation using paramInt3 . If you would like to show suggest text will come after you enter 3 letter then perform an operation here in this manner:
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (count%3 == 1)
{
adapter.clear();
GetPlaces task = new GetPlaces();
task.execute(dep_place.getText().toString());
}
}
here I haved updated text from server side in background. You just need to modify this portion from where your text will come use this code here.
Thanks.

Android - EditText saves T9 suggested values

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!

Categories

Resources