Reputation: 359
I am using Fragment
class in which I want to change the color of TextView
When EditText
is empty after text is changed. I am able to change the color of TextView
on text change but when the text is clear the TextView
background color doesnot change to previous one. What are the possible ways to change the background color of TextView
When EditText
is empty after text is changed . My code looks like this :
public class Fragment_AboutUs extends android.app.Fragment {
TextView about_btn_call_customer;
TextView about_btn_Submit;
EditText about_feedback;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_fragment_about_us, container, false);
//reference section
about_btn_call_customer= (TextView) rootView.findViewById(R.id.about_btn_call_customer);
about_btn_Submit=(TextView)rootView.findViewById(R.id.about_btn_Submit);
about_feedback=(EditText)rootView.findViewById(R.id.about_feedback);
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) {
String textinEdit= about_feedback.getText().toString().trim();
if (textinEdit != null) {
about_btn_Submit.setBackgroundColor(getResources().getColor(R.color.light_green));
}else if (textinEdit ==""){
about_feedback.setBackgroundColor(getResources().getColor(R.color.grayColor));
}
}
};
about_feedback.addTextChangedListener(textWatcher);
return rootView;
}
}
Thank you in advance ...any kind of help would be appreciated...
Upvotes: 1
Views: 2089
Reputation: 157467
check count
onTextChanged
and choose the color:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int color = count == 0 ? R.color.light_green : R.color.grayColor;
about_btn_Submit.setBackgroundColor(getResources().getColor(color));
}
and you could do the same thing for the other TextView
Upvotes: 1
Reputation: 391
Try This,
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 0) {
// for empty text color
about_btn_Submit.setBackgroundColor(getResources().getColor(R.color.grayColor));
} else {
// for non empty field color
about_btn_Submit.setBackgroundColor("change your color");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
};
Upvotes: 0