0

I have a specific need to texcolorlist put the drawable with the background button. My textColor:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@android:color/black" />
    <item android:state_checked="true" android:color="@android:color/black" />
    <item android:color="#8e000000"/>
</selector>

My Button State:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="#005b7f"
                android:startColor="#051725"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <gradient 
                android:angle="90" 
                android:endColor="#e6e6e6" 
                android:startColor="#b3b3b3"/>
            <corners 
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="5dp"/>
        </shape>
    </item>
</selector>

I know that in this way I can put the button color: android:textColor="@drawable/button_text_color"

I wonder if the button text color can be put directly into My Button State. This is something, but it does not work:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle"
            android:textColor="#000000">
            <gradient
                android:angle="90"
                android:endColor="#e6e6e6"
                android:startColor="#b3b3b3"/>
            <corners
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="5dp"/>
        </shape>
    </item>
</selector>
Nicklas Jensen
  • 1,424
  • 12
  • 19
Ibrahim Sušić
  • 438
  • 3
  • 20

3 Answers3

0

You need to define selector also to the text color:

android:textColor="@drawable/yourselector"

Here example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:state_pressed="false" android:color="@color/blue" />
   <item android:state_focused="true" android:state_pressed="true" android:color="@color/red" />
   <item android:state_focused="false" android:state_pressed="true" android:color="@color/green" />

</selector>

Edit: I didn't saw I refereed to that possibility. I don't think what you want is possible, because what you define in your selector refer to the android attribute you assign it. Since the attribute is background there is no meaning to text color there.

yshahak
  • 4,996
  • 1
  • 31
  • 37
0

I wonder if the button text color can be put directly into My Button State.

No, it cannot. Drawable XML resources can not set properties on the elements they are applied to. What you might be interested in, is using a Style.

values/styles.xml

<style name="MyButtonStyle">
    <item name="android:textColor">@drawable/button_text_color</item>
    <item name="android:background">@drawable/button_background</item>
</style>

MyLayout.xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/MyButtonStyle"/>
Nicklas Jensen
  • 1,424
  • 12
  • 19
  • Not a bad idea. But I have two modes in the app. The first mode is normal, the second mode is the admin section. In the first mode, I need the text changes when you click on the button a second mode only want the text to be one color. The two modes use the same button just different drawable send. – Ibrahim Sušić Jul 21 '15 at 08:51
  • It would be nice if you edited your question to reflect what you're actually asking, and include the relevant XML and / or Java code for this specific `Button`. It sounds like the button is basically disabled when in "normal" mode, in which case you can simply call `setEnabled` on it. – Nicklas Jensen Jul 21 '15 at 08:57
0

This question was answered before:

Android customized button; changing text color

Hope it will help.

Community
  • 1
  • 1
Asaf Savich
  • 623
  • 1
  • 9
  • 29
  • lease reed full my qustion I write " I know that in this way I can put the button color: `android:textColor="@drawable/button_text_color – Ibrahim Sušić Jul 21 '15 at 08:52