Android set background color button pressed

11,962

The problem is that you are not using a selector, but a shape.

Try this code (button_selector.xml, put it in your drawable folder):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/rose_fonce" android:state_selected="true"></item>
    <item android:drawable="@color/rose_fonce" android:state_pressed="true"></item>
    <item android:drawable="@color/rose_normal"></item>

</selector>

When setting this selector as the background of a Button, it will have the "rose_normal" color in normal state, and the "rose_fonce" color when pressed or selected.

Example:

<Button
    android:background="@drawable/button_selector" />
Share:
11,962
wawanopoulos
Author by

wawanopoulos

Updated on June 05, 2022

Comments

  • wawanopoulos
    wawanopoulos almost 2 years

    I would like to change the background color of my ImageButton on pressed event.

    Here is what i am done :

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <solid android:color="@color/rose_normal" />
        <solid
            android:state_pressed="true"
            android:color="@color/rose_fonce" />
    
        <stroke
            android:width="1sp"
            android:color="@color/rose_fonce" />
    
    </shape>
    

    My button is well in "rose_normal" color, but never in "rose_fonce" color on press.

    Where is the problem ?

    EDIT : PROBLEM SOLVED :

    Create one xml file called background_rounded_button.xml :

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

    rounded_button_rose_fonce.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <solid android:color="@color/rose_fonce" />
    
        <stroke
            android:width="1sp"
            android:color="@color/rose_fonce" />
    
    </shape>
    

    rounded_button_rose_normal.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <solid android:color="@color/rose_normal" />
    
        <stroke
            android:width="1sp"
            android:color="@color/rose_fonce" />
    
    </shape>
    

    And finally, apply background for the button :

     <ImageButton
            android:id="@+id/all_annonce_button_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/background_rounded_button.xml"
            android:padding="16dp"
            android:src="@drawable/ic_action_search" />
    
  • wawanopoulos
    wawanopoulos almost 10 years
    Thanks, it works fine but i have lost the oval shape of my button. How can i re-add this oval ?
  • Haresh Chhelana
    Haresh Chhelana almost 10 years
    @wawanopoulos,Try to set your custom two ovel image instead of color.