Common TextWatcher class
You can create a BaseFragment
that will be extended by your fragments.
Therefore, you can manage your TextWatcher
inside this BaseFragment
and consequently the fragments which have this heritage, will receive your expected logic.
As in the following example:
BaseFragment.class
public abstract class BaseFragment extends Fragment implements TextWatcher {
EditText editText;
Button button;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate your edit text
...
//inflate your button
...
editText.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//text watcher listener
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//text watcher listener
}
@Override
public void afterTextChanged(Editable s) {
//text watcher listener
}
}
YourFragment.class
public class YourFragment extends BaseFragment {
...
}
How to add Text Watcher on multiple Edit text in android?
There is two ways you can do this.
First
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) {
}
@Override
public void afterTextChanged(Editable s) {
if (m_InputMobile.getText().hashCode() == s.hashCode()) {
checkFieldsForEmpty();
}
else if (m_InputPassword.getText().hashCode() == s.hashCode()) {
checkFieldsForEmpty();
}
}
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);
m_InputMobile.addTextChangedListener(getmWatcher);
m_InputPassword = (EditText) findViewById(R.id.input_password);
m_InputPassword.addTextChangedListener(getmWatcher);
or make a custome TextWatcher
class
Second
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_InputMobile = (EditText) findViewById(R.id.input_mobile);
m_InputMobile.addTextChangedListener(new CustomTextWatcher(m_InputMobile));
m_InputPassword = (EditText) findViewById(R.id.input_password);
m_InputPassword.addTextChangedListener(new CustomTextWatcher(m_InputPassword));
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
@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) {
checkFieldsForEmpty();
}
}
For more details visit this Question.
Happy Coding.
How to call TextWatcher from Class
Your editText
is not 'aware' that it should call your class whenever something changes in it.
You should do it this way - your class should implement TextWatcher
:
public class FormTextCosmetics implements TextWatcher {
//Your code goes here
}
And then you use it like this -
etName.addTextChangedListener(new FormTextCosmetics());
Now the editText
will call your class whenever something happens in it's text.
Accessing TextWatcher in another class - nullpointer exception
You need to pass your activity reference to another class to like below:
MainActivity code:
public class UnitConverter extends AppCompatActivity {
public EditText input;
.
......
input = (EditText) findViewById(R.id.etInput);
.........
case Sample :
new AnotherClass(UnitConverter.this);
}
AnotherClass code:
public class AnotherClass {
public AnotherClass(UniteConverter ac) {
ac.input.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
}
}
How to use TextWatcher in android?
I think this piece of code will help you.....
EditText etSearchBar = (EditText)findViewById(Your_id);
etSearchBar.addTextChangedListener(textwatcher);
private TextWatcher textwatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
//Do what ever you want here after you enter each text
}
};
Related Topics
Save Arraylist to Sharedpreferences
Make an Http Request With Android
How to Implement Onbackpressed() in Fragments
Get Filename and Path from Uri from Mediastore
Horizontal Listview in Android
Start_Sticky and Start_Not_Sticky
Custom Listview Adapter With Filter Android
How to Read Contacts on Android 2.0
Call an Activity Method from a Fragment
How to Dynamically Add Elements to a Listview on Android
Failed to Resolve: Com.Android.Support:Cardview-V7:26.0.0 Android
This Activity Already Has an Action Bar Supplied by the Window Decor
"Default Activity Not Found" on Android Studio Upgrade