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!
Aucun commentaire:
Enregistrer un commentaire