سؤال

I'm trying to get my buttons to show 3 states, normal, pressed and selected. Normal and pressed show ok, but the selected state does not. I've tried using state_focused state_activated and state_active and combinations of states set to true and false but without success.

Can buttons show the selected state? and if so what am I doing wrong?

minSdkVersion = 11 targetSdkVersion = 19

Here's the xml for the selector, red_button.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape>
            <solid android:color="#000000" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ef4444" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ef4444"
                android:endColor="#992f2f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

and here's the xml for the button, which is contained within a LinearLayout and loaded as a fragment into mainActivity

<Button
            android:background="@drawable/red_button"
            android:id="@+id/scene_1_btn"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:maxWidth="150dp"
            android:minWidth="100dp"
            android:text="@string/scene_1_btn_text"
            android:onClick="sendMessage"
            style="@style/button_text" />

Thank you.

هل كانت مفيدة؟

المحلول

Buttons actually behave like keyboard buttons, i.e. they have only two states viz normal and pressed.

If you want to remember/show the seleted state, i got an idea..., try using 'radio group' (if you want only one to belected from a group) or use toggle button/switch (each of them remember their state as ON or OFF) and create their custom UI so that they look like buttons as you want them to.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top