CustomSwitchPreference.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Intermodalics All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package eu.intermodalics.tango_ros_streamer.android;
00018 
00019 import android.content.Context;
00020 import android.preference.SwitchPreference;
00021 import android.util.AttributeSet;
00022 import android.view.View;
00023 import android.view.ViewGroup;
00024 import android.widget.Switch;
00025 
00026 /*
00027  * This CustomSwitchPreference is a workaround for the SwitchPreference bug in Android 4.4
00028  * (version used by the Tango Development Kit device): having multiple SwitchPreference is not
00029  * working properly in this Android version, i.e. when switching one preference the other one also
00030  * switches.
00031  * source:
00032  * http://stackoverflow.com/questions/17664259/android-switchpreference-not-working-correctly-in-4-2-2
00033  */
00034 public class CustomSwitchPreference extends SwitchPreference {
00035 
00043     public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
00044         super(context, attrs, defStyle);
00045     }
00046 
00053     public CustomSwitchPreference(Context context, AttributeSet attrs) {
00054         super(context, attrs);
00055     }
00056 
00062     public CustomSwitchPreference(Context context) {
00063         super(context, null);
00064     }
00065 
00066     @Override
00067     protected void onBindView(View view) {
00068         // Clean listener before invoke SwitchPreference.onBindView
00069         ViewGroup viewGroup= (ViewGroup)view;
00070         clearListenerInViewGroup(viewGroup);
00071         super.onBindView(view);
00072     }
00073 
00079     private void clearListenerInViewGroup(ViewGroup viewGroup) {
00080         if (null == viewGroup) {
00081             return;
00082         }
00083 
00084         int count = viewGroup.getChildCount();
00085         for(int n = 0; n < count; ++n) {
00086             View childView = viewGroup.getChildAt(n);
00087             if(childView instanceof Switch) {
00088                 final Switch switchView = (Switch) childView;
00089                 switchView.setOnCheckedChangeListener(null);
00090                 return;
00091             } else if (childView instanceof ViewGroup){
00092                 ViewGroup childGroup = (ViewGroup)childView;
00093                 clearListenerInViewGroup(childGroup);
00094             }
00095         }
00096     }
00097 
00098 }


TangoRosStreamer
Author(s):
autogenerated on Thu Jun 6 2019 19:49:57