Reputation: 389
I want to know if it's possible to change button's color when a particular edittext is empty or not empty. Like when edittext is empty, the button says "ADD" and when its not empty it says "CHANGE". Can someone help me? Thank you in advance.
Upvotes: 1
Views: 3653
Reputation: 47817
About Button Text
set like:
if(editText.getText().toString.length()>0){
yourbutton.setText("Change");
}else{
yourbutton.setText("Add");
}
And if you implement EditText addTextChangedListener
then
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence str, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence str, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable str) {
if(str.toString().trim().length()>0){
yourbutton.setText("Change");
}else{
yourbutton.setText("Add");
}
}
});
And you can also set Button Text Color
dynamically like:
yourbutton.setTextColor(Color.BLUE);
and For More information go to: http://developer.android.com/reference/android/content/res/ColorStateList.html
Upvotes: 4
Reputation: 6162
To change Button's
text and background color
do something like this
urEditText.addTextChangedListener(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(s.toString().trim().length()>0){
urBtn.setBackgroundColor(Color.GREY);
urBtn.setText("Change");
}else{
urBtn.setBackgroundColor(Color.WHITE);
urBtn.setText("Add");
}
}
});
Upvotes: 2
Reputation: 24848
// try this way
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edtValue"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnAddOrChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Add"/>
</LinearLayout>
**MyActivity.java**
public class MyActivity extends Activity{
private EditText edtvalue;
private Button btnAddOrChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtvalue = (EditText) findViewById(R.id.edtValue);
btnAddOrChange = (Button) findViewById(R.id.btnAddOrChange);
edtvalue.addTextChangedListener(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(s.toString().trim().length()>0){
btnAddOrChange.setText("Change");
}else{
btnAddOrChange.setText("Add");
}
}
});
}
}
Upvotes: 2