From 85852786bc1bd5a845f37c33bf48aca96a35eefb Mon Sep 17 00:00:00 2001 From: Stanley Wang Date: Thu, 4 Feb 2021 00:08:00 +0800 Subject: [PATCH] Update MainSwitch widget. - Fix the MainSwitchBar overlapping issue. - Add setTitle method to support string resource id. - Add setChecked method to fix the state of switch not sync issue. - Get the resource id of title from xml. Fix: 178425979 Test: Run robotest and apply the widget in Settings and see the ui Change-Id: I332b017c6a6e32e5ea3929d4b3c7e81104445bcc --- .../res/layout/main_switch_bar.xml | 1 + .../settingslib/widget/MainSwitchBar.java | 15 ++++++++++- .../widget/MainSwitchPreference.java | 25 +++++++++++-------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml index 52779bcabf000..85c01c5732cab 100644 --- a/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml +++ b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml @@ -19,6 +19,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="match_parent" + android:background="?android:attr/colorBackground" android:orientation="vertical"> mSwitchChangeListeners = new ArrayList<>(); private MainSwitchBar mMainSwitchBar; - private String mTitle; + private CharSequence mTitle; private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin; @@ -81,24 +80,28 @@ public class MainSwitchPreference extends TwoStatePreference { setLayoutResource(R.layout.main_switch_layout); if (attrs != null) { - TypedArray a = context.obtainStyledAttributes(attrs, + final TypedArray a = context.obtainStyledAttributes(attrs, androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/, 0 /*defStyleRes*/); final CharSequence title = TypedArrayUtils.getText(a, androidx.preference.R.styleable.Preference_title, androidx.preference.R.styleable.Preference_android_title); - if (!TextUtils.isEmpty(title)) { - setTitle(title.toString()); - } + setTitle(title); a.recycle(); } } - /** - * Set the preference title text - */ - public void setTitle(String text) { - mTitle = text; + @Override + public void setChecked(boolean checked) { + super.setChecked(checked); + if (mMainSwitchBar != null) { + mMainSwitchBar.setChecked(checked); + } + } + + @Override + public void setTitle(CharSequence title) { + mTitle = title; if (mMainSwitchBar != null) { mMainSwitchBar.setTitle(mTitle); }