Skip to content

Instantly share code, notes, and snippets.

@almozavr
Last active November 28, 2022 15:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almozavr/0c64ccd1f4afd92522b1 to your computer and use it in GitHub Desktop.
Save almozavr/0c64ccd1f4afd92522b1 to your computer and use it in GitHub Desktop.
FixedSwitchPreference
public class FixedSwitchPreference extends SwitchPreference {
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
* @param defStyle Theme attribute defining the default style options
*/
public FixedSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
*/
public FixedSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Construct a new SwitchPreference with default style options.
*
* @param context The Context that will style this preference
*/
public FixedSwitchPreference(Context context) {
super(context, null);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FixedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
private boolean value;
@InjectView(android.R.id.switchInputMethod)
CompoundButton widget;
@Override
protected void onBindView(View view) {
ButterKnife.inject(this, view);
if (widget == null) return; // no custom widget provided
// Clean listener before invoke SwitchPreference.onBindView
ViewGroup viewGroup = (ViewGroup) view;
clearListenerInViewGroup(viewGroup);
super.onBindView(view);
// Set initial value and main check-listener
widget.setChecked(value);
widget.setOnCheckedChangeListener((buttonView, isChecked) -> {
setChecked(isChecked);
}
);
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
value = checked;
}
/**
* Clear listener in Switch for specify ViewGroup.
*
* @param viewGroup The ViewGroup that will need to clear the listener.
*/
private void clearListenerInViewGroup(ViewGroup viewGroup) {
if (null == viewGroup) {
return;
}
int count = viewGroup.getChildCount();
for (int n = 0; n < count; ++n) {
View childView = viewGroup.getChildAt(n);
if (childView instanceof CompoundButton) {
final CompoundButton switchView = (CompoundButton) childView;
switchView.setOnCheckedChangeListener(null);
return;
} else if (childView instanceof ViewGroup) {
ViewGroup childGroup = (ViewGroup) childView;
clearListenerInViewGroup(childGroup);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
android:id="@android:id/switchInputMethod"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="true"
android:focusable="false"
/>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.threewaytalk.android.ui.widget.FixedSwitchPreference
android:key="switch_key"
android:title="Try me"
android:summary="I'm working"
android:defaultValue="true"
android:widgetLayout="@layout/preference_switch" />
</PreferenceScreen>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment