Jame
Jame

Reputation: 3854

Set checked/selected imagebutton in Android

I have a linear layout which contains three image buttons. Each image buttons has a different color. At the initial time, all image buttons are unchecked. I want to set checked/selected for an image button if the image button is selected (a blue V will overlap to the image button background ), and Another image button will be unchecked. How can I do it in android?

This is my layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="7dp"
    android:layout_centerInParent="true">
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color1"
        android:background="@color/colorAccent"/>
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color2"
        android:layout_marginLeft="5dp"
        android:background="@color/colorPrimaryDark" />
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color3"
        android:layout_marginLeft="5dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>

Updated: The blue V means checked status. It is similar the result

enter image description here

Upvotes: 1

Views: 1391

Answers (1)

Sergey Trukhachev
Sergey Trukhachev

Reputation: 538

I would suggest to use checkboxes for this task.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="7dp"
    android:layout_centerInParent="true">
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color1"
        android:button="@drawable/color_selector"
        android:background="@color/accent"/>
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color2"
        android:button="@drawable/color_selector"
        android:layout_marginLeft="5dp"
        android:background="@color/primary"
        android:checked="false" />
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:button="@drawable/color_selector"
        android:id="@+id/color3"
        android:layout_marginLeft="5dp"
        android:background="@color/primary_dark"/>
</LinearLayout>

and color_selector.xml should be placed into the drawable folder with the required selector (just an example)

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

<item android:state_checked="true" android:drawable="@drawable/ic_done_white_18dp">
    <shape android:shape="rectangle" >
        <stroke android:color="@color/white" android:width="2dp"/>
        <corners
            android:bottomLeftRadius="@dimen/spacing_xxsmall"
            android:bottomRightRadius="@dimen/spacing_xxsmall"
            android:topLeftRadius="@dimen/spacing_xxsmall"
            android:topRightRadius="@dimen/spacing_xxsmall" />
    </shape>
</item>
<item android:state_checked="false">
    <shape android:shape="rectangle">
        <stroke android:color="@color/white" android:width="1dp"/>
        <corners
            android:bottomLeftRadius="@dimen/spacing_xxsmall"
            android:bottomRightRadius="@dimen/spacing_xxsmall"
            android:topLeftRadius="@dimen/spacing_xxsmall"
            android:topRightRadius="@dimen/spacing_xxsmall" />
    </shape></item>
</selector>

Hope this will help.

Upvotes: 1

Related Questions