TextWatcher for more than one EditText
Suggested solution in @Sebastian Roth's answer is not one instance of TextWatcher
for some EditTexts
. It is one class and n instances of that class for n EditTexts
.
Each EditText has its own Spannable. TextWatcher
's events has this Spannable as s
parameter. I check their hashCode (unique Id of each object). myEditText1.getText() returns that Spannable. So if the myEditText1.getText().hashCode()
equals with s.hashCode()
it means that s
belongs to myEditText1
So if you want to have one instance of TextWatcher
for some EditTexts
you should use this:
private TextWatcher generalTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_onTextChanged(s, start, before, count);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_onTextChanged(s, start, before, count);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_beforeTextChanged(s, start, count, after);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_beforeTextChanged(s, start, count, after);
}
}
@Override
public void afterTextChanged(Editable s) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_afterTextChanged(s);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_afterTextChanged(s);
}
}
};
and
myEditText1.addTextChangedListener(generalTextWatcher);
myEditText2.addTextChangedListener(generalTextWatcher);
TextWatcher for multiple EditText fields
Change your code as :
First of all you need to add textwatcher to particular EditText, which you didn't. Try as below :
yourEditText.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) {
// TODO Auto-generated method stub
}
});
And then,
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (registerNameInput.getText().toString().length() > 0
&& registerEmailInput.getText().toString().length() > 0
&& registerUsernameInput.getText().toString().length() > 0
&& registerPasswordInput.getText().toString().length() > 0) {
registerAction.setEnabled(true);
} else {
registerAction.setEnabled(false);
}
}
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.
TextWatcher on two edittexts
As you have set textChangeListener on both edit text and and setting text on both call back events(onTextChangeListener) it will be infinite iteration. It will keep setting text on edittext of one side and call back will be keep calling. Hence this cycle will keep running untill app gets crashed.
Edit
To achieve what you want see below details
You need to keep track currently on what edit text you have focus(This is for setting text in edit text). You need to have 2 boolean variables. Now see below code
public class MainActivity extends AppCompatActivity {
EditText edt1, edt2;
boolean et1Focus, et2Focus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = findViewById(R.id.et1);
edt2 = findViewById(R.id.et2);
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//quando o texto é alterado chamamos o filtro.
if (et1Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor * 100);
edt2.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (et2Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor / 100);
edt1.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et1Focus = b;
}
});
edt2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et2Focus = b;
}
});
}
}
Hope that helps ..If you have any question feel free to ask
multiple edittext in textwatcher
Since you havent posted any logs i'm going to guess that you might need to check each string for null or empty before attempting to convert it to a Double or Int (basically your strings are "") :
if(str != null && !str.isEmpty()) {
/* code */
}
Example
Double sellRateInNo = 0;
String rate = item_sale_rate.getText().toString();
If (rate != null && !rate.isEmpty()) {
sellRateInNo = Double.valueOf(rate);
}
Single TextWatcher for multiple EditTexts?
You can attach the same TextWatcher watch to each EditText. Depending on what you need to do you may need to create you implementation of TextWatcher with some context.
Related Topics
Background Listview Becomes Black When Scrolling
Developing Cross Platform Mobile Application
Font-Face on Android 4.0.X Doesn't Work
Android Studio - Android Emulator Wifi Connected with No Internet
Android Calendarview for Showing Events
Android Viewpager with Bottom Dots
Programmatically Update Widget from Activity/Service/Receiver
How to Enable Logging for Apache Commons Httpclient on Android
Implementation of Onscrolllistener to Detect the End of Scrolling in a Listview
Bluetooth Low Energy Startscan on Android 6.0 Does Not Find Devices
What Is the Maximum Amount of Ram an App Can Use
Changing Position of the Dialog on Screen Android
Setuservisiblehint Called Before Oncreateview in Fragment
How to Open Email Program via Intents (But Only an Email Program)