Check Mute Event? - java

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!

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);
}
}
});

If statement for two checkboxes are checked problem

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.

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.

What is the difference in true and false in this code?

What is the difference between true and false in this code?
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
int cbcheck = 1;
} else {
int cbcheck = 0;
}
}
#Override
public void onClick(View v) {
;
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.registerbut:
onCheckedChanged(cbagree, false);
if (cbcheck == 1) {
Intent intent = new Intent(this,
RegisterScreen2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} else if (cbcheck == 0){
}
break;
}
}
The part where it says onCheckedChanged(cbagree, true); what would happen if I passed false instead?
I wish to make it so if the box is checked the intent will run, otherwise it'll state for you to check the box. I have tried saving the checkboxing and then loading it using savepreferences and loadpreferences but I find that is too much hassle. Is there any other way to accomplish this?
This is in android btw.
As it's written, there's no difference since your function onCheckedChanged() literally does nothing: In two local blocks local variables get declared which immediately go out of scope, and there is no net effect of this function at all.
Perhaps you mean to modify some private class member instead?
Suggestion: here's an idea on passing the state change through a private member:
class Thing
{
private int cbcheck;
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
cbcheck = isChecked ? 1 : 0;
}
/* ... */
}
This code looks buggy, onclick checks an instance (or class) variable cbcheck,
if (cbcheck == 1) {
after calling onCheckedChanged but the cbcheck set in onCheckedChanged is a local variable so the change has no effect.
Perhaps onCheckedChanged should be
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cbcheck = 1;
} else {
cbcheck = 0;
}
}
A much simple way to do this would be to hold a reference to the CompoundButton you are using and call CompoundButton.isChecked in your onClick method.
You shouldn't be calling onCheckChanged yourself and should be registering a listener using CompoundButton.setOnCheckedChangeListener instead (in this case probably cbagree.setOnCheckedChangeListener(this)). Now when the user 'checks' the button, isChecked is true and when the user 'unchecks' the button, isChecked is false.
However, in this case you don't even need to register a listener:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.registerbut:
if (cbagree.isChecked()) {
Intent intent = new Intent(this,
RegisterScreen2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
break;
}
}
Not my field though true in this case means "boolean isChecked".

Categories

Resources