How to check state Changing of CheckBox in android? - android

I want to check if my Checkboxes are checked to change some values.
I got the answer for radioButton which is inserted to a RadioGroup and we can simply use :
radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{ . . . }
}
i'm looking for something like this ,However this approach is differ from Checkboxes because there is no widget like CheckboxGroup to use setOnCheckedChangeListener On it .
For example i have checkbox1 and checkbox2. how can i get as long as these two checkboxes are checked to changing some value like :
if(checkbox1==isChecked && checkbox2==isChecked)
//Do sth ...
else
//Do sth Different
EDIT : Maybe i could not explain my problem perfectly , this is just like question below but for Checkboxes
How to check state Changing of Radio Button android?

As commented you can use checkboxRef.isChecked()
if(checkbox1.isChecked() && checkbox2.isChecked()){
}
else{
}
but I want to use them in a different function , may globally access
to this checkboxes
You can use sharedPreference and with a utility class to store the state of your checkbox.
SharedPreferences helper class

This is way to check which checkbox is checked..
if(checkbox1.isChecked())
{
//means checkbox1 is checked...
}else
{
//means checkbox2 is checked...
}

If you want to set an event listener to detect when a change is made you can use:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//Code here
} } );
On the other hand if you want to check if it is checked you can use:
if(checkbox.isChecked()){
//Code here
}

Finally i got the answer , first of all I need to set onclick attribute as the same for all of the checkboxes , and check my Needed for approach desire output in the onClick Function,
Xml:
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="func"
android:text="1"
android:id="#+id/c1"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:onClick="func"
android:id="#+id/c2"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:onClick="func"
android:id="#+id/c3"/>
JavaCode:
public void func(View view) {
CheckBox checkBox1 = (CheckBox)findViewById(R.id.c1);
CheckBox checkBox2 = (CheckBox)findViewById(R.id.c2);
CheckBox checkBox3 = (CheckBox)findViewById(R.id.c3);
if(checkBox1.isChecked() && checkBox2.isChecked() && (!checkBox3.isChecked()))
{
Next = true;
}
else
{
Next = false;
}
}

checkBox1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkBox1.isChecked() && checkBox2.isChecked()){
}
}
});

Related

How to use Checkbox ClickListener correctly?

I want to show a Seekbar, when I check an Checkbox and the Seekbar shall disappear, when I uncheck the Checkbox.
But it only works for me, that the Seekbar appears by clicking the Checkbox but doesn't disappear, when I uncheck the Checkbox.
I tried it with a ClickListener:
begrenzungsCheckBox = (CheckBox) findViewById(R.id.filebegrenzungCheckBox);
begrenzungsCheckBox.setOnClickListener(filebegrenzungsListenerCheckbox);
OnClickListener filebegrenzungsListenerCheckbox = new OnClickListener() {
#Override
public void onClick(View v) {
TextView filebegrenzungAnzeige = (TextView) findViewById(R.id.filebegrenzungAnzeige);
SeekBar filebegrenzungsSeekbar = (SeekBar) findViewById(R.id.filebegrenzungSeekbar);
filebegrenzungAnzeige.setVisibility(View.VISIBLE);
filebegrenzungsSeekbar.setVisibility(View.VISIBLE);
}
};
and here is the corresponding xml layout file:
<SeekBar
android:id="#+id/filebegrenzungSeekbar"
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content" />
How to use the ClickListener in order to have the possibility to check and uncheck the Checkbox or which alternatives are there to realise that?
OnClickListener fires for every click event. It doesn't care whether the checkbox gets checked or unchecked. Use an OnCheckedChangedListener instead:
*your checkbox*.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
TextView filebegrenzungAnzeige = (TextView) findViewById(R.id.filebegrenzungAnzeige);
SeekBar filebegrenzungsSeekbar = (SeekBar) findViewById(R.id.filebegrenzungSeekbar);
filebegrenzungAnzeige.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
filebegrenzungsSeekbar.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
}
});
Use this:
if (chk1.isChecked()) {
ShowSeekBar();
} else {
makeInvisibleSeekBar();
}

CheckBox won't toggle in Android ListView with focusable="false"

I'm creating a ListView with a custom Adapter for displaying my entries. Each row contains a checkbox, and my adapter contains the following code:
CheckBox cb = (CheckBox) v.findViewById(R.id.item_sold);
cb.setChecked(p.isSold);
setupCheckboxListener(cb, v, position);
...
private void setupCheckboxListener(CheckBox check, final View v, final int position) {
check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
System.out.println("Should become false!");
cb.setChecked(false);
} else {
System.out.println("Should become true!");
cb.setChecked(true);
}
}
});
My row XML file includes the following:
<CheckBox android:id="#+id/item_sold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_gravity="center"
android:gravity="center"
android:focusable="false"
android:clickable="false"/>
But anytime I press one of the checkboxes, check.isChecked() returns true, even if the box is unchecked. I even checked to make sure that the checkboxes were distinct and weren't picking up just the last value/etc.
Setting up the listeners inline instead of in a method doesn't seem to help, either. It's literally just the isChecked() condition that isn't working appropriately - it seems to always give me the inverse value.
Setting an onClick on the row is not acceptable in this case because I need to allow row selection for something else.
What could be causing this issue?

How to set radio button to checked when i click on list item

I am having a problem with my listview ( 2 textview and 1 radio button).
The problem:
My idea is that the user clicks on the item in the listview and the radio button gets checked automatically.
I have been searching for a while, but I can't get the radio button to work.
My XML
<RadioButton
android:id="#+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
My Adapter
r = (RadioButton) convertView.findViewById(R.id.rdBtn);
r.setChecked(selectedPosition == position);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//r.setChecked(true);
Toast.makeText(context, InformationActivity.result,
Toast.LENGTH_SHORT).show();
selectedPosition = (Integer) view.getTag();
notifyDataSetInvalidated();
}
});
return convertView;
I tried
r.setChecked(true);
inside my activity class and the first click worked, but the second chooses a different item on the listview.
I hope some of you can help me.
Thanks
I found the solution here:
How to check checkbox on clicking an image?
searchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
LinearLayout item_view = (LinearLayout) view;
final RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.rdBtn);
if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}
itemcheck.setChecked(true);
<RadioButton
android:id="#+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
I'm not sure I understand what you are trying to achieve, but maybe this would help?
r.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// radio button is checked
} else {
// radio button is not checked
}
}
});
Edit.
Assuming list is your ListView, you could do the following:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
r.setChecked(true); // true to make r checked, false otherwise.
}
});
you can set the list setOnItemClickListener.
You should set OnClick Listener for 'convertview' and toggle radioButton as follows-
convertview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.toggle()
}
});
I solved this issue by using below functionality,
1.Use radiogroup.clearCheck();
To clear the all the checked state of radio buttons in listview scrolling.
2.Maintain the checked state of each radio button in a list.
If radio button checked state is changed update the value in list.
You may use checktextview inside the listview and set the type of checktextview to look like radiobutton or use background image for checktextview check so that it look like radio button.Hope this helps you

Android Development: Checkbox setChecked not working

In my xml:
<CheckBox android:id="#+id/checkboxUpdateLessonPlanAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chkLessonPlanAll"
android:onClick="onCheckboxClicked"/>
In my java:
public void onCheckboxClicked(View view) {
//CheckBox box = (CheckBox) view;
CheckBox box = (CheckBox) findViewById(R.id.checkboxUpdateLessonPlanAll);
box.setChecked(!box.isChecked());
Log.v("qwerty", "checkbox clicked " + box.isChecked() + "!!");
}
I can see my log message in LogCat and it shows it as false when I click on the checkbox but its state doesn't change. It remains unchecked.
Why would you try to overwrite the default behavior with something like the default behavior? The checkbox toggles automatically on every click.
If you want to react on that, use the OnCheckedChangeListener.
To Make CheckBox checked or unchecked you can also use like
box.setChecked(true);
box.setChecked(false);
and to get state of CheckBox
if(box.isChecked()) {
//do something here...
} else {
//do something here...
}

android radiobutton check after clearing check issue

friends. I'm having a really silly problem with radiogroup. Yet I'm unable to find solution.
I will try to describe how to reproduce my problem:
I have radiobutton group and two buttons inside.
I select one of them, lets say 1st one.
Then I'm clearing selection by calling radioGroup.clearCheck()
After I'm trying to select 1st button, but its not checking.
If I check 2nd, it checks normally.
If I check 1st after checking 2nd it also works normally.
This may sound crazy, yet I can't fix it. Please help me, thanks in advance.
I use
#Override
protected void init() {
View view = View
.inflate(getContext(), R.layout.wo_task_yn_result, null);
performed = (RadioButton) view.findViewById(R.id.yn_yes);
notPerformed = (RadioButton) view.findViewById(R.id.yn_no);
radioGroup = (RadioGroup) view.findViewById(R.id.yn_options);
performed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(YES, "verify");
if (isChecked) {
Log.d(YES, "checked");
result = YES;
}
}
});
notPerformed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView,
final boolean isChecked) {
Log.d(NO, "verify");
if (isChecked) {
Log.d(NO, "checked");
result = NO;
}
}
});
addView(view);
}
To create buttons and
#Override
public void clear() {
radioGroup.clearCheck();
result = "";
}
for clearing them
I had the same problem. Was unable to select the first radioButton in the group, but could select the other two. And after selecting the second radioButton, I could select the first one as well. I resolved it by doing a single radioGroup.clearCheck() instead of individual radioButtonA.setChecked(false)
Use OnCheckedChangeListener and make use of the method setSelected(true) or setChecked(true).
if u want to uncheck radio button try this method:
#Override
public void clear() {
performed.setChecked(false);
nonperformed.setChecked(false);
}

Categories

Resources