Hans Adiputra Arijanto
Hans Adiputra Arijanto

Reputation: 655

Android bg drawable selector state of most recent presed

I have a button that is assigned a bg with a selector that controls its bg drawable ( shown below ). I can't seem to find a state where an item persist after a button has been pressed. To clarify iam looking for a state where the current/most recent button pressed can be assigned an image accordingly.

Does anyone know how this can be done?

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true"
    android:dither="false"
    android:variablePadding="false">
    <item android:drawable="@drawable/option_button_yellow" android:state_pressed="true"/>
    <item android:drawable="@drawable/option_button_yellow" android:state_focused="true"/>
    <item android:drawable="@drawable/option_button_white"/>
</selector>

Upvotes: 0

Views: 202

Answers (3)

Hans Adiputra Arijanto
Hans Adiputra Arijanto

Reputation: 655

Hey guys so i think i was unclear about the last question and might have found a solution myself. I was looking for a mutually exclusive set of buttons that will keep its changed bg image once clicked and revert the rest of the button to its default bg image. I seem to have been able to do this by replacing the button with radio buttons and setting its parent to radio group. Plus simple changing the xml drawable selector to contain a state_checked = "true" item. However I am now having issues trying to include alligned text in a custom bg to radio button while removing its button ( setting android:button="@nul" ). This seems to mess up the alignment. Any thoughts?

layout

<RadioGroup android:id="@+id/options" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"  android:paddingTop="@dimen/option_button_layout_top_margin" android:paddingBottom="@dimen/option_button_layout_bottom_margin">

        <RadioButton android:id="@+id/option_skill" android:text="@string/option_skill" style="@style/option_button"/>
        <RadioButton android:id="@+id/option_item" android:text="@string/option_item" style="@style/option_button" android:layout_marginLeft="@dimen/option_button_spacing"/>

    </RadioGroup>

style.xml

<style name='option_button'>
        <item name="android:layout_width">@dimen/option_button_size</item>
        <item name="android:layout_height">@dimen/option_button_size</item>
        <item name="android:button">@null</item>
        <item name="android:background">@drawable/option_button</item>
        <item name="android:onClick">option_onclick</item>
</style>

drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true"
    android:dither="false"
    android:variablePadding="false">
    <item android:drawable="@drawable/option_button_yellow" android:state_pressed="true"/>
    <item android:drawable="@drawable/option_button_yellow" android:state_checked="true"/>
    <item android:drawable="@drawable/option_button_white"/>
</selector>

Upvotes: 0

Ali
Ali

Reputation: 12684

Use a ToggleButton then you can use state_checked in your selector. If you want it to uncheck the button if something else is clicked then use a RadioGroup with ToggleButtons inside it (or possibly other views), you'll have to manage the onXXX() events yourself there is an example here.

Upvotes: 1

Salman Khakwani
Salman Khakwani

Reputation: 6714

May be you are looking for some thing like this:

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

where btn_state_highlighted and btn_state_normal are your images for the respective states of your button.

Upvotes: 0

Related Questions