Search code examples
androidbuttonbackgroundselectionselectionchanged

Android change button background


I want to reverse the colors (text & background) of my button when it is clicked.

I have the following XML files. When I test it, I can see the text color changes but the background color remains the same. I could not see what I'm missing.

layout.xml

...
<Button
    android:id="@+id/start_btn"
    android:layout_gravity="center_horizontal|center_vertical"
    android:layout_margin="0dp"
    android:text="start"
    android:background="@drawable/button_background"
    android:textColor="@drawable/button_text_color"
    android:fontFamily="@string/font_family"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    />
...

button_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:color="@color/miops_dark_red" />
    <item android:color="@android:color/white" />
</selector>

button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_focused="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@android:color/white" />
            <stroke
                android:width="1dp"
                android:color="@color/miops_dark_red" />
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@color/miops_dark_red" />
            <stroke
                android:width="1dp"
                android:color="@android:color/white" />
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
        </shape>
    </item>
</selector>

Solution

  • finally I managed to solve the problem; I have modified background.xml as followed;

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/button_background_pressed"
            android:state_focused="true"
            />
        <item
            android:drawable="@drawable/button_background_pressed"
            android:state_pressed="true"
            />
        <item
            android:drawable="@drawable/button_background_released"
            />
    </selector>
    

    and added folowing two;

    button_background_pressed.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white" />
    <stroke
        android:width="1dp"
        android:color="@color/miops_dark_red" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
    </shape>
    

    button_background_released.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/miops_dark_red" />
        <stroke
            android:width="1dp"
            android:color="@android:color/white" />
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />
    </shape>