android checkbox and check change listener - android

in my android app i have a custom table list with one checkbox for each row.
i would like to check, if a checkbox was checked or unchecked.
for this i have this code:
SelectCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (SelectCheckBox.isChecked()) {
Log.e("-->", "IS CHECKED");
} else {
Log.e("-->", "IS NOT CHECKED");
}
}
});
this works fine, if i check/uncheck the checkboxes.
but i have got another code, which checks all checkboxes at once, after pressing a button.
SelectCheckBox.setChecked(true);
but than the CheckedChangeListener will not be active.
Here my Code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_CheckAll:
for (int i = 0; i < 30; i++) {
SelectCheckBox.setChecked = true;
break;
}
}
}

SelectCheckBox.performClick() insted of SelectCheckBox.setChecked(true); should solve your issue. I think the way you are coding .setChecked(true); is forcing the checkbox to stay in checked state.
Can you post your full activity code.

Related

Android - How to filter checkboxes with drawables by clicking a checkbox with multiple checkboxes

I have been researching this, and I can't find a solution with a get drawable with a checkbox. What I'm trying to do is when you click this particular checkbox, it will filter the checkboxes of the corresponding color. This is in a fragment by the way. So, I have checkboxes that can be red, yellow, or green. If the user clicks on them, it changes the color. For example, I have a checkbox for the yellow which is chkProgress, and when it is clicked, I want to find all the checkboxes that are yellow and filter the checkboxes that are yellow. So, I have drawable ids, and I need to get the drawables to be able to locate the checkboxes that are that color. I want to be able to filter the checkboxes that are yellow when the user clicks chkProgress. So, here is my code:
private CheckBox [] checkBoxes = new CheckBox[2];
checkBoxes[0] = chkStart;
checkBoxes[1] = chkDownload;
private Drawable yellowDrawable = getResources().getDrawable(R.drawable.custom_yellow_checkbox);
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()){
//something here
if (chkStart.getDrawable == yellowDrawable) // need proper code here or a for loop to do this efficiently.
} else if (!chkInProgess.isChecked()){
chkInProgess.setChecked(true);
}
}
});
Here are the checkboxes that I would need to find if they are yellow:
chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countStart = checked(chkStart, countStart);
editor.putInt(CHECK_START_ID, countStart);
editor.apply();
}
});
chkDownload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countDownload = checked(chkDownload, countDownload);
editor.putInt(CHECK_DOWNLOAD_ID, countDownload);
editor.apply();
}
});
Here is the method for checking the checkboxes if they are a certain color:
public int checked(CheckBox checkBox, int count){
if (checkBox.isChecked()){
count++;
} else if (!checkBox.isChecked()){
checkBox.setChecked(true);
count++;
}
if (count == 1){
checkBox.setButtonDrawable(R.drawable.custom_yellow_checkbox);
}
if (count == 2){
checkBox.setButtonDrawable(R.drawable.custom_green_checkbox);
}
if (count > 2){
count = 0;
checkBox.setButtonDrawable(R.drawable.custom_red_checkbox);
}
return count;
}
Here is the method when I resume the app:
#Override
public void onResume() {
super.onResume();
start = mSharedPreferences.getInt(CHECK_START_ID, 0);
start = idChecked(chkStart, start);
countStart = start;
download = mSharedPreferences.getInt(CHECK_DOWNLOAD_ID, 0);
download = idChecked(chkDownload, download);
countDownload = download;
}
So, I am trying to get the drawables and match them to the checkboxes that are that color. I can't find anything that would help me. Any help would be much appreciated. Thanks!
I figured out the problem. So, I made a method to check if the count of the checkbox was a certain number like I did for the checked method. I would set the checkbox with that number to visible or not.
public int colorCheckYellow(CheckBox checkbox, int value){
if (value == 1)
checkbox.setVisibility(View.VISIBLE);
else
checkbox.setVisibility(View.GONE);
return value;
}
Then I call it like this:
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()) {
countStart = colorCheckYellow(chkStart, countStart);
countDownload = colorCheckYellow(chkDownload, countDownload);
}else if (!chkInProgess.isChecked()) {
checkboxVisible();
}
}
I have some other code as well to make everything work but that was it!

Getting the isChecked() value from CheckBox or RadioButton

I have set the click event listener on a number of CheckBox and RadioButton views. Both of these view types have an isChecked() method. I can get the state of either one by casting to its type for every single switch case, but can I get the state just in a general sense for either of them before I enter the switch cases?
#Override
public void onClick(View view) {
boolean checkState;
switch (view.getId()) {
case R.id.radio_button_1:
checkState = ((RadioButton) view).isChecked();
// ...
break;
case R.id.check_box_1:
checkState = ((CheckBox) view).isChecked();
// ...
break;
// more check boxes and ratio buttons ...
}
}
This question has a similar title to this one, but there was too much code there for me to easily understand if they were asking the same thing.
While I was in the process of writing this question, I found an answer, which I will post below. However, if there is a better answer, I will accept it.
Consider one RadioButton and one CheckBox
RadioButton button = (RadioButton) findViewById(R.id.radio_button);
button.setOnCheckedChangeListener(listener);
CheckBox box = (CheckBox) findViewById(R.id.checkbox);
box.setOnCheckedChangeListener(listener);
Both have a single listener
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView instanceof CheckBox ){
Toast.makeText(getApplicationContext(), "ChechBox " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "RadionButton " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
}
}
};
If there are many still we can use a single listener as follows
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView instanceof CheckBox ){
final ChechBox b = (ChechBox)buttonView;
switch(b.getId()){
case R.id.first_checkbox:
. . . . . . . .
}
}else {
final RadioButton r = (RadioButton)buttonView;
switch(r.getId()){
case R.id.first_radio
. . . . . . . . . . .
}
}
}
};
You need to attach all of the views to this listener
Direct Answer
As #MikeM answered in the comments, you can use instanceof to check once for CompoundButton or Checkable and cast to that.
#Override
public void onClick(View view) {
boolean checkState = false;
if (view instanceof Checkable) {
checkState = ((Checkable) view).isChecked();
}
switch (view.getId()) {
// ...
}
}
Better Answer
However, as #MikeM also mentioned, you should be using an OnCheckedChangeListener rather than an OnClickListener. There are two similar checked changed listeners:
CompoundButton.OnCheckedChangeListener
RadioGroup.OnCheckedChangeListener
The CompoundButton listener can be used for a CheckBox or a RadioButton. However, if all the radio buttons in a RadioGroup had this set, the listener would get called twice, once for the button getting turned off and once for the button getting turned on. Thus, it is better to use the RadioGroup listener for radio buttons.
So add the RadioGroup.OnCheckedChangeListener to the RadioGroup and the CompoundButton.OnCheckedChangeListener to each CheckBox.
// set RadioGroup.OnCheckedChangeListener
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(radioGroupListener);
// set `CompoundButton.OnCheckedChangeListener`
CheckBox cb1 = (CheckBox) view.findViewById(R.id.check_box_1);
cb1.setOnCheckedChangeListener(checkBoxListener);
CheckBox cb2 = (CheckBox) view.findViewById(R.id.check_box_2);
cb2.setOnCheckedChangeListener(checkBoxListener);
// ...
where the listeners are implemented as follows:
Radio Buttons
The isChecked() value is true for whatever RadioButton id is returned by the RadioGroup listener.
RadioGroup.OnCheckedChangeListener radioGroupListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
// checkedId is the id of the newly checked radio button
switch (checkedId) {
case R.id.radio_button_1:
// ...
break;
case R.id..radio_button_2:
// ...
break;
// ...
}
}
};
Check Boxes
The isChecked value is directly passed in to the CompoundButton listener.
CompoundButton.OnCheckedChangeListener checkBoxListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.checkbox_1:
// ...
break;
case R.id.checkbox_2:
// ...
break;
}
}
};

How to Go to the next page if RadioButton is checked ? in Android programming

I am new in android . I want to go to the next page if Radio button is checked . I wrote below code but it doesn't work .
what is the problem ?
public class second extends Activity {
public void onCreate (Bundle shamim){
super.onCreate(shamim);
setContentView(R.layout.second);
findViewById(R.id.radioButton1);
RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
findViewById(R.id.radioButton2);
RadioButton radioButton2 = (RadioButton)findViewById(R.id.radioButton2);
if (radioButton1.isChecked()) {
startActivity(new Intent(second.this,third.class));
else { (radioButton2.isChecked()) {
startActivity(new Intent(second.this,MyActivity.class));
}
}
}
}
isChecked() will only give you the current state of the button. It won't inform you when the state has changed (which by default is not checked), so your code isn't set up to react to when a user actually clicks the button.
Instead of calling isChecked, you probably want to set a listener on each button to know when either is clicked, so that you can respond appropriately:
radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
startActivity(new Intent(second.this,third.class));
}
}
}
radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
startActivity(new Intent(second.this,MyActivity.class));
}
}
}

Android: How to uncheck checkbox without perform click on it?

I have 3 checkbox in my Layout:
(case #1) mCheckBoxAll: when is checked or unchecked, all other boxes are set in the same state.
(case #2) mCheckBoxChoice1 and mCheckBoxChoice2: if one of these is unchecked by the user then the mCheckBoxAll must be unchecked.
mCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
mCheckBoxChoice2.setChecked(isChecked);
}
});
mCheckBoxChoice1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked)
mCheckBoxAll.setChecked(!isChecked);
}
});
But this code doesn't work in case #2. All cases are unchecked in the same time...
I would put all your checkboxes that should change at once in a RadioGroup, and when mCheckBoxAll is checked iterate over RadioGroup and select / deselect them all
case 1
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
for (int i = 0; i < radioGroup.getChildCount(); i++){
((RadioButton) radioGroup.getChildAt(i)).setChecked(isChecked);
}
case 2 - you could call this method, to determine if one of the buttons is unchecked (call inside your 2 buttons onCheckedChanged). If it returns false, deselecte the main RadioButton.
private boolean isButtonUnchecked() {
for (int i = 0; i < radioGroup.getChildCount(); i++){
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (!radioButton.isChecked()) {
return false;
}
}
return true;
}
When you checkbox2 is unchecked then you set the state of checkboxall to be unchecked, and there you call checkbox1 to be uncheck as well, here is your problem.
So if checkbox 1 and 2 are checked and then you unchecked checkbox2 it will affect checkbox1.
Edit:
You should identify the source of the unchecked in checkboxall, if it comes from one of the two checkbox or if it comes from the uncheck all.
The easiest solution is to declare a global variable in your activity/fragment which tells you if you comes from checkboxes 1 and 2.
And if your checkboxall listener uncheck 1 and 2 only if the value is true.
Don't forget to change its value when you are done to false.
Edited code:
modify your code to
mListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxChoice1.setChecked(isChecked);
}
};
mCheckBoxAll.setOnCheckedChangeListener(mListener);
mCheckBoxChoice1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
mCheckBoxChoice2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
mCheckBoxAll.setOnCheckedChangeListener(null);
mCheckBoxAll.setChecked(!isChecked);
mCheckBoxAll.setOnCheckedChangeListener(mListener)
}
}
});
use below code and it will work your scenario
int mCheckBoxAll_clicktimes=0;
int mCheckBoxChoice2_clicktimes=0;
int mCheckBoxChoice1_clicktimes=0;
mCheckBoxAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes++;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxAll_clicktimes%2 !=0 || mCheckBoxAll_clicktimes==0){
//-----when mcheckbox all checked
mCheckBoxAll.setChecked(true);
mCheckBoxChoice1.setChecked(true);
mCheckBoxChoice2.setChecked(true);
}else{
//===when mcheckboxall unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
}
}
});
mCheckBoxChoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes=0;
mCheckBoxChoice1_clicktimes++;
if(mCheckBoxChoice1_clicktimes%2 !=0 || mCheckBoxChoice1_clicktimes==0){
//when mCheckBoxChoice1 checked
mCheckBoxChoice1.setChecked(true);
}else{
//====when mCheckBoxChoice1 unchecked
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});
mCheckBoxChoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckBoxChoice1.setChecked(false);
mCheckBoxAll.setChecked(false);
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll_clicktimes=0;
mCheckBoxChoice2_clicktimes++;
mCheckBoxChoice1_clicktimes=0;
if(mCheckBoxChoice2_clicktimes%2 !=0 || mCheckBoxChoice2_clicktimes==0){
//when mCheckBoxChoice2 checked
mCheckBoxChoice2.setChecked(true);
}else{
//when mCheckBoxChoice2 unchecked
mCheckBoxChoice2.setChecked(false);
mCheckBoxAll.setChecked(false);
}
}
});

Access the button in parent view from a switch android

I have an issue accessing the view from the parent of a compoundbutton.
((Switch) convertView.findViewById(R.id.push_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (rowItem.getPush().equals("N")) {
rowItem.setFollow(true);
rowItem.setPush("Y");
setFollowTickGreen(rowItem, convertView);
}
else if (rowItem.getPush().equals("Y")) {
rowItem.setPush("N");
}
}
});
This code shows my switch button but I need to get access to convertView to change something else when this button is pressed.
This is being done inside an adapter, so the switch code is in the getView function but the setFollowTickGreen is outside of that function.
I can't set convertView as final as it changes throughout the getView function.
I understand that the compoundButton is the switch but the switch is at the same level that I want to access a Button.
Is there a general way of doing this?
This would be similar for other situations, as well as compoundbuttons I assume.
Thanks.
Does this help..
final View tmpConvertView=convertView;
((Switch) convertView.findViewById(R.id.push_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (rowItem.getPush().equals("N")) {
rowItem.setFollow(true);
rowItem.setPush("Y");
setFollowTickGreen(rowItem, tmpConvertView);
}
else if (rowItem.getPush().equals("Y")) {
rowItem.setPush("N");
}
}
});

Categories

Resources