Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox and radio buttons

Tags:

android-xml

Do checkboxes have the permission to behave like radio buttons.I am developing a quiz application where in the options have the behaviour of radio buttons and the icon of the options are to be like the checkbox and is it possible for me to group the checkbox as we group radio buttons?

like image 466
Vivek Kalkur Avatar asked Aug 02 '11 10:08

Vivek Kalkur


People also ask

What is the difference between checkbox and option button?

Main differences. In Check box, you can select multiple options. In Option Button (Radio button) you can select one option. Radio buttons are circular and check boxes are square.

Does checked work for radio buttons?

The checked property sets or returns the checked state of a radio button. This property reflects the HTML checked attribute.

What is check box button?

The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

If you want Radio Buttons which look like Check boxes. Set Style of RadioButton as @android:style/Widget.CompoundButton.CheckBox

e.g:

<RadioButton style="@android:style/Widget.CompoundButton.CheckBox" />
like image 85
Goutham Avatar answered Oct 14 '22 11:10

Goutham


I don't know if this is the best solution but you can create a "manager" for your checkboxes, and run it whenever any of them gets clicked.

For simplicity I've added the manager in the xml code, but feel free to use setOnClickListener, or setOnCheckedChangeListener as well.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox1" 
    android:onClick="cbgroupmanager"
    />

  ...

<CheckBox
    android:id="@+id/checkBox5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox5" 
    android:onClick="cbgroupmanager"/>

</LinearLayout>

You need an ArrayList to iterate through, so you can settle the status of each checkbox, whenever any of them gets clicked on.

   public class Q6910875 extends Activity 

    ArrayList<CheckBox> cb = new ArrayList<CheckBox>();         
    int CheckBoxNum = 5; //number of checkboxes
    Iterator<CheckBox> itr ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        cb.add((CheckBox) findViewById(R.id.checkBox1));
        cb.add((CheckBox) findViewById(R.id.checkBox2));
        cb.add((CheckBox) findViewById(R.id.checkBox3));
        cb.add((CheckBox) findViewById(R.id.checkBox4));
        cb.add((CheckBox) findViewById(R.id.checkBox5));
        itr = cb.iterator();

    }

And here we have our manager, one iterator to pass trough the whole list, unchecking everything, when it reaches the clicked one, checks!

public void cbgroupmanager(View v) { 
    CheckBox cbaux;
    while(itr.hasNext()) {
        cbaux = (CheckBox) itr.next(); // we need this because it returns a Object, and we need the setChecked, which is a CheckBox method.
        Log.d("soa", "click");
        if (cbaux.equals(v))     //if its the one clicked, mark it as checked!
            cbaux.setChecked(true);
         else 
            cbaux.setChecked(false);

    }

I also could find this other solution linked below, you can change the theme of your checkbox, but I dont have any experience on themes, so I can't help you any further on this approach.

Is it possible to change the radio button icon in an android radio button group

like image 39
caiocpricci2 Avatar answered Oct 14 '22 12:10

caiocpricci2