how to change the checked state of Toggle Button and CheckBox - android

I have to two Class in my App
Setting(for saving the settings)
main Class
I defined the Toggle button and check box in Setting class for On and Off.
now when I call the setting class and from the main class and the change the checked state of Toggle Button and Check Box and the return to main class and if I again call the Setting class the checked state of the Toggle button and check box is not change it come back to it original state please help
My CODE:
yes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox) v).isChecked())
sound.toggle();
}
});
sound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
Log.v("CheckBoxActivity", (isChecked ? "checked" : "not checked"));
}
});
}
public void doClick(View view) {
Log.v("CheckBoxActivity", ((CheckBox) view).isChecked() ? "checked" : "not checked");
}

This probably will be the problem as each time your activity starts,it would take default selection state for the components.
To achieve you goal,you need to store what state you need for a toggle button and checkbox.And then you will have to set the according state of the same each time you load activity,that mean in onCreate() just before you start defining onClick() on those components.
You can use SharedPreference object to store state at a time and use the same in onCreate() of you activity to set stored states of toggle button and checkboxes.

Related

Changes apply on the second tap

I have a RecyclerView and I am trying to make it with multiple selections so I can perform an action to all items selected.
I have to mention that in the fragment that holds one item I have a radio button(radioButton), some text, and a button(joinBtn).
What I want to do is that when I click the item, the radio button will become checked, and the button will become visible and enabled.
My class Group has a private boolean clicked = false by default.
In the ViewHolder I have the method bind :
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(group.isClicked())
radioButton.setChecked(true);
else
radioButton.setChecked(false);
}
And in the onClick method i have the actions I want to perform:
#Override
public void onClick(View view) {
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
My problem is the behaviour I get:
1st tap: radio button gets checked.
2nd tap: joinBtn appears.
3rd tap: radio button gets unchecked.
4th tap: joinBtn disappears.
How can I make the actions from 1st and 2nd tap to happen in one tap? The same for 3rd and 4th?
Try calling notifyItemChanged(int position) on the adapter instance after your onClick() logic. Pass the index of the item as the param.
The UI may not update by itself, but this function should refresh it.
Maintain a flag which will change on group click
boolean isChecked = false ;
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(isChecked)
isChecked = false;
radioButton.setChecked(false);
else
isChecked = true;
radioButton.setChecked(true);
}
and then
#Override
public void onClick(View view) {
if (isChecked) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
Apparently the problem was because of the radio button. The first thing I did was to use a ImageView instead of the radio button. In that ImageView I used (when needed) one of two pictures: selected or unselected.
In the bind method all I needed was: joinBtn.setVisibility(View.GONE); and radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
In the onClick method I used:
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setClickable(false);
radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setClickable(true);
radioBtnImage.setImageResource(R.drawable.radio_btn_selected);
}
This did it for me!

Android Studio programmatically check a Checkbox and call its OnCheckedChange

Can I programmatically check a checkbox and make it call whatever it is coded to do once checked or unchecked?
For example, if I have a checkbox like this
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(getContext(), "You checked the checkbox!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(), "You unchecked the checkbox!", Toast.LENGTH_SHORT).show();
}
}
});
And then would've called
checkBox.setChecked(true);
my checkbox would appear checked but it wouldnt make the toast.
You can do any of the following
1. Create the listener object separate and call it manually when you call setChecked
2. Extract the method for implementation of onCheckedChanged and call it manually on your change.
As the name suggest setOnCheckedChangeListener, it only calls your callback if the checkbox value actually changes.
So if it's already checked (true), and then you call checkbox.setChecked(true), the value hasn't changed, so your callback won't be called.
Try to do checkbox.setChecked(false), and it should be working correctly.
That's my best guess, without seeing the rest of your code / xml.
Android widgets' click or touch events are not simulated. you can change states of of widgets like disabling ,enabling ,checked or unchecked programmatically but todo any task when state changes , you have to change their states manually by touching on that widget.
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getContext(), "You checked the checkbox!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), "You unchecked the checkbox!", Toast.LENGTH_SHORT).show();
}
}
});
Just Cheked it. it will work for me.
if you add checkboxes in LinearLayout (let's call it checkLayout) like this :
val widget = AppCompatCheckBox(checkLayout.context)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
widget.tag = "something you can specify later"
checkLayout.addView(widget)
the code for get the selected checkbox is like this:
checkLayout.children.forEach { cb ->
if(cb is AppCompatCheckBox && cb.isChecked){
// here you have your checkbox and by tag maybe you can do whatever you want
}
}
and for dynamic added RadioButton you can add them in RadioGroup (let's call it radioLayout) and get the selected like :
val rb: AppCompatRadioButton? = radioLayout.findViewById(radioLayout.checkedRadioButtonId)
if(rb.tag == "something"){
//your code here
}

CheckBox OnClick Toggled Before OnClick Handler?

So I'm working on a list of check boxes. I'm using the OnClickListener to catch the touch event. I've also tried the OnTouch listener and the OnCheckChanged listener. The issue I've come across is the fact that the check box IsChecked value is set to true before it reaches any of these event listeners. So if I were to do something like this:
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.isChecked())
checkBox.setChecked(false);
}
});
It will always hit this if statement and immediately set the checkbox to unchecked because it's setting it as ischecked true before it reaches OnClickListener. The easiest way around this I've found is:
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
}
});
But this is what I would call hackish. The other option I see being to create a new checkbox class and override the preformClick method.
#Override
public boolean performClick() {
toggle();
final boolean handled = super.performClick();
if (!handled) {
// View only makes a sound effect if the onClickListener was
// called, so we'll need to make one here instead.
playSoundEffect(SoundEffectConstants.CLICK);
}
return handled;
}
But that seems like a lot more work than should need to go in to accomplishing this task.
So, essentially my question is: Is there a method where I can override and intercept the setting of the checkbox before it is actually changed?
The OnClickListener of the CheckBox always gets called after it changed its checked state (as you can see in the performClick() method you copied, the toggle() method changes the checked state, before calling super's performClick(), which will call the OnClickListener).
If you don't want to always change the checked state of the CheckBox, then you can override performClick() and leave out the toggle() method (or only call it if some condition is true).
But if you want to change the state every time a click happens, then the easiest way is to use the OnClickListener, and just negate your conditions (e.g. if you want to do something when the checkbox was empty, then you check if the checkbox's new state is not empty).

Android: ToggleButton Listener

I have this code here
ToggleButton toggleAlarm = (ToggleButton) d.findViewById(R.id.toggle_alarm);
toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
I have to keep track if its ON or OFF. But when I logged something to logcat where it is on or off, it won't do a thing. I don't know, what is wrong, because on my other code same, syntax but it works I just copy paste it and change only the ToggleButton variable.
EDIT
I have observed, with the help of cdr. Powell of course, that when you put this code block, the one that I have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that I don't understand is that, there is also a outer listener in my code, it is like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.
try this, May be the problem is with import
toggleAlarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
Try toggleAlarm.isChecked() too see if the button is checked or not.
In case toggleAlarm.isChecked() does not work for you you could always.
boolean _isChecked = false;
((ToggleButton) d.findViewById(R.id.toggle_alarm)).setOnClickListener(new OnOnClickListener() {
public void onClick(View arg0) {
_isChecked = !isChecked;
if(_isChecked()) {
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else {
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
So i have observed, with the help of cdr. powell of course, that when u put this code block, the one that i have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that i don't understand is that, there is also a outer listener in my code, its like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.

How to receive a event on android checkbox check change?

What would be the correct way of receiving and sending an event when a check box gets enabled or disabled?
In C# I could just easily double click and all the code would be done for me. But in android it appears to be a bit more obscure. I thought of using the touch event handlers but then if the user has a keyboard it won't detect the change since it's not touch. I figure android should have a native event for check box state change.
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
Since CheckBox (eventually) extends View, you can use a standard OnClickListener to detect when the CheckBox is actually tapped by the user (as opposed to the ListView updates):
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( ((CheckBox)v).isChecked() ) {
// perform logic
}
}
});
In Kotlin:
checkBoxView.setOnCheckedChangeListener { _, isChecked ->
print("checked: $isChecked")
}
Try this
CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);
checkbox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (checkbox.isChecked())
{
//Perform action when you touch on checkbox and it change to selected state
}
else
{
//Perform action when you touch on checkbox and it change to unselected state
}
}
});

Categories

Resources