Android Edittext Onchange Listener

android edittext onchange listener

First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.

EDIT:

To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:

public class YourClass extends Activity {

private EditText yourEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);

yourEditText.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {

// you can call or do what you want with your EditText here

// yourEditText...
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}

Note that the above sample might have some errors but I just wanted to show you an example.

android on Text Change Listener

You can add a check to only clear when the text in the field is not empty (i.e when the length is different than 0).

field1.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
field2.setText("");
}
});

field2.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
field1.setText("");
}
});

Documentation for TextWatcher here.

Also please respect naming conventions.

EditText onChangeListener function Android

I think what you are looking for is the TextWatcher (though the onFocusChanged might work, but this is another approach). Here is sample code:

TextWatcher watcher= new TextWatcher() {
public void afterTextChanged(Editable s) {
if (TextBox1.getText().toString().equals("")) {
TextBox1.setBackgroundColor(getResources().getColor(android.R.color.white));
TextBox1.setTextColor(getResources().getColor(android.R.color.black));
}

}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//Do something or nothing.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Do something or nothing
}
};

TextBox1.addTextChangedListener(watcher);

Also you will want to do String comparisons using .equals().

How to properly use edit text change listener in android?

It stays visible because searchusersedittext is always non-null in that call (if it were null, the earlier call to searchusersedittext.addTextChangedListener would fail with a NullPointerException), so once you type anything at all it only goes into the first case every time (keeping it visible).

Also searchusersedittext.equals("") isn't checking for an empty entry in the EditText, since searchusersedittext is an EditText object, not the entered string. The EditText object continues to exist, even if there is no text entered in it.

Try something like this:

@Override
public void afterTextChanged(Editable s) {
String txt = searchusersedittext.getText().toString();
// or String txt = s.toString();
if( !txt.isEmpty() ) {
recyclerViews.setVisibility(View.VISIBLE);
filter(txt);
}
else {
recyclerViews.setVisibility(View.INVISIBLE);
}
}

How do you change an EditText on Android based on specific values inputted?

You should not use addTextChangedListener() for this purpose.
You should wait for the user to hit the enter or done key on their keyboard. This can be achieved by the below code:

settings_distance_edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event == null || !event.isShiftPressed()) {
// the user is done typing and has submitted the input.

// your code
int settings_distance_edit_value = Integer.parseInt(settings_distance_edit.getText().toString()+"");
if (settings_distance_edit_value >= DISTANCE_MIN && settings_distance_edit_value <= DISTANCE_MAX) {
Log.v("Tag", settings_distance_edit_value+"");

} else {
settings_distance_edit.setText(DISTANCE_DEFAULT+"");
}

return true;
}
}
return false; // the user has not submitted the input yet.
}
});


Related Topics



Leave a reply



Submit