Reputation: 306
How can I sum all the total of my multiple EditText
without using a Button
but only on TextWatcher's onTextChanged()
method. assuming this is my layout:
EditText1 = 5
EditText2 = 5
EditText3 = 5
EditText4 = 5
Total = 20
and so on
and getting the total of it?. the total should be CHANGING when the EditText
's value is changing.
I have read this answer. but I can't execute it well on my program.
This is my Layout of it
Upvotes: 1
Views: 3821
Reputation: 69689
You need to use a TextWatcher()
instead of using 5 TextWatcher()
you can manage it using only a single TextWatcher()
Try this
Using below answer if all your 4 four edittext
is not empty then it will calculate the sum of editext
value
public class MainActivity extends AppCompatActivity {
EditText edtOne, edtTwo, edtThree, edtFour;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtOne = findViewById(R.id.edtOne);
edtTwo = findViewById(R.id.edtTwo);
edtThree = findViewById(R.id.edtThree);
edtFour = findViewById(R.id.edtFour);
tvResult = findViewById(R.id.tvResult);
edtOne.addTextChangedListener(textWatcher);
edtTwo.addTextChangedListener(textWatcher);
edtThree.addTextChangedListener(textWatcher);
edtFour.addTextChangedListener(textWatcher);
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!TextUtils.isEmpty(edtOne.getText().toString().trim())
|| !TextUtils.isEmpty(edtTwo.getText().toString().trim())
|| !TextUtils.isEmpty(edtThree.getText().toString().trim())
|| !TextUtils.isEmpty(edtFour.getText().toString().trim())
) {
int answer = Integer.parseInt(edtOne.getText().toString().trim()) +
Integer.parseInt(edtTwo.getText().toString().trim()) +
Integer.parseInt(edtThree.getText().toString().trim()) +
Integer.parseInt(edtFour.getText().toString().trim());
Log.e("RESULT", String.valueOf(answer));
tvResult.setText(String.valueOf(answer));
}else {
tvResult.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
}
if you want to calculate value of all editext
evenif your editext is empty than try below TextWatcher()
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!TextUtils.isEmpty(edtOne.getText().toString().trim())
|| !TextUtils.isEmpty(edtTwo.getText().toString().trim())
|| !TextUtils.isEmpty(edtThree.getText().toString().trim())
|| !TextUtils.isEmpty(edtFour.getText().toString().trim())
) {
int firtValue = TextUtils.isEmpty(edtOne.getText().toString().trim()) ? 0 : Integer.parseInt(edtOne.getText().toString().trim());
int secondValue = TextUtils.isEmpty(edtTwo.getText().toString().trim()) ? 0 : Integer.parseInt(edtTwo.getText().toString().trim());
int thirdValue = TextUtils.isEmpty(edtThree.getText().toString().trim()) ? 0 : Integer.parseInt(edtThree.getText().toString().trim());
int forthValue = TextUtils.isEmpty(edtFour.getText().toString().trim()) ? 0 : Integer.parseInt(edtFour.getText().toString().trim());
int answer = firtValue + secondValue + thirdValue + forthValue;
Log.e("RESULT", String.valueOf(answer));
tvResult.setText(String.valueOf(answer));
}else {
tvResult.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
Upvotes: 3
Reputation: 3147
I'm going to tell you how to do it, but I won't post the solution as a total code.
First of all, define integers that is the amount of your edit text count. If there is 4 edit texts, define 4 integers with like int first = 0, second = 0, third = 0, fourth = 0;
Then, add separate text watchers to your edit texts, and on your afterTextChanged
methods, get the integer value from the string by using Integer.parseInt(Editable s.getText.toString())
and make sure the input is convertable, otherwise you will get a NumberFormatException
. Then, assign this value to the related edit text, for example on your first edit text use first = Integer.parse(s.getText().toString())
.
Create a function that uses these variables to display on a textview. For instance:
private void showResult() {
textView.setText(String.valueOf(first + second + third + fourth));
}
Then, at every afterTextChanged methods call this function to display the total amount.
Upvotes: 1