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);
}
}
});
Related
I have set setFocusable(false) in my edit text. But when i checked checkbox i want to get focus in my edittext. below is example.
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
edittext1.setFocusable(true);
edittext1.setClickable(true);
}else {
edittext1.setFocusable(false);
edittext1.setClickable(false);
}
}
});
I am trying above example but i am not getting focus when i checked checkbox
I have a set of switch components, around 30. I want to override the setOnCheckedChangeListener so that I can avoid unnecessary code that does nearly the same thing.
I know, it is possible for onClick function, so using multiple buttons and changing the logic in the overridden function is way better.
What I have so far is:
final Switch question1 = (Switch) findViewById(R.id.question1);
question1.setText(R.string.no);
question1.setChecked(false);
question1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question1.setText(R.string.yes);
} else {
question1.setText(R.string.no);
}
UpdateScores();
}
});
final Switch question2 = (Switch) findViewById(R.id.question2);
question2.setText(R.string.no);
question2.setChecked(false);
question2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question2.setText(R.string.yes);
} else {
question2.setText(R.string.no);
}
UpdateScores();
}
});
As can be seen much of the code is replicated and I don't want that. Is there any way to achive this?
You can create one listener for all your switchers.
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
And then add this listener to your Switchers:
question1.setOnCheckedChangeListener(listener);
question2.setOnCheckedChangeListener(listener);
....
question30.setOnCheckedChangeListener(listener);
OnCheckedChangeListner onchange = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
}
question2.setOnCheckedChangeListener(onchange);
you want it?
I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!
In Android, is this code rith to verify that the checkbox is clicked to be unchecked ?
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(IdentifyActivity.this, "clicked to check", Toast.LENGTH_LONG);
}
else{
Toast.makeText(IdentifyActivity.this, "clicked to uncheck", Toast.LENGTH_LONG);
}
you can use OnCheckedChangeListener. you have to implement the callback
onCheckedChanged(CompoundButton buttonView, boolean isChecked)
that's the way your task is usualy done. Your code works as well and, probably, between both approces there is not a real difference
CheckBox cb = (CheckBox) findViewById(R.id.myCheckBox);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// It's checked
} else {
// It's not checked
}
}
});
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked==true)
{
//your checkbox is checked
}
else
{
//your checkbox is not checked
}
}
}
I have a number of check boxes creating dynamically.
Now i just want to select any of them not more than one. When i select the second one the first one should unselect.
I can't use radio group. Any help
Here is my code .. i am creating check boxes dynamically
private void createRadioButton() {
cb = new CheckBox(fContext);
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
Log.i("comVisa","getPos =="+getPosition);
}
});
}
You can save last check box checked and unchecked it when checked other one like this
In public area define this
HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();
then edit your code :
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//int getPosition = (Integer) buttonView.getTag();
if(isChecked){
if(hash.size()>0){
hash.get("1").setChecked(false);
}
hash.put("1", buttonView);
}else{
hash.clear();
}
Log.i("comVisa","getPos =="+getPosition);
}
});
A really simple example of using single selection checkbox. try this link
CheckBox myCheckBox[] = new CheckBox[noofCheckBox];
for (int i=0; i<noofCheckBox; i++) {
myCheckBox[i] = new myCheckBox(this);
myCheckBox[i].setId(i+1);
myCheckBox[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if(myCheckBox[i].getId() == getPosition){
myCheckBox[i].setChecked(true);
}else{
myCheckBox[i].setChecked(false)
}
}
});
}