CustomSwitchPreference.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Intermodalics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package eu.intermodalics.tango_ros_streamer.android;
18 
19 import android.content.Context;
20 import android.preference.SwitchPreference;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.Switch;
25 
26 /*
27  * This CustomSwitchPreference is a workaround for the SwitchPreference bug in Android 4.4
28  * (version used by the Tango Development Kit device): having multiple SwitchPreference is not
29  * working properly in this Android version, i.e. when switching one preference the other one also
30  * switches.
31  * source:
32  * http://stackoverflow.com/questions/17664259/android-switchpreference-not-working-correctly-in-4-2-2
33  */
34 public class CustomSwitchPreference extends SwitchPreference {
35 
43  public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
44  super(context, attrs, defStyle);
45  }
46 
53  public CustomSwitchPreference(Context context, AttributeSet attrs) {
54  super(context, attrs);
55  }
56 
62  public CustomSwitchPreference(Context context) {
63  super(context, null);
64  }
65 
66  @Override
67  protected void onBindView(View view) {
68  // Clean listener before invoke SwitchPreference.onBindView
69  ViewGroup viewGroup= (ViewGroup)view;
70  clearListenerInViewGroup(viewGroup);
71  super.onBindView(view);
72  }
73 
79  private void clearListenerInViewGroup(ViewGroup viewGroup) {
80  if (null == viewGroup) {
81  return;
82  }
83 
84  int count = viewGroup.getChildCount();
85  for(int n = 0; n < count; ++n) {
86  View childView = viewGroup.getChildAt(n);
87  if(childView instanceof Switch) {
88  final Switch switchView = (Switch) childView;
89  switchView.setOnCheckedChangeListener(null);
90  return;
91  } else if (childView instanceof ViewGroup){
92  ViewGroup childGroup = (ViewGroup)childView;
93  clearListenerInViewGroup(childGroup);
94  }
95  }
96  }
97 
98 }
CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle)


TangoRosStreamer
Author(s):
autogenerated on Mon Jun 10 2019 15:37:54