I want to share one of my problem which I faced while I was working on one of my android application. Problem was related to edittext.That time , I was using many edittext in my application.
Application Functionality:
Whenever value is placed or changed in any edittext at run time, then it should be reflected to it’s corresponding editexts (diagonalwise, rowise, columnwise). This corresponding edit texts represent total sum of edittexts in a row, column and diagonal. I suffered this problem and tried hard to find it’s solution.
Finally, I found solution of this problem, and the solution is addTextChangedListener.
Here, I am giving e.g. of some EditText because rest of them have same issue:
first_EditText_Box.addTextChangedListener(new MyTextWatcher(
firstRow_TotalUserNo_EditText,
firstColumn_TotalUserNo_EditText,
rightDiagonal_TotalUserNo_EditText).setUserVaule(1, 1, 0, 1));
second_EditText_Box.addTextChangedListener(new MyTextWatcher(
firstRow_TotalUserNo_EditText,
secondColumn_TotalUserNo_EditText).setUserVaule(1, 2, 0, 0));
third_EditText_Box.addTextChangedListener(new MyTextWatcher(
firstRow_TotalUserNo_EditText,
thirdColumn_TotalUserNo_EditText,
leftDiagonal_TotalUserNo_EditText).setUserVaule(1, 3, 1, 0));
//other EditTexts are registered with addTextChangedListener are the same as mentioned //above
private class MyTextWatcher implements TextWatcher {
private EditText editText_Row, editText_Column, editText_Diagonal;
private int row_No, column_No, leftDiagonal_No, rightDiagonal_No;
public MyTextWatcher(EditText et_row, EditText et_column) {
editText_Row = et_row;
editText_Column = et_column;
}
public MyTextWatcher(EditText et_row, EditText et_column,
EditText et_diagonal) {
editText_Row = et_row;
editText_Column = et_column;
editText_Diagonal = et_diagonal;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (row_No == 1 && column_No == 1 && rightDiagonal_No == 1) {
changeUserText_ToInt();
editText_Row.setText(String.valueOf(firstBox+secondBox
+ thirdBox));
editText_Column.setText(String.valueOf(firstBox +forthBox
+ seventhBox));
editText_Diagonal.setText(String.valueOf(firstBox + fifthBox
+ ninthBox));
}
else if (row_No == 1 && column_No == 2) {
changeUserText_ToInt();
editText_Row.setText(String.valueOf(firstBox + secondBox
+ thirdBox));
editText_Column.setText(String.valueOf(secondBox + fifthBox
+ eighthBox));
} else if (row_No == 1 && column_No == 3 && leftDiagonal_No == 1) {
changeUserText_ToInt();
editText_Row.setText(String.valueOf(firstBox + secondBox
+ thirdBox));
editText_Column.setText(String.valueOf(thirdBox + sixthBox
+ ninthBox));
editText_Diagonal.setText(String.valueOf(thirdBox + fifthBox
+ seventhBox));
}
//further EditTexts condition and code implementation are same as described above
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
private TextWatcher setUserVaule(int row_no, int column_no,
int leftdiagonal_no, int rightdiagonal_no) {
row_No = row_no;
column_No = column_no;
leftDiagonal_No = leftdiagonal_no;
rightDiagonal_No = rightdiagonal_no;
return this;
}
private void changeUserText_ToInt() {
if (first_EditText_Box.getText().length() != 0)
firstBox = Integer
.parseInt(first_EditText_Box.getText().toString());
else
firstBox = first_EditText_Box.getText().length();
}
}
The onChange event is helpful when you’ve to deal with the following things:
- Let the user know (in realtime) how many characters he typed.
- Let the user know (in realtime) how many remaining characters
he is allowed to type.
- Make realtime processing of the content (like sending it
online and fetch some partial results of the partial typed
edittext)
You’ve to implement your own instance of TextWatcher and let the edittext know that you want to be notified at each change by calling the method EditText.addTextChangedListener..
-By Jitendra Sharma
Android Developer
Comments are closed.