Jennifer
Jennifer

Reputation: 1852

Android: Multiple selectable buttons

I've recently started to build android apps, and I wanted to know if it's actually possible to make a selectable button in android like the image in the link below ?

https://lh4.ggpht.com/ouHPcTcFzsdYrTU09pStGBicxgX_cki613g5Eq3loYCh2TOXzqpfeyWnOdlLuc8eNS0=h900-rw

I'm trying to make a list of numbers that you can do multiple checks, however, I'm not sure if I must use a check box or some other widget. It would be great if I can hear some tips from the pros here!

Upvotes: 0

Views: 3051

Answers (2)

Himanshu arora
Himanshu arora

Reputation: 161

Try this sample

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view" />
</LinearLayout>

</RelativeLayout>

button_change_view.xml is :-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/location_hide"
      android:state_checked="true" />
<item android:drawable="@drawable/location_show"
      android:state_checked = "false" />
</selector>

Using this you don't need to maintain button state , checkbox maintain by itself.

Upvotes: 0

einschnaehkeee
einschnaehkeee

Reputation: 1888

It really depends on what logic you want to have. Are there any logical connections between those numbers? Any conditions?

You could also use a ToggleButton: http://developer.android.com/guide/topics/ui/controls/togglebutton.html

Styling: Android toggle button custom look

Then there is the question of how many buttons do you want to have? Perhaps a variable amount? Then you could use a GridView with ToggleButtons.

Also there are possibilities to use RadioButtons, it's up to you.

Upvotes: 1

Related Questions