Entire Switch bar should be a touch state

- add click listener for toggling the Switch and its text
- add Ripple background to the SwitchBar to make a nice Ripple
when there is a click/touch

See bug #15385724 Entire Switch bar should be a touch state

Change-Id: I26ef77ace596029e9c3e1e17b153b54df4a9110e
This commit is contained in:
Fabrice Di Meglio
2014-06-02 15:56:38 -07:00
parent 11a67c14a2
commit e9cb75c636
4 changed files with 42 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
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.
-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:tint="?android:attr/colorControlHighlight">
<item>
<color android:color="@color/switchbar_background_color" />
</item>
</ripple>

View File

@@ -31,7 +31,7 @@
<com.android.settings.widget.SwitchBar android:id="@+id/switch_bar"
android:layout_height="?android:attr/actionBarSize"
android:layout_width="match_parent"
android:background="@color/switch_bar_background_color"
android:background="@drawable/switchbar_background"
android:theme="@android:style/Theme.Quantum" />
<FrameLayout

View File

@@ -59,6 +59,6 @@
<color name="actionbar_background_color">#ff263238</color>
<color name="dashboard_background_color">#ffe1e1e0</color>
<color name="switch_bar_background_color">#ff384248</color>
<color name="switchbar_background_color">#ff384248</color>
<color name="switch_accent_color">#ff7fcac3</color>
</resources>

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.transition.TransitionManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
@@ -31,7 +32,8 @@ import com.android.settings.R;
import java.util.ArrayList;
public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedChangeListener {
public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedChangeListener,
View.OnClickListener {
private ToggleSwitch mSwitch;
private TextView mTextView;
@@ -82,6 +84,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mSwitch.setTrackResource(R.drawable.switch_track);
mSwitch.setThumbResource(R.drawable.switch_inner);
setOnClickListener(this);
// Default is hide
setVisibility(View.GONE);
}
@@ -105,13 +109,23 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
public void onClick(View v) {
final boolean isChecked = !mSwitch.isChecked();
mSwitch.setChecked(isChecked);
}
public void propagateChecked(boolean isChecked) {
final int count = mSwitchChangeListeners.size();
for (int n = 0; n < count; n++) {
mSwitchChangeListeners.get(n).onSwitchChanged(mSwitch,isChecked);
mSwitchChangeListeners.get(n).onSwitchChanged(mSwitch, isChecked);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
propagateChecked(isChecked);
}
public void addOnSwitchChangeListener(OnSwitchChangeListener listener) {
if (mSwitchChangeListeners.contains(listener)) {
throw new IllegalStateException("Cannot add twice the same OnSwitchChangeListener");