Reputation: 39
I have two EditText input, when text changes after a calculation i have to display result on a textView in same activity. addTextChangedListener is not working for me
Activity.java code:
width.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") ) {
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
perimeterdetails.setText(s);
}
}
});
XML code:
<EditText
android:id="@+id/width"
android:layout_width="0dp"
android:background="@drawable/edittextbackground"
android:layout_weight="1"
android:inputType="numberDecimal"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/perimeterdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
Upvotes: 1
Views: 2413
Reputation: 78
Try this :)
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
width.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.length() <= 0) {
perimeterdetails.setText(s);
}
}
});
Upvotes: 0
Reputation: 1052
do not findViewById inside on Textchanged. Do it before adding the textchangelistener. in the onTextChange use
if(!TextUtils.isEmpty(s)){
perimeterdetails.setText(s);
}
Upvotes: 0
Reputation: 1433
Try it
Change the condition :
to
Upvotes: 0
Reputation: 8853
change your code as below
Define this in onCreate
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
and compare your string as below in onTextChanged
if(!TextUtils.isEmpty(s.toString())) {
perimeterdetails.setText(s);
}
Upvotes: 1
Reputation: 93
Try this
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
width.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") ) {
perimeterdetails.setText(s);
}
}
});
Upvotes: 0
Reputation: 3353
Cause you are define your text view in onTextChanged
cut this line TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
befor addTextChangedListener
Upvotes: 0