/* * Copyright (C) 2015 Jorge Ruesga * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ruesga.android.wallpapers.photophase.preferences; import android.annotation.TargetApi; import android.content.Context; import android.preference.CheckBoxPreference; import android.support.annotation.NonNull; import android.support.v7.widget.SwitchCompat; import android.util.AttributeSet; import android.view.View; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import com.ruesga.android.wallpapers.photophase.R; public class SwitchPreference extends CheckBoxPreference { private final OnCheckedChangeListener mListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (!callChangeListener(isChecked)) { buttonView.setChecked(!isChecked); return; } SwitchPreference.this.setChecked(isChecked); } }; public SwitchPreference(Context context) { super(context); init(); } public SwitchPreference(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @TargetApi(21) public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { setWidgetLayoutResource(R.layout.preference_widget_switch); } @Override protected void onBindView(@NonNull View view) { super.onBindView(view); SwitchCompat switchWidget = (SwitchCompat) view.findViewById(android.R.id.checkbox); switchWidget.setOnCheckedChangeListener(mListener); } }