How to inspect before checkbox state changed - android

I want to inspect something before CheckBox's state changed. If user has logged in, change the state and post data to server. Otherwise, keep the state and show a log-in activity.
OnCheckedChangeListener does not fit. Is there anything like PreferenceChangeListener (return false to keep stateļ¼‰ in CheckBox.
Or other methods.
Thank you!
Update:
OnClickListener work for me.
#Override
public void onClick(View view) {
boolean isChecked = checkBox.isChecked();
checkBox.setChecked(!isChecked);
if (loggedIn) {
checkBox.setChecked(isChecked);
// push data to server
} else {
// show log-in activity
}
}

If I'm not misunderstood your question, you can still achieve it by using OnCheckedChangeListener:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isLogin) {
buttonView.setChecked(true);
Toast.makeText(this, "Sending data", Toast.LENGTH_SHORT).show();
} else {
buttonView.setChecked(false);
Toast.makeText(this, "Show login activity", Toast.LENGTH_SHORT)
.show();
}
}

You can do something like this:
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
or if you just want to see if the checkbox has been checked or not, then you can use this
CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
checkBox.setChecked(!checkBox.isChecked());

#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// awesome code you may use
// if (isChecked)
// (buttonView.getId() == R.id.check1 ? check2 : check1)
// .setChecked(false);
//Or use the code below
if (isChecked) {
switch (buttonView.getId()) {
case R.id.check1:
check2.setChecked(false);
Toast.makeText(getApplicationContext(), "Check1 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
case R.id.check2:
check1.setChecked(false);
Toast.makeText(getApplicationContext(), "Check2 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}

Related

switch button works on the second click - Android

I added a switch button to my app and created a setOnCheckChangeListener the will pop up toast message..
But it works only after the second click.
Any ideas?
MainActivity:
public void checkSwitch (View view)
{
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
beeps = true;
Toast.makeText(MainActivity.this,"beeps is True",Toast.LENGTH_LONG).show();
}else
{
beeps = false;
Toast.makeText(MainActivity.this,"beeps is False",Toast.LENGTH_LONG).show();
}
}
});
}
I think in xml file which contains addBeeps switch you are using this code.
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="checkSwitch"/>
Root cause: For the first time when you check the switch, a listener will be set on it and do nothing, the second time when you check the switch again the listener is triggered and the Toast showed.
Solution: Here is a solution for you.
Remove onClick from the switch in xml file
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Delete or comment-out checkSwitch method from MainActivity and move set listener block code to onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBeeps = findViewById(R.id.addBeeps);
// TODO: Put set listener block code here
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
beeps = true;
Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
} else {
beeps = false;
Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
}
}
});
}
// TODO: Delete or comment-out this method
// public void checkSwitch(View view) {
// addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// #Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if (isChecked) {
// beeps = true;
// Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
// } else {
// beeps = false;
// Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
// }
// }
// });
// }

How to use setChecked method in the following code

I have this code
Switch serviceSwitch;
serviceSwitch =(Switch) this.findViewById(R.id.switchservice);
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
arrayChecking();
serviceSwitch.setChecked(false);
}
}
}
public void arrayChecking()
{
if(tbPreferenceArray.length>0)
{
Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
}
else
{
Intent i = new Intent(MainActivity.this, TimerService.class);
MainActivity.this.startService(i);
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
}
}
From the code, when the user turned on the switch the method call for arrayChecking() will execute and also there is the code to reset the switch to (false), but this code is not working, serviceSwitch.setChecked (false); is not working. Can anybody help me to solve this simple issue?? Sorry for the language
Modify your arrayChecking method a little bit:
public void arrayChecking(){
if(tbPreferenceArray.length>0){
Toast.makeText(getApplicationContext(), "Please Set the Time and Day", Toast.LENGTH_LONG).show();
//Add this to set the Switch checked to false
serviceSwitch.setChecked(false);
}
else{
Intent i = new Intent(MainActivity.this, TimerService.class);
MainActivity.this.startService(i);
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
}
}
And remove the serviceSwitch.setChecked(false); from your onCheckedChanged method:
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
arrayChecking();
}
}
}

Check if Any Checkbox is Checked

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!

How to verify that a checkbox is just about to be unchecked?

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

Android How do I correctly get the value from a Switch?

I'm creating a Android application which uses a Switch.
I'm trying to listen for changes and get the value when changed.
I have two questions when using switches:
What action listener do I use?
How do I get the the switch value?
Switch s = (Switch) findViewById(R.id.SwitchID);
if (s != null) {
s.setOnCheckedChangeListener(this);
}
/* ... */
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "The Switch is " + (isChecked ? "on" : "off"),
Toast.LENGTH_SHORT).show();
if(isChecked) {
//do stuff when Switch is ON
} else {
//do stuff when Switch if OFF
}
}
Hint: isChecked is the new switch value [true or false] not the old one.
Since it extends from CompoundButton (docs), you can use setOnCheckedChangeListener() to listen for changes; use isChecked() to get the current state of the button.
Switch switch = (Switch) findViewById(R.id.Switch2);
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
...switch on..
} else {
...switch off..
}
}
});
i hope this will solve your problem
I added this in kotlin
switchImage.setOnCheckedChangeListener { compoundButton: CompoundButton, b: Boolean ->
if (b) // Do something
else // Do something
}
Kotlin but in More readable Java Style
videoLoopSwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(switch: CompoundButton?, isChecked: Boolean) {
videoPlayer?.apply {
setLooping(isChecked)
}
}
})
Try This one
#OnClick({R.id.sw_auto_reload})
void onChangedAutoReload(){
Switch s = (Switch) findViewById(R.id.sw_auto_reload);
if (s != null) {
s.setOnCheckedChangeListener(this);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
paymentSettingsPresenter.onAutoReload(b);
}
});
}
}

Categories

Resources