The CompoundButton.OnCheckedChangeListener is using for creating callback to be invoked when the checked state
of a compound button changed. It can be assign to any view class inheriting the CompoundButton class.
But i found a problem when using this interface with view Adapters. The problem is if we set a callback for getting the checked change state to a ToggleButton , we can't understand the callback is fired when the user touches the screen or by changing the checked state by code .
Consider the code Below:
So when the user changed the state of toggleButton by taping in it , the onCheckedChanged will fired.
So we cant understand the callback is fired by the user or by the ToogleButton class.
I have solved this issue by creating a custom callBack option for The ToggleButton. And i am blessed if anyone found this post is helpful :) .
ToggleButtonWithExtendedCallback.java
On the onChangedState callBack the boolean isFromUser indicating the callback is fired by user or by code.
To change the Checked state of toggleButton :
But i found a problem when using this interface with view Adapters. The problem is if we set a callback for getting the checked change state to a ToggleButton , we can't understand the callback is fired when the user touches the screen or by changing the checked state by code .
Consider the code Below:
ToggleButton toogle = (ToggleButton) findViewById(R.id.btnId);
toogle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
}
});
So when the user changed the state of toggleButton by taping in it , the onCheckedChanged will fired.
ToggleButton toogle = (ToggleButton) findViewById(R.id.btnId);It is also possible to do the same by this
toogle.setChecked(true);
So we cant understand the callback is fired by the user or by the ToogleButton class.
I have solved this issue by creating a custom callBack option for The ToggleButton. And i am blessed if anyone found this post is helpful :) .
ToggleButtonWithExtendedCallback.java
package com.raspberry.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ToggleButton;
/*********************************************************************
*
* Toogle button with extended options in OnCkeckedChange callback
* to identify the checked change state is initiated by user , or
* by programmatically.
*
* @author Name: JITHINRAJ.P
* @author Url: https://in.linkedin.com/pub/jithin-raj/97/5b4/14a
* @author Email : jithinrajktd@gmail.com
*********************************************************************/
public class ToggleButtonWithExtendedCallback extends ToggleButton{
private MyOnCkeckedChangeListner myOnCkeckedChangeListner;
private boolean UP_FLAG;
public ToggleButtonWithExtendedCallback(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
UP_FLAG = true;
break;
case MotionEvent.ACTION_UP:
if(UP_FLAG){
super.toggle();
if(myOnCkeckedChangeListner!= null)
myOnCkeckedChangeListner.onChangedState(super.isChecked(), true);
UP_FLAG = false;
}
break;
default:
break;
}
return true;
}
public void setMyOnCkeckedChangeListner(MyOnCkeckedChangeListner listner){
this.myOnCkeckedChangeListner = listner;
}
public void setMyButtonChecked(boolean state){
if(super.isChecked() == state){
return;
}
else
{
super.setChecked(state);
if(myOnCkeckedChangeListner!= null)
myOnCkeckedChangeListner.onChangedState(super.isChecked(), false);
}
}
public boolean getMyButtonState(){
return super.isChecked();
}
public interface MyOnCkeckedChangeListner{
public void onChangedState(boolean state , boolean isFromUser);
}
}
Usage
In layout
Creating callback
ToggleButtonWithExtendedCallback allarmtoogle = (ToggleButtonWithExtendedCallback)view.findViewById(R.id.alarmtoogle);
allarmtoogle.setMyOnCkeckedChangeListner(new MyOnCkeckedChangeListner() {
@Override
public void onChangedState(boolean state, boolean isFromUser) {
}
});
On the onChangedState callBack the boolean isFromUser indicating the callback is fired by user or by code.
To change the Checked state of toggleButton :
allarmtoogle.setMyButtonChecked(boolean);
No comments:
Post a Comment