0

this is an easy question,

in my xml file i have :

<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bf"
android:background="@drawable/button_purple" 
android:layout_weight="1"
android:textColor="#ffffff"
android:onClick="action"            
/>

And in my activity i have that :

public void action (View v)
{
    s = "m";
    changeCouleur("blue");
    v.setPressed(true);
}

When i pressed the button it's working but the button don't stay pressed.

I don't use an image this is what i use for the color :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Thanks in advance if you noticed something wrong. Please anyone have any idea ?

user1527152
  • 946
  • 1
  • 13
  • 37

2 Answers2

0

you have to use two images to do this.

button_normal button_pressed then create a xml resource in drawable folder

<?xml version="1.0" encoding="UTF-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="false"
    android:drawable="@drawable/button_normal" />

<item android:state_pressed="true"
    android:drawable="@drawable/button_pressed" />

</selector>

then, set this file as a background for the imageview. here we are using imageview as button. dont forget to include those two buttons in the drawable folder.

user1071979
  • 1,701
  • 2
  • 12
  • 25
0

From what I understand is that you are trying to use a button to turn a state off/on, also the button's state will clearly indicate the feature's state.

If I am correct then use custom check box. You will have to anyways define selector for different states of checkbox (as mentioned by user1071979).

Chitranshu Asthana
  • 1,089
  • 8
  • 19