Yoo
Yoo

Reputation: 1441

Select only one checkbox

I have a multiple checkboxes, but I want user to select one checkbox only. I try if-else statement but it is not useable. So, I want to know, how to be like that.

Thanks

Upvotes: 10

Views: 50486

Answers (8)

eggham0518
eggham0518

Reputation: 151

val checkBoxList = mutableListOf<CheckBox>()
checkBoxList.add(...)

      checkBoxList.forEach {
        it.setOnCheckedChangeListener { buttonView, isChecked ->
            checkBoxList.forEach {
                it.isChecked = false
            }
            it.isChecked = isChecked
        }
    }

Upvotes: 0

Ismail Osunlana
Ismail Osunlana

Reputation: 434

The simplest approach.

STEP 1

chkEnglish.setOnClickListener(checkboxListener);
chkFrench.setOnClickListener(checkboxListener);

STEP 2

View.OnClickListener checkboxListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v == chkEnglish){
            chkEnglish.setChecked(true);
            chkFrench.setChecked(false);
        } else   if (v == chkFrench){
            chkFrench.setChecked(true);
            chkEnglish.setChecked(false);
        }
    }
};

Upvotes: 0

user15351527
user15351527

Reputation:

use setoncliklistener

gdA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gdAB.setChecked(false);
                gdB.setChecked(false);
                gdO.setChecked(false);
            }
        });
        gdB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gdA.setChecked(false);
                gdAB.setChecked(false);
                gdO.setChecked(false);
            }
        });
        gdAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gdA.setChecked(false);
                gdB.setChecked(false);
                gdO.setChecked(false);
            }
        });

Upvotes: 0

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13129

For Kotlin I manage to do this.

Example

First store a value isChecked in your Object

data class OptionsDetails(val optionName: String, var isChecked: Boolean = false)

Then, if you are using a recyclerview with multiple CheckBoxes, just check the object that you just pressed

inner class OptionRemoveViewHolder(itemView: View) : BaseViewHolder<OptionsDetails>(itemView) {
        override fun bind(item: OptionsDetails, position: Int) {

            itemView.checkBox.setOnCheckedChangeListener(null)
            itemView.item_name.text = item.optionName
            itemView.checkBox.isChecked = item.isChecked

            itemView.setOnClickListener {
                itemView.checkBox.isChecked = !itemView.checkBox.isChecked
            }

            itemView.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                itemClickListener.onCheckBoxClickListener(position)
            }
        }
    }

Here I have an interface that I will use in my view to interact with that checkbox click, then in my view I just put to false the others checkboxes that are not currently selected and just pass true to isChecked to the object I have already pressed.

  override fun onCheckBoxClickListener(position:Int) {

        for((index,value) in optionList.withIndex()){
            value.isChecked = position == index
        }
        adapter.notifyDataSetChanged()
}

Thats it, this will let you just check one checkbox at the time

Upvotes: 0

Mohamed AbdelraZek
Mohamed AbdelraZek

Reputation: 2819

it's recommended to use Radio Buttons instead but as in my case I had to follow design so I did it like that using Kotlin.

override setOnClickListener when checkBox clicked make other check boxes unchecked

driver_check_box.setOnClickListener() {
            driver_check_box.isChecked = true
            station_check_box.isChecked = false
            broadcast_check_box.isChecked = false

        }
        station_check_box.setOnClickListener {
            driver_check_box.isChecked = false
            station_check_box.isChecked = true
            broadcast_check_box.isChecked = false

        }
        broadcast_check_box.setOnClickListener {
            driver_check_box.isChecked = false
            station_check_box.isChecked = false
            broadcast_check_box.isChecked = true
        }

enter image description here

Upvotes: 3

yago
yago

Reputation: 59

I made this:

public void check_checkbox(){

    int CB_count=0;
    if (CB_1.isChecked()) {
        CB_count=CB_count+1;
    }if (CB_2.isChecked()) {
        CB_count=CB_count+1;
    }if (CB_3.isChecked()) {
            CB_count=CB_count+1;}

        if (CB_count == 1) {

            //do your magic

        } else {
            Toast.makeText(getActivity(), "you only can select 1 checkbox", Toast.LENGTH_SHORT).show();
        }
}

I improve it, on the onCreateView=

CB_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    CB_2.setChecked(false);
                    CB_3.setChecked(false);
                }
            }
        });
        CB_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    CB_1.setChecked(false);
                    CB_3.setChecked(false);
                }
            }
        });
        CB_3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    CB_2.setChecked(false);
                    CB_1.setChecked(false);
                }
            }
        });

Upvotes: 3

Nanda
Nanda

Reputation: 1058

Why don't you use Radio Buttons instead? They are meant exactly for that.

But if you insist on using check boxes, modify the event of on select of any check box such that when it is selected it disables the other check boxes.

Upvotes: 21

Lerb90
Lerb90

Reputation: 124

I did this:

- In my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alignmentMode="alignBounds"
    android:columnCount="1"
    android:background="#ff99f6ff"
  >

    <ImageView
        android:id="@+id/escudo"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center_horizontal|fill_vertical"
        android:src="@drawable/dicon2"
        android:contentDescription="foto logo"
        android:layout_marginTop="30dp"
        android:layout_alignParentTop="false"
        android:layout_centerInParent="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="top"
        android:layout_below="@+id/radioGroup"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="34dp"
        android:gravity="center_horizontal"
        android:checkedButton="1"
        >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/check_a"
            android:id="@+id/checkBoxA"
            android:layout_above="@+id/escudo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:onClick="onCheckboxClicked" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/check_b"
            android:id="@+id/checkBoxB"
            android:layout_alignTop="@+id/checkBoxA"
            android:layout_toRightOf="@+id/checkBoxA"
            android:layout_toEndOf="@+id/checkBoxA"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/check_c"
            android:id="@+id/checkBoxC"
            android:layout_alignTop="@+id/checkBoxB"
            android:layout_toRightOf="@+id/checkBoxB"
            android:layout_toEndOf="@+id/checkBoxB"
            android:onClick="onCheckboxClicked"/>

    </RadioGroup>
</RelativeLayout>


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends ActionBarActivity {

    private CheckBox checkBoxA, checkBoxB, checkBoxC;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkBoxA = (CheckBox) findViewById(R.id.checkBoxA);
        checkBoxB = (CheckBox) findViewById(R.id.checkBoxB);
        checkBoxC = (CheckBox) findViewById(R.id.checkBoxC);
    }

    public void onCheckboxClicked(View view) {

        switch(view.getId()) {

            case R.id.checkBoxA:

                    checkBoxB.setChecked(false);
                    checkBoxC.setChecked(false);

                break;

            case R.id.checkBoxB:

                    checkBoxC.setChecked(false);
                    checkBoxA.setChecked(false);

                break;

            case R.id.checkBoxC:

                    checkBoxA.setChecked(false);
                    checkBoxB.setChecked(false);

                break;
        }
    }
}

Upvotes: 2

Related Questions