If statement for two checkboxes are checked problem - java

I am trying to write a code that states that if one checkbox is checked the second one can not be checked at the same time.
I have one checkbox called dayShift and another called nightShift.
If I am checking nightShift and THEN dayShift the code works as I expected. But if I check dayShift first and THEN nightShift, the code does not apply.
public void days(View view) {
CheckBox nightShift_check = (CheckBox)findViewById(R.id.nightShift);
CheckBox dayShift_check = (CheckBox)findViewById(R.id.dayShift);
if (nightShift_check.isChecked() && dayShift_check.isChecked())
{
nightShift_check.setChecked(false);
dayShift_check.setChecked(false);
Toast.makeText(mainApplication.this,"Error message",Toast.LENGTH_LONG).show();
}
}

If you need to perform different logic for that you could use RadioButton so there can't be two statements true at the same time.
Otherwise you could add something that is called setOnCheckedChangeListener on your CheckBox and uncheck them both if they both are checked for example:
dayShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && nightShift_check.isChecked()) {
dayShift_check.setChecked(false);
nightShift_check.setChecked(false);
}
}
});
But I still strongly recommend you to use RadioButton if you need to choose from two, as #NileshRathod mentioned above in comment section.
If you need to choose one option from more you probably want to use RadioGroup.

You should use a listener on your CheckBox to get the state at each click :
nightShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dayShift_check.setChecked(!isChecked);
}
});
dayShift_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
nightShift_check.setChecked(!isChecked);
}
});
When one have a state the other have the opposite state.
Hope it's help.

Related

setVisibility of TableRow using CheckBox in android studio

I am currently working on android studio, but I cannot understand why my code does not work.
if (paidCheck.isChecked()) {
paidRow.setVisibility(View.VISIBLE);
} else {
paidRow.setVisibility(View.GONE);
}
As you can see, variables end with Checks are CheckBoxes, and end with Rows are TableRows.
My problem is even if I tap the CheckBoxes, TableRows do not appear in the app. Is there anything I did wrong?
The logic you have implemented gets called once whenever your activity or Fragment loads up, it does not gets called whenever you check or uncheck again.
What you need is a CheckedChangeListener which will be as follows
paidCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
// do somtheing when is checked
paidRow.setVisibility(View.VISIBLE);
}else{
paidRow.setVisibility(View.GONE);
}
}
});

Check Mute Event?

I have a mute switch in my application.
Now I want to check if the switch is checked or unchecked.
How could I do this?
You can check like below code;
Boolean switchState = simpleSwitch.isChecked();
if (switchState==true)
{
Toast.makeText(MainActivity.this,"checked..",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this,"not checked..",Toast.LENGTH_LONG).show();
}
Like the previous answer stated, people normally use the OnCheckedChangedListener
but since Switch extends CompoundButton, it inherits the isChecked() method. Use it anytime to retrieve it's current status
This has been answered like a hundred times :)
The below code checks whether it is checked or not.
Checkbox muteSwitch = mute.getSelectedCheckbox();
if(null != muteSwitch ) {
//not checked
} else {
//Checked
}
hope you are using android.support.v7.widget.SwitchCompat;
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
// Do your stuff
}else{
// not checked
}
}
});
You would use setOnChangeListener to your switch
Eg : switch.setOnChangeListener() etc.
Then inside this, you can check by using isChecked.
eg.
if(isChecked) {
//do something ect...
Hope this helps!

Event of multiple checkboxes using onCheckedChanged

I have a problem with login checkboxes
First, I`m going to make 3 checkboxes and created change listener for each
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private CheckBox main_id_saver;
private CheckBox main_pw_saver;
private CheckBox main_auto_login;
and allocate ID of wigets for each too in onCreate(xml file is skipped)
main_id_saver = (CheckBox) findViewById(R.id.main_id_saver);
main_pw_saver = (CheckBox) findViewById(R.id.main_pw_saver);
main_auto_login = (CheckBox) findViewById(R.id.main_auto_login);
main_id_saver.setOnCheckedChangeListener(this);
main_pw_saver.setOnCheckedChangeListener(this);
main_auto_login.setOnCheckedChangeListener(this);
and I want to set a click event for checkboxes which satisfies below
If ID, PW checkboxes is checked, then auto login checkboxes are checked automatically
cannot check PW checkbox only
If auto login is checked, then ID, PW are checked automatically
If any checkbox is clicked in status of all checkboxes are checked, then all checkboxes are unchecked
I made some tries and below is that
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (main_auto_login.isChecked()){
if(!main_id_saver.isChecked() || !main_pw_saver.isChecked()) {
main_id_saver.setChecked(true);
main_pw_saver.setChecked(true);
return;
} else if (main_id_saver.isChecked()&&main_pw_saver.isChecked()&&main_auto_login.isChecked()){
main_auto_login.setChecked(false);
main_id_saver.setChecked(false);
main_pw_saver.setChecked(false);
return;
}
} else if(main_pw_saver.isChecked()){
if(main_id_saver.isChecked()){
main_auto_login.setChecked(true);
return;
} else if(!main_id_saver.isChecked()){
Toast.makeText(MainActivity.this, "CANNOT REMEMBER PASSWORD BUT FOR ID",Toast.LENGTH_SHORT).show();
main_pw_saver.setChecked(false);
return;
}
}
return;
}
but it doesn`t work as I have thought
what is the best code for this problem?
In my arrogant opinion, making onChangedLinster event separately is the best answer but I cannot make it...
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
if(isChecked)
//Do something
else
//Do something
break;
case R.id.checkBox2:
if(isChecked)
//Do something
else
//Do something
break;
}
}
});

OnclickListener in a checkbox (Android Studio)

here is part of my code which includes the CheckBox(ChckBoxNo):
final CheckBox ChckBoxNo = (CheckBox)promptsView.findViewById(R.id.ChkBoxNo);
ChckBoxNo.setChecked(true);
ChckBoxNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ChckBoxNo.isChecked()) {
ChckBoxNo.setChecked(false);
}
else if (!ChckBoxNo.isChecked())
{
ChckBoxNo.setChecked(true);
}
}
});
At the begining I set true for the isChecked() method on my checkbox , then I implement the onclicklistener on the checkbox.
When I run the app the checkbox is Checked as I defined eralier , but when I click on the checkbox ,It's unchecked and then immidately checked again(I didnt clciked again on the checkbox! )
What should I do in order to fix that,what wrong in my code ?
Thanks!
try this:
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setChecked(checkPasswordExist());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()) {
// your code to checked checkbox
}
} else {
// your code to no checked checkbox
}
}
});
A Checkbox will handle the "checking" process automatically - you do not have to manage this yourself for the standard usage.
Check out this example from the docs. Here, when a click event is caught, they are doing operations based on the isChecked() state.
You're battling the CheckBox. It's standart behavior that checkbox change states, you dont need to do that by yourself.
Remove setChecked true and false and paste something usefull there instead )
You dont need to set the checked state on the same that you have klicked.
You shold handle some other Operations on specific state.

Using a dialog preference with custom layout

I have a custom layout that I am using with a dialog preference, and the layout appears fine in the dialog box but the problem is that I have check boxes in the layout, and checking them doesnt make them behave as expected. Below is the code of my DialogPreferences.java class:
#Override
protected void onDialogClosed(boolean positiveResult) {
LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
View view = inflater.inflate(R.layout.imagechoice, null);
button1 = (CheckBox)view.findViewById(R.id.background1);
button2 = (CheckBox)view.findViewById(R.id.background2);
button3 = (CheckBox)view.findViewById(R.id.background3);
button4 = (CheckBox)view.findViewById(R.id.background4);
button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
selected=1;
button2.setChecked(false);
button3.setChecked(false);
button4.setChecked(false);
}
});
button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
selected=2;
button1.setChecked(false);
button3.setChecked(false);
button4.setChecked(false);
}
});
if(positiveResult){
Log.d("BACKGROUND onDialogClosed", Integer.toString(selected));
persistInt(selected);
}
super.onDialogClosed(positiveResult); //To change body of overridden methods use File | Settings | File Templates.
}
When the app is running and I have the log open, I tried selecting the first or second check and I always see the value 0 printed by the log statement, indicating that the variable selected is never being updated as expected. I have tried to put the check box code in the
public DialogPreferences(Context oContext, AttributeSet attrs)
{...}
method and that led to the same results. In which method of this file is the proper place to put the code needed to do something when the check boxes are checked?
You should override onCreateDialogView and put your onCheckChangeListeners in there. The problem is currently they are only listening when onDialogClosed is called which is after you are trying check them.

Categories

Resources