Reputation: 1539
I have an android application that has a button which I want to have black text and background while it is disabled and then a green background with white writing when it is enabled.
Ive got the enabling and disabling working but if I change the colors the button stays the same regardless of its state.
I've read that I need to use a custom selector to set the colors manually and this is what I have got so far:
continuebutton.xml in res/drawable:(green is declared in /res/values/colors.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android.textColor ="@android:color/black"
android:drawable="@android:color/black" />
<item
android:state_enabled="true"
android.textColor ="@android:color/white"
android:drawable="@color/green" />
continuebutton in layout xml file:
android:id="@+id/continueButton"
android:layout_width="200dp"
android:layout_height="55dp" android:layout_gravity="center" android:paddingTop="10dp" android:textSize="18dp"
android:text="@string/continueButton" android:background="@drawable/continuebutton" />
I think I'm doing something wrong in the continuebutton.xml file but I'm not sure how to fix it.
Any help would be greatly appreciated!
Thanks
UPDATE:
I have the background changing color, the last problem is resolve is the text color which is staying black regardless of whether the button is disabled or enabled(should be white for enabled).
Do i need to make a new xml file in res/drawable for the text color?
Any ideas?
Upvotes: 5
Views: 13606
Reputation: 3540
You have to add your file in res/color
folder.
You have some "state" values availables. For enabled/disable see android:state_enabled
.
mybutton_color.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/> <!-- pressed -->
<item android:state_focused="true" android:color="#fff"/> <!-- focused -->
<item android:state_enabled="false" android:color="#000"/> <!-- disabled -->
<item android:color="#fff"/> <!-- default -->
</selector>
In your view use android:textColor
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/mybutton_color" />
As mentionned here : https://developer.android.com/guide/topics/resources/color-list-resource
Upvotes: 1
Reputation: 1277
Remove the attribute
android:src="@drawable/continuebutton"
and use
android:background="@drawable/continuebutton"
at runtime you can change the background image by
myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.myfirstbg));
myButton.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.mysecondbg));
if you want to use background Color remove both properties`
android:src="@drawable/continuebutton"
android:background="@drawable/continuebutton"
and change background color by using this
myButton.setBackgroundColor(Color.BLUE);
myButton.setBackgroundColor(Color.GREEN);
Upvotes: 1