Android set background color button pressed

wawanopoulos

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" />
Philipp Jahoda

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" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to set background color for an icon button?

From Dev

set background color: Android

From Dev

How to change text background color of a button in Android?

From Dev

Android : change button text and background color

From Dev

Set mywallpaper in android after pressed button

From Dev

How to set rounded buttons with background color and change color on pressed

From Dev

How to set the background color on a disabled button by a converter?

From Dev

Remove background color of button pressed and replace with image in Windows Phone 8.1

From Dev

Simple way to show to the user that he pressed a button by changing the background color?

From Dev

Android background that ripples when pressed, but solid color when selected?

From Dev

Android button background color changes button size

From Dev

Swift change button background color and image color while pressed

From Dev

Android Button background color not changing

From Dev

Android Custom Keyboard Change Background color key pressed

From Dev

Disable pressed background color of button

From Dev

Pressed button background change in Android as in Xcode

From Dev

Set Checkbox Pressed Background Color Windows Phone 8 in C#

From Dev

Change Button Background when pressed - Android

From Dev

Set button hover or onmouseover background color

From Dev

Set mywallpaper in android after pressed button

From Dev

How to set rounded buttons with background color and change color on pressed

From Dev

Android set background color button pressed

From Dev

Remove background color of button pressed and replace with image in Windows Phone 8.1

From Dev

Replicate default mouseover background color when a button is pressed

From Dev

Swift change button background color and image color while pressed

From Dev

How to set a button background and back according to selected state not pressed state

From Dev

How to set :focus background color on the button

From Dev

customly set button background color ios

From Dev

How to set Backgound color of a button programmatically using BackgroundTint and using as background @android:color/holo_orange_light

Related Related

  1. 1

    How to set background color for an icon button?

  2. 2

    set background color: Android

  3. 3

    How to change text background color of a button in Android?

  4. 4

    Android : change button text and background color

  5. 5

    Set mywallpaper in android after pressed button

  6. 6

    How to set rounded buttons with background color and change color on pressed

  7. 7

    How to set the background color on a disabled button by a converter?

  8. 8

    Remove background color of button pressed and replace with image in Windows Phone 8.1

  9. 9

    Simple way to show to the user that he pressed a button by changing the background color?

  10. 10

    Android background that ripples when pressed, but solid color when selected?

  11. 11

    Android button background color changes button size

  12. 12

    Swift change button background color and image color while pressed

  13. 13

    Android Button background color not changing

  14. 14

    Android Custom Keyboard Change Background color key pressed

  15. 15

    Disable pressed background color of button

  16. 16

    Pressed button background change in Android as in Xcode

  17. 17

    Set Checkbox Pressed Background Color Windows Phone 8 in C#

  18. 18

    Change Button Background when pressed - Android

  19. 19

    Set button hover or onmouseover background color

  20. 20

    Set mywallpaper in android after pressed button

  21. 21

    How to set rounded buttons with background color and change color on pressed

  22. 22

    Android set background color button pressed

  23. 23

    Remove background color of button pressed and replace with image in Windows Phone 8.1

  24. 24

    Replicate default mouseover background color when a button is pressed

  25. 25

    Swift change button background color and image color while pressed

  26. 26

    How to set a button background and back according to selected state not pressed state

  27. 27

    How to set :focus background color on the button

  28. 28

    customly set button background color ios

  29. 29

    How to set Backgound color of a button programmatically using BackgroundTint and using as background @android:color/holo_orange_light

HotTag

Archive