Merge "Update Spinner and Progressbar style to adopt Material Next style."

This commit is contained in:
Edgar Wang
2022-02-17 14:10:39 +00:00
committed by Android (Google) Code Review
23 changed files with 184 additions and 198 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The Android Open Source Project
Copyright (C) 2022 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.
@@ -18,8 +18,7 @@
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/SettingsSpinnerTitleBar"
style="@style/SettingsSpinnerDropdown"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/settings_spinner_dropdown_background"/>
android:layout_height="wrap_content"/>

View File

@@ -22,7 +22,7 @@
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
<com.android.settingslib.widget.settingsspinner.SettingsSpinner
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View File

@@ -1,19 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2021 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.
-->
<resources>
<color name="ripple_color">@*android:color/material_grey_900</color>
</resources>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2021 The Android Open Source Project
<!-- Copyright (C) 2022 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.
@@ -15,5 +15,6 @@
-->
<resources>
<color name="ripple_color">?android:attr/colorControlHighlight</color>
<color name="settingslib_spinner_title_color">@android:color/system_neutral1_900</color>
<color name="settingslib_spinner_dropdown_color">@android:color/system_neutral2_700</color>
</resources>

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2022 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.
-->
<resources>
<style name="SettingsSpinnerTitleBar">
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:textColor">@color/settingslib_spinner_title_color</item>
<item name="android:maxLines">1</item>
<item name="android:ellipsize">marquee</item>
<item name="android:minHeight">@dimen/spinner_height</item>
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">36dp</item>
<item name="android:paddingTop">@dimen/spinner_padding_top_or_bottom</item>
<item name="android:paddingBottom">@dimen/spinner_padding_top_or_bottom</item>
</style>
<style name="SettingsSpinnerDropdown">
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:textColor">@color/settingslib_spinner_dropdown_color</item>
<item name="android:maxLines">1</item>
<item name="android:ellipsize">marquee</item>
<item name="android:minHeight">@dimen/spinner_height</item>
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">36dp</item>
<item name="android:paddingTop">@dimen/spinner_padding_top_or_bottom</item>
<item name="android:paddingBottom">@dimen/spinner_padding_top_or_bottom</item>
</style>
</resources>

View File

@@ -18,10 +18,8 @@
<resources>
<style name="SettingsSpinnerTitleBar">
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:maxLines">1</item>
<item name="android:ellipsize">marquee</item>
<item name="android:minHeight">@dimen/spinner_height</item>
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">36dp</item>
<item name="android:paddingTop">@dimen/spinner_padding_top_or_bottom</item>

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2022 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.
@@ -14,18 +14,18 @@
* limitations under the License.
*/
package com.android.settingslib.widget.settingsspinner;
package com.android.settingslib.widget;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.android.settingslib.widget.R;
/**
* An ArrayAdapter which was used by {@link SettingsSpinner} with settings style.
* An ArrayAdapter which was used by Spinner with settings style.
* @param <T> the data type to be loaded.
*/
public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
@@ -43,7 +43,7 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
public SettingsSpinnerAdapter(Context context) {
super(context, DEFAULT_RESOURCE);
setDropDownViewResource(DFAULT_DROPDOWN_RESOURCE);
setDropDownViewResource(getDropdownResource());
mDefaultInflater = LayoutInflater.from(context);
}
@@ -59,6 +59,11 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
* drop down view.
*/
public View getDefaultDropDownView(int position, View convertView, ViewGroup parent) {
return mDefaultInflater.inflate(DFAULT_DROPDOWN_RESOURCE, parent, false /* attachToRoot */);
return mDefaultInflater.inflate(getDropdownResource(), parent, false /* attachToRoot */);
}
private int getDropdownResource() {
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
? DFAULT_DROPDOWN_RESOURCE : android.R.layout.simple_spinner_dropdown_item;
}
}

View File

@@ -20,16 +20,14 @@ import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceViewHolder;
import com.android.settingslib.widget.settingsspinner.SettingsSpinner;
import com.android.settingslib.widget.settingsspinner.SettingsSpinnerAdapter;
/**
* This preference uses SettingsSpinner & SettingsSpinnerAdapter which provide default layouts for
* This preference uses Spinner & SettingsSpinnerAdapter which provide default layouts for
* both view and drop down view of the Spinner.
*/
public class SettingsSpinnerPreference extends Preference implements OnPreferenceClickListener {
@@ -113,7 +111,7 @@ public class SettingsSpinnerPreference extends Preference implements OnPreferenc
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final SettingsSpinner spinner = (SettingsSpinner) holder.findViewById(R.id.spinner);
final Spinner spinner = (Spinner) holder.findViewById(R.id.spinner);
spinner.setAdapter(mAdapter);
spinner.setSelection(mPosition);
spinner.setOnItemSelectedListener(mOnSelectedListener);

View File

@@ -1,135 +0,0 @@
/*
* Copyright (C) 2018 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.
*/
package com.android.settingslib.widget.settingsspinner;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Spinner;
import androidx.annotation.RequiresApi;
import com.android.settingslib.widget.R;
/**
* A {@link Spinner} with settings style.
*
* The items in the SettingsSpinner come from the {@link SettingsSpinnerAdapter} associated with
* this view.
*/
public class SettingsSpinner extends Spinner {
/**
* Constructs a new SettingsSpinner with the given context's theme.
* And it also set a background resource with settings style.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public SettingsSpinner(Context context) {
super(context);
setBackgroundResource(R.drawable.settings_spinner_background);
}
/**
* Constructs a new SettingsSpinner with the given context's theme and the supplied
* mode of displaying choices. <code>mode</code> may be one of
* {@link Spinner#MODE_DIALOG} or {@link Spinner#MODE_DROPDOWN}.
* And it also set a background resource with settings style.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param mode Constant describing how the user will select choices from
* the spinner.
*
* @see Spinner#MODE_DIALOG
* @see Spinner#MODE_DROPDOWN
*/
public SettingsSpinner(Context context, int mode) {
super(context, mode);
setBackgroundResource(R.drawable.settings_spinner_background);
}
/**
* Constructs a new SettingsSpinner with the given context's theme and the supplied
* attribute set.
* And it also set a background resource with settings style.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
*/
public SettingsSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.settings_spinner_background);
}
/**
* Constructs a new SettingsSpinner with the given context's theme, the supplied
* attribute set, and default style attribute.
* And it also set a background resource with settings style.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default
* values for the view. Can be 0 to not look for
* defaults.
*/
public SettingsSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setBackgroundResource(R.drawable.settings_spinner_background);
}
/**
* Constructs a new SettingsSpinner with the given context's theme, the supplied
* attribute set, and default styles. <code>mode</code> may be one of
* {@link Spinner#MODE_DIALOG} or {@link Spinner#MODE_DROPDOWN} and determines how the
* user will select choices from the spinner.
* And it also set a background resource with settings style.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default
* values for the view. Can be 0 to not look for
* defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme.
* Can be 0 to not look for defaults.
* @param mode Constant describing how the user will select choices from
* the spinner.
*
* @see Spinner#MODE_DIALOG
* @see Spinner#MODE_DROPDOWN
*/
@RequiresApi(Build.VERSION_CODES.M)
public SettingsSpinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes,
int mode) {
super(context, attrs, defStyleAttr, defStyleRes, mode, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setDropDownVerticalOffset(getMeasuredHeight() - (int) getContext().getResources()
.getDimension(R.dimen.spinner_padding_top_or_bottom));
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The Android Open Source Project
Copyright (C) 2022 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.

View File

@@ -27,11 +27,12 @@
<item
android:id="@android:id/progress">
<clip>
<scale android:scaleWidth="100%" android:useIntrinsicSizeAsMinimum="true">
<shape>
<corners android:radius="8dp" />
<solid android:color="?android:attr/textColorPrimary" />
<size android:width="8dp"/>
</shape>
</clip>
</scale>
</item>
</layer-list>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2022 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:color="@color/settingslib_ripple_color">
<item android:id="@android:id/background">
<layer-list android:paddingMode="stack">
<item
android:top="8dp"
android:bottom="8dp">
<shape>
<corners android:radius="28dp"/>
<solid android:color="@android:color/system_accent1_100"/>
<size android:height="@dimen/settingslib_spinner_height"/>
</shape>
</item>
<item
android:gravity="center|end"
android:width="18dp"
android:height="18dp"
android:end="12dp"
android:drawable="@drawable/settingslib_arrow_drop_down"/>
</layer-list>
</item>
</ripple>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The Android Open Source Project
Copyright (C) 2022 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.
@@ -17,12 +17,12 @@
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:priv-android="http://schemas.android.com/apk/prv/res/android"
android:color="@color/ripple_color">
android:color="@color/settingslib_ripple_color">
<item android:id="@android:id/background">
<shape>
<solid android:color="?priv-android:attr/colorAccentSecondary"/>
<corners android:radius="10dp"/>
<solid android:color="@android:color/system_accent2_100"/>
</shape>
</item>
</ripple>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2022 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="24"
android:viewportHeight="24"
android:width="24dp"
android:height="24dp">
<path
android:pathData="M7 10l5 5 5 -5z"
android:fillColor="?android:attr/textColorPrimary"/>
</vector>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The Android Open Source Project
Copyright (C) 2022 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.
@@ -16,9 +16,8 @@
-->
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:priv-android="http://schemas.android.com/apk/prv/res/android"
android:color="@color/ripple_color">
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/settingslib_ripple_color">
<item android:id="@android:id/background">
<layer-list android:paddingMode="stack">
@@ -27,18 +26,24 @@
android:bottom="8dp">
<shape>
<corners android:radius="28dp"/>
<solid android:color="?priv-android:attr/colorAccentPrimary"/>
<size android:height="@dimen/spinner_height"/>
<corners
android:radius="20dp"/>
<solid
android:color="?android:attr/colorPrimary"/>
<stroke
android:color="#1f000000"
android:width="1dp"/>
<size
android:height="32dp"/>
</shape>
</item>
<item
android:gravity="center|end"
android:width="18dp"
android:height="18dp"
android:end="8dp"
android:drawable="@drawable/arrow_drop_down"/>
android:width="24dp"
android:height="24dp"
android:end="4dp"
android:drawable="@drawable/settingslib_arrow_drop_down"/>
</layer-list>
</item>
</ripple>

View File

@@ -47,4 +47,6 @@
<color name="settingslib_text_color_secondary_device_default">@android:color/system_neutral2_200</color>
<color name="settingslib_text_color_preference_category_title">@android:color/system_accent1_100</color>
<color name="settingslib_ripple_color">@color/settingslib_material_grey_900</color>
</resources>

View File

@@ -69,4 +69,7 @@
<color name="settingslib_text_color_secondary_device_default">@android:color/system_neutral2_700</color>
<color name="settingslib_text_color_preference_category_title">@android:color/system_accent1_600</color>
<color name="settingslib_ripple_color">?android:attr/colorControlHighlight</color>
<color name="settingslib_material_grey_900">#ff212121</color>
</resources>

View File

@@ -25,4 +25,6 @@
<dimen name="settingslib_listPreferredItemPaddingStart">24dp</dimen>
<!-- Right padding of the preference -->
<dimen name="settingslib_listPreferredItemPaddingEnd">24dp</dimen>
<dimen name="settingslib_spinner_height">36dp</dimen>
</resources>

View File

@@ -45,4 +45,13 @@
<item name="android:progressDrawable">@drawable/settingslib_progress_horizontal</item>
<item name="android:scaleY">0.5</item>
</style>
<style name="Spinner.SettingsLib"
parent="android:style/Widget.Material.Spinner">
<item name="android:background">@drawable/settingslib_spinner_background</item>
<item name="android:popupBackground">@drawable/settingslib_spinner_dropdown_background</item>
<item name="android:dropDownVerticalOffset">48dp</item>
<item name="android:layout_marginTop">16dp</item>
<item name="android:layout_marginBottom">8dp</item>
</style>
</resources>

View File

@@ -26,6 +26,7 @@
<item name="preferenceTheme">@style/PreferenceTheme.SettingsLib</item>
<item name="android:switchStyle">@style/Switch.SettingsLib</item>
<item name="android:progressBarStyleHorizontal">@style/HorizontalProgressBar.SettingsLib</item>
<item name="android:spinnerStyle">@style/Spinner.SettingsLib</item>
</style>
<!-- Using in SubSettings page including injected settings page -->

View File

@@ -26,4 +26,10 @@
<style name="TextAppearance.CategoryTitle.SettingsLib"
parent="@android:style/TextAppearance.DeviceDefault.Medium">
</style>
<style name="Spinner.SettingsLib"
parent="android:style/Widget.Material.Spinner">
<item name="android:background">@drawable/settingslib_spinner_background</item>
<item name="android:dropDownVerticalOffset">48dp</item>
</style>
</resources>

View File

@@ -19,6 +19,7 @@
<!-- Only using in Settings application -->
<style name="Theme.SettingsBase" parent="@android:style/Theme.DeviceDefault.Settings">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
<item name="android:spinnerStyle">@style/Spinner.SettingsLib</item>
</style>
<!-- Using in SubSettings page including injected settings page -->

View File

@@ -22,14 +22,12 @@ import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import androidx.preference.PreferenceViewHolder;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.runner.AndroidJUnit4;
import com.android.settingslib.widget.settingsspinner.SettingsSpinner;
import com.android.settingslib.widget.settingsspinner.SettingsSpinnerAdapter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,7 +40,7 @@ public class SettingsSpinnerPreferenceTest {
private Context mContext;
private PreferenceViewHolder mViewHolder;
private SettingsSpinner mSpinner;
private Spinner mSpinner;
private SettingsSpinnerPreference mSpinnerPreference;
@Before
@@ -53,7 +51,7 @@ public class SettingsSpinnerPreferenceTest {
final View rootView = inflater.inflate(mSpinnerPreference.getLayoutResource(),
new LinearLayout(mContext), false /* attachToRoot */);
mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
mSpinner = (SettingsSpinner) mViewHolder.findViewById(R.id.spinner);
mSpinner = (Spinner) mViewHolder.findViewById(R.id.spinner);
}
@Test