diff options
17 files changed, 494 insertions, 41 deletions
diff --git a/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_background.xml b/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_background.xml new file mode 100644 index 000000000000..f29f3ae79fa6 --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_background.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. + --> +<selector xmlns:android="http://schemas.android.com/apk/res/android"> + + <!-- Highlight the selected item --> + <item android:state_activated="true" android:drawable="@drawable/settings_expressive_spinner_dropdown_item_selected"/> +</selector> diff --git a/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_item_selected.xml b/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_item_selected.xml new file mode 100644 index 000000000000..5da3f7172582 --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/drawable-v36/settings_expressive_spinner_dropdown_item_selected.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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="?android:colorControlHighlight"> + <item> + <shape android:shape="rectangle"> + <solid + android:color="@color/settingslib_materialColorPrimaryContainer" /> + <corners + android:radius="@dimen/settingslib_expressive_radius_large2" /> + </shape> + </item> +</ripple> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v33/settings_spinner_view.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v33/settings_spinner_view.xml index 1d0c9b941881..3c379bf0162d 100644 --- a/packages/SettingsLib/SettingsSpinner/res/layout-v33/settings_spinner_view.xml +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v33/settings_spinner_view.xml @@ -18,6 +18,8 @@ <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" + android:layout_centerVertical="true" + android:gravity="center_vertical" style="@style/SettingsSpinnerTitleBar" android:layout_width="wrap_content" android:layout_height="wrap_content"/> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_preference_full.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_preference_full.xml new file mode 100644 index 000000000000..6d1057c8780b --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_preference_full.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. +--> +<RelativeLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:minHeight="?android:attr/listPreferredItemHeight" + android:paddingStart="?android:attr/listPreferredItemPaddingStart" + android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:filterTouchesWhenObscured="true"> + <Spinner + android:id="@+id/spinner" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_centerVertical="true"/> +</RelativeLayout> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_full.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_full.xml new file mode 100644 index 000000000000..d3832f786ccb --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_full.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. +--> +<TextView + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@android:id/text1" + android:layout_centerVertical="true" + android:gravity="center_vertical" + style="@style/SettingsSpinnerTitleBar.Expressive.Large" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:filterTouchesWhenObscured="true"/> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_large.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_large.xml new file mode 100644 index 000000000000..3e7f0fa7ca4f --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressive_spinner_view_large.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. +--> +<TextView + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@android:id/text1" + android:layout_centerVertical="true" + android:gravity="center_vertical" + style="@style/SettingsSpinnerTitleBar.Expressive.Large" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:filterTouchesWhenObscured="true"/> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_dropdown_view.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_dropdown_view.xml new file mode 100644 index 000000000000..acf2a0dd5858 --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_dropdown_view.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + style="@style/SettingsSpinnerDropdown.Expressive"> + <ImageView + android:id="@android:id/icon" + android:layout_width="@dimen/settingslib_expressive_space_small3" + android:layout_height="@dimen/settingslib_expressive_space_small3" + android:importantForAccessibility="no" + android:src="@drawable/settingslib_expressive_icon_check" + android:tint="@color/settingslib_spinner_dropdown_color" + android:layout_gravity="center_vertical" + android:layout_marginEnd="@dimen/settingslib_expressive_space_extrasmall4" + android:scaleType="centerInside"/> + + <TextView + android:id="@android:id/text1" + style="@style/SettingsSpinnerDropdownText" + android:gravity="center_vertical" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:filterTouchesWhenObscured="true"/> +</LinearLayout> diff --git a/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_view.xml b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_view.xml new file mode 100644 index 000000000000..e300099ee298 --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout-v36/settings_expressvie_spinner_view.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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. +--> +<TextView + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@android:id/text1" + android:layout_centerVertical="true" + android:gravity="center_vertical" + style="@style/SettingsSpinnerTitleBar.Expressive" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:filterTouchesWhenObscured="true"/> diff --git a/packages/SettingsLib/SettingsSpinner/res/values-v33/styles.xml b/packages/SettingsLib/SettingsSpinner/res/values-v33/styles.xml index 6e26ae180685..2d720d210def 100644 --- a/packages/SettingsLib/SettingsSpinner/res/values-v33/styles.xml +++ b/packages/SettingsLib/SettingsSpinner/res/values-v33/styles.xml @@ -27,6 +27,7 @@ <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> + <item name="android:filterTouchesWhenObscured">true</item> </style> <style name="SettingsSpinnerDropdown"> @@ -40,5 +41,6 @@ <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> + <item name="android:filterTouchesWhenObscured">true</item> </style> </resources> diff --git a/packages/SettingsLib/SettingsSpinner/res/values-v36/attr.xml b/packages/SettingsLib/SettingsSpinner/res/values-v36/attr.xml new file mode 100644 index 000000000000..d40cd565e8ce --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/values-v36/attr.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2025 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> + <attr name="SettingsSpinnerPreferenceStyle" format="reference"/> + <declare-styleable name="SettingsSpinnerPreference"> + <attr name="style" format="enum"> + <enum name="normal" value="0"/> + <enum name="large" value="1"/> + <enum name="full" value="2"/> + <enum name="outlined" value="3"/> + </attr> + </declare-styleable> +</resources> diff --git a/packages/SettingsLib/SettingsSpinner/res/values-v36/styles.xml b/packages/SettingsLib/SettingsSpinner/res/values-v36/styles.xml new file mode 100644 index 000000000000..219788733eca --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/values-v36/styles.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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.Expressive"> + <item name="android:textAppearance">@style/TextAppearance.SettingsLib.LabelLarge</item> + <item name="android:textColor">@color/settingslib_materialColorOnSecondaryContainer</item> + <item name="android:maxLines">1</item> + <item name="android:ellipsize">marquee</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium3</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_extrasmall5</item> + </style> + + <style name="SettingsSpinnerTitleBar.Expressive.Large"> + <item name="android:textAppearance">@style/TextAppearance.SettingsLib.TitleMedium</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium5</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + </style> + + <style name="SettingsSpinnerDropdown.Expressive"> + <item name="android:background">@drawable/settings_expressive_spinner_dropdown_background</item> + <item name="android:minHeight">@dimen/spinner_dropdown_height</item> + <item name="android:paddingStart">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="android:paddingEnd">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="android:paddingTop">@dimen/settingslib_expressive_space_extrasmall7</item> + <item name="android:paddingBottom">@dimen/settingslib_expressive_space_extrasmall7</item> + </style> + + <style name="SettingsSpinnerDropdownText"> + <item name="android:textAppearance">@style/TextAppearance.SettingsLib.LabelLarge</item> + <item name="android:textColor">@color/settingslib_spinner_dropdown_color</item> + <item name="android:maxLines">1</item> + <item name="android:ellipsize">marquee</item> + </style> +</resources> diff --git a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerAdapter.java b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerAdapter.java index f33cacd36c6d..67bc82ccf100 100644 --- a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerAdapter.java +++ b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerAdapter.java @@ -22,7 +22,13 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.android.settingslib.widget.SettingsSpinnerPreference.Style; import com.android.settingslib.widget.spinner.R; + /** * An ArrayAdapter which was used by Spinner with settings style. * @param <T> the data type to be loaded. @@ -30,8 +36,13 @@ import com.android.settingslib.widget.spinner.R; public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> { private static final int DEFAULT_RESOURCE = R.layout.settings_spinner_view; - private static final int DFAULT_DROPDOWN_RESOURCE = R.layout.settings_spinner_dropdown_view; + private static final int DEFAULT_DROPDOWN_RESOURCE = R.layout.settings_spinner_dropdown_view; + private static final int DEFAULT_EXPRESSIVE_RESOURCE = + R.layout.settings_expressvie_spinner_view; + private static final int DEFAULT_EXPRESSIVE_DROPDOWN_RESOURCE = + R.layout.settings_expressvie_spinner_dropdown_view; private final LayoutInflater mDefaultInflater; + private int mSelectedPosition = -1; /** * Constructs a new SettingsSpinnerAdapter with the given context. @@ -41,17 +52,68 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> { * access the current theme, resources, etc. */ public SettingsSpinnerAdapter(Context context) { - super(context, getDefaultResource()); + super(context, getDefaultResource(context, Style.NORMAL)); + + setDropDownViewResource(getDropdownResource(context)); + mDefaultInflater = LayoutInflater.from(context); + } + + @Override + public View getDropDownView( + int position, @Nullable View convertView, @NonNull ViewGroup parent) { + View view; + if (convertView == null) { + view = + mDefaultInflater.inflate( + getDropdownResource(getContext()), parent, false /* attachToRoot */); + } else { + view = convertView; + } + TextView textView = view.findViewById(android.R.id.text1); + ImageView iconView = view.findViewById(android.R.id.icon); + iconView.setVisibility((position == mSelectedPosition) ? View.VISIBLE : View.GONE); + String item = (String) getItem(position); + textView.setText(item); + return view; + } - setDropDownViewResource(getDropdownResource()); + public void setSelectedPosition(int pos) { + mSelectedPosition = pos; + } + + public SettingsSpinnerAdapter(Context context, SettingsSpinnerPreference.Style style) { + super(context, getDefaultResource(context, style)); + + setDropDownViewResource(getDropdownResource(context)); mDefaultInflater = LayoutInflater.from(context); } + private static int getDefaultResourceWithStyle(Style style) { + switch (style) { + case NORMAL -> { + return DEFAULT_EXPRESSIVE_RESOURCE; + } + case LARGE -> { + return R.layout.settings_expressive_spinner_view_large; + } + case FULL_WIDTH -> { + return R.layout.settings_expressive_spinner_view_full; + } + case OUTLINED -> { + return DEFAULT_RESOURCE; + } + default -> { + return DEFAULT_RESOURCE; + } + } + } + /** * In overridded {@link #getView(int, View, ViewGroup)}, use this method to get default view. */ public View getDefaultView(int position, View convertView, ViewGroup parent) { - return mDefaultInflater.inflate(getDefaultResource(), parent, false /* attachToRoot */); + return mDefaultInflater.inflate( + getDefaultResource(getContext(), Style.NORMAL), parent, false /* attachToRoot */); } /** @@ -59,15 +121,21 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> { * drop down view. */ public View getDefaultDropDownView(int position, View convertView, ViewGroup parent) { - return mDefaultInflater.inflate(getDropdownResource(), parent, false /* attachToRoot */); + return mDefaultInflater.inflate( + getDropdownResource(getContext()), parent, false /* attachToRoot */); } - private static int getDefaultResource() { + private static int getDefaultResource(Context context, Style style) { + int resId = SettingsThemeHelper.isExpressiveTheme(context) + ? getDefaultResourceWithStyle(style) : DEFAULT_DROPDOWN_RESOURCE; return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) - ? DEFAULT_RESOURCE : android.R.layout.simple_spinner_dropdown_item; + ? resId : android.R.layout.simple_spinner_dropdown_item; } - private static int getDropdownResource() { + + private static int getDropdownResource(Context context) { + int resId = SettingsThemeHelper.isExpressiveTheme(context) + ? DEFAULT_EXPRESSIVE_DROPDOWN_RESOURCE : DEFAULT_DROPDOWN_RESOURCE; return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) - ? DFAULT_DROPDOWN_RESOURCE : android.R.layout.simple_spinner_dropdown_item; + ? resId : android.R.layout.simple_spinner_dropdown_item; } } diff --git a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java index 1170f1e7c695..096cec0d58a7 100644 --- a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java +++ b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java @@ -17,12 +17,15 @@ package com.android.settingslib.widget; import android.content.Context; +import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.accessibility.AccessibilityEvent; import android.widget.AdapterView; import android.widget.Spinner; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.preference.Preference; import androidx.preference.Preference.OnPreferenceClickListener; import androidx.preference.PreferenceViewHolder; @@ -44,29 +47,28 @@ public class SettingsSpinnerPreference extends Preference /** * Perform inflation from XML and apply a class-specific base style. * - * @param context The {@link Context} this is associated with, through which it can - * access the current theme, resources, {@link SharedPreferences}, etc. - * @param attrs The attributes of the XML tag that is inflating the preference + * @param context The {@link Context} this is associated with, through which it can access the + * current theme, resources, {@link SharedPreferences}, etc. + * @param attrs The attributes of the XML tag that is inflating the preference * @param defStyle 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. + * resource that supplies default values for the view. Can be 0 to not look for defaults. */ public SettingsSpinnerPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - setLayoutResource(R.layout.settings_spinner_preference); + initAttributes(context, attrs, defStyle); setOnPreferenceClickListener(this); } /** * Perform inflation from XML and apply a class-specific base style. * - * @param context The {@link Context} this is associated with, through which it can - * access the current theme, resources, {@link SharedPreferences}, etc. - * @param attrs The attributes of the XML tag that is inflating the preference + * @param context The {@link Context} this is associated with, through which it can access the + * current theme, resources, {@link SharedPreferences}, etc. + * @param attrs The attributes of the XML tag that is inflating the preference */ - public SettingsSpinnerPreference(Context context, AttributeSet attrs) { + public SettingsSpinnerPreference(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); - setLayoutResource(R.layout.settings_spinner_preference); + initAttributes(context, attrs, 0); setOnPreferenceClickListener(this); } @@ -75,8 +77,31 @@ public class SettingsSpinnerPreference extends Preference * * @param context The Context this is associated with. */ - public SettingsSpinnerPreference(Context context) { + public SettingsSpinnerPreference(@NonNull Context context) { this(context, null); + initAttributes(context, null, 0); + } + + public enum Style { + NORMAL, + LARGE, + FULL_WIDTH, + OUTLINED, + } + + private void initAttributes( + @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + int layoutRes = R.layout.settings_spinner_preference; + try (TypedArray a = + context.obtainStyledAttributes( + attrs, R.styleable.SettingsSpinnerPreference, defStyleAttr, 0)) { + int style = a.getInteger(R.styleable.SettingsSpinnerPreference_style, 0); + switch (style) { + case 2 -> layoutRes = R.layout.settings_expressive_spinner_preference_full; + default -> layoutRes = R.layout.settings_spinner_preference; + } + } + setLayoutResource(layoutRes); } @Override @@ -115,6 +140,10 @@ public class SettingsSpinnerPreference extends Preference public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); final Spinner spinner = (Spinner) holder.findViewById(R.id.spinner); + if (spinner == null) { + return; + } + mAdapter.setSelectedPosition(mPosition); spinner.setAdapter(mAdapter); spinner.setSelection(mPosition); spinner.setOnItemSelectedListener(mOnSelectedListener); @@ -140,20 +169,22 @@ public class SettingsSpinnerPreference extends Preference private final AdapterView.OnItemSelectedListener mOnSelectedListener = new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { - if (mPosition == position) return; - mPosition = position; - if (mListener != null) { - mListener.onItemSelected(parent, view, position, id); - } - } + @Override + public void onItemSelected( + AdapterView<?> parent, View view, int position, long id) { + if (mPosition == position) return; + mPosition = position; + mAdapter.setSelectedPosition(mPosition); + if (mListener != null) { + mListener.onItemSelected(parent, view, position, id); + } + } - @Override - public void onNothingSelected(AdapterView<?> parent) { - if (mListener != null) { - mListener.onNothingSelected(parent); - } - } - }; + @Override + public void onNothingSelected(AdapterView<?> parent) { + if (mListener != null) { + mListener.onNothingSelected(parent); + } + } + }; } diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_background.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_background.xml new file mode 100644 index 000000000000..139418b38e03 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_background.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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="?android:attr/colorControlHighlight"> + + <item android:id="@android:id/background"> + <layer-list + android:paddingMode="stack" + android:paddingStart="0dp" + android:paddingEnd="@dimen/settingslib_expressive_space_small4"> + <item> + <shape> + <corners android:radius="@dimen/settingslib_expressive_radius_full"/> + <solid android:color="@color/settingslib_materialColorSecondaryContainer"/> + <size android:height="@dimen/settingslib_expressive_space_medium3"/> + </shape> + </item> + + <item + android:gravity="center|end" + android:width="@dimen/settingslib_expressive_space_small4" + android:height="@dimen/settingslib_expressive_space_small4" + android:end="@dimen/settingslib_expressive_space_small1"> + <vector + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24" + android:tint="@color/settingslib_materialColorOnSecondaryContainer"> + <path + android:fillColor="@android:color/white" + android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6 -1.41,-1.41z"/> + </vector> + </item> + </layer-list> + </item> +</ripple> diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_dropdown_background.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_dropdown_background.xml new file mode 100644 index 000000000000..ac38c3e9223b --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v36/settingslib_expressive_spinner_dropdown_background.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2025 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="?android:attr/colorControlHighlight"> + + <item android:id="@android:id/background"> + <layer-list + android:paddingMode="stack" + android:paddingStart="@dimen/settingslib_expressive_space_extrasmall4" + android:paddingEnd="@dimen/settingslib_expressive_space_extrasmall4" + android:paddingTop="@dimen/settingslib_expressive_space_extrasmall4" + android:paddingBottom="@dimen/settingslib_expressive_space_extrasmall4"> + + <item> + <shape> + <corners android:radius="@dimen/settingslib_expressive_radius_large3"/> + <solid android:color="@color/settingslib_materialColorSurfaceContainerLow"/> + </shape> + </item> + </layer-list> + </item> +</ripple> diff --git a/packages/SettingsLib/SettingsTheme/res/values-v36/styles_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v36/styles_expressive.xml index de48f99215fb..94cad47c4fd0 100644 --- a/packages/SettingsLib/SettingsTheme/res/values-v36/styles_expressive.xml +++ b/packages/SettingsLib/SettingsTheme/res/values-v36/styles_expressive.xml @@ -31,6 +31,13 @@ <item name="trackTint">@color/settingslib_expressive_color_main_switch_track</item> </style> + <style name="SettingslibSpinnerStyle.Expressive" + parent="android:style/Widget.Material.Spinner"> + <item name="android:background">@drawable/settingslib_expressive_spinner_background</item> + <item name="android:popupBackground">@drawable/settingslib_expressive_spinner_dropdown_background</item> + <item name="android:dropDownVerticalOffset">@dimen/settingslib_expressive_space_large3</item> + </style> + <style name="EntityHeader"> <item name="android:paddingTop">@dimen/settingslib_expressive_space_small4</item> <item name="android:paddingBottom">@dimen/settingslib_expressive_space_small1</item> @@ -125,4 +132,4 @@ <item name="android:layout_marginEnd">@dimen/settingslib_expressive_space_extrasmall4</item> <item name="android:background">@drawable/settingslib_expressive_button_background_outline</item> </style> -</resources>
\ No newline at end of file +</resources> diff --git a/packages/SettingsLib/SettingsTheme/res/values-v36/themes_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v36/themes_expressive.xml index 5173ebeaa9a1..ffbc65cc622b 100644 --- a/packages/SettingsLib/SettingsTheme/res/values-v36/themes_expressive.xml +++ b/packages/SettingsLib/SettingsTheme/res/values-v36/themes_expressive.xml @@ -32,9 +32,9 @@ <item name="preferenceTheme">@style/PreferenceTheme.SettingsLib.Expressive</item> <!-- Set up Spinner style --> - <!--item name="android:spinnerStyle"></item> - <item name="android:spinnerItemStyle"></item> - <item name="android:spinnerDropDownItemStyle"></item--> + <item name="android:spinnerStyle">@style/SettingslibSpinnerStyle.Expressive</item> + <!--<item name="android:spinnerItemStyle"></item> + <item name="android:spinnerDropDownItemStyle"></item>--> <!-- Set up edge-to-edge configuration for top app bar --> <item name="android:clipToPadding">false</item> @@ -66,4 +66,4 @@ <item name="buttonBarNegativeButtonStyle">@style/Widget.SettingsLib.DialogButton.Outline</item> <item name="buttonBarNeutralButtonStyle">@style/Widget.SettingsLib.DialogButton</item> </style> -</resources>
\ No newline at end of file +</resources> |