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

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".

Related

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.

Unable to get a reference for quick settings tile

I can set the state of the qs tile using the onClick() method but I would also like to toggle its state using a switch in my Main Activity. However, I can't change the state of the tile using a switch because getQsTile returns null outside the onClick() method.
public class AwesomeTileService extends TileService{
Tile tile;
public Activity activity;
public AwesomeTileService(Activity activity){
this.activity=activity;
Switch switch1 = (Switch)this.activity.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
tile=getQsTile(); //tile is null
SwitchState(); //SwitchState does not work with onCheckedChanged
Log.v("Switch State=", ""+isChecked);
}
});
}
public AwesomeTileService(){
//zero arg constructor
}
#Override
public void onTileAdded() {
tile=getQsTile();
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
}
#Override
public void onClick() {
tile=getQsTile(); //tile is not null
SwitchState(); //SwitchState method works with onClick
}
public void SwitchState(){
if(tile!=null){
if (tile.getState() == Tile.STATE_INACTIVE) {
tile.setState(Tile.STATE_ACTIVE);
}
else{
tile.setState(Tile.STATE_INACTIVE);
}
tile.updateTile();
}
}
}
I can simply remove the switch from the app but I'm curious to know what the issue is here and how I can overcome it.
If you want to update the tile programmatically from another activity, use sharedPreferences and store boolean value and write code in onStartListening method in tile service class to update tile. (onStartListening is called every time you swipe down the panel).

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

Boolean value not being set upon onClickListener

I have two classes. I am trying to change a boolean value depending on if the user clicks a checkbox. Then, depending on whether that value is true/false (whether the checkbox was checked or not), I want to display specific text in a textview. Here is the MainActivity class:
public class BoiseActivity extends ActionBarActivity {
CheckBox cb;
public boolean isTrue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boise);
cb = (CheckBox) findViewById(R.id.boiseCheckBox);
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isTrue = true;
}
});
public boolean isTrue() {
if (isTrue == true) {
return true;
}
return false;
}
Here is the new Activity that is opened once the user presses the "submit" button:
public class BoiseResults extends BoiseActivity {
TextView TV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boise_results);
TV = (TextView) findViewById(R.id.textView1);
if (isTrue() == true) {
TV.setText("checkbox checked");
}
}
The boolean value is never changed in the BoiseActivity class onCreate-> onClick method. Can anyone see what is wrong here? Thanks
Apart from what has been said on you picking up a book on OOP (come on guys, everybody started somewhere):
The isTrue instance variable of your BoiseActivity will be present in it's child class BoiseResults because it (BoiseResults) inherits isTrue from BoiseActivity, but it's value will not be set and therefore interpreted als false, as Vikram pointed out.
What to do to straighten this out:
use onCheckedChangeListener for CheckBox
pass the boolean ('if the checkbox has been ticked') to your second Activity via the Intent you are starting it with, links on how to do this have been given in comments
ask yourself if BoiseResults really needs to subclass BoiseActivity (I do not think it does, it is just another Activity)
get rid of all the boolean mayhem you created with the isTrue() method returning the value of your isTrue variable
Rename BoiseResults to BoiseResultsActivity for clarity
Your onCreate{...} method for BoiseActivity doesn't seem to be a closed method.
You don't need the isTrue() method as it is really only returning the value of isTrue and isTrue is already a public instance variable. Regardless of that detail, you should look into (Explicit) Intents for passing data between activities: http://www.vogella.com/tutorials/AndroidIntent/article.html
Consider the use of the OnCheckedChangeListener;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// DO SOMETHING HERE WHEN CHECKED/UNCHECKED
}
});
You can then check whether it is checked or not using the checkBox.isChecked() method.
It's also best for you to avoid performing unnecessary checks on a boolean value like;
public boolean isTrue() {
if (isTrue == true) {
return true;
}
return false;
}
The following would achieve the same;
public boolean isTrue(){
return isTrue;
}
With regards to your BoiseResults Activity, take a look at Intents if you are going to be changing from the BoiseActivity Activity to the BoiseResults Activity from your submit and need to pass data to BoiseResults.

Categories

Resources