1

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

Jame
  • 3,746
  • 6
  • 52
  • 101

1 Answers1

1

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.

  • You do not need to use `AppCompatCheckBox` unless you are creating a custom `View` class; it will automatically be used where applicable. From the [documentation](https://developer.android.com/reference/android/support/v7/widget/AppCompatCheckBox.html): "This will automatically be used when you use `CheckBox` in your layouts. You should only need to manually use this class when writing custom views." – Bryan Oct 04 '16 at 15:55
  • Sorry for the delay. This is exactly what I need. Could you suggest some value for `@dimen/spacing_xxsmall` in `android:bottomLeftRadius`, `android:bottomRightRadius`... – Jame Oct 05 '16 at 06:03
  • 1
    Well ... you can use whatever you need there, I used 2dp – Sergey Trukhachev Oct 05 '16 at 08:36