Adds PreferenceScreen in a CoordinatorLayout w/ a collapsing app bar. Bug: 330062053 Test: No tests, just sample Change-Id: I4c67941ba59c53cf7bf6fb4e5a8f84004d14daa9
diff --git a/samples/Support4Demos/build.gradle b/samples/Support4Demos/build.gradle index 061c521..2e86206 100644 --- a/samples/Support4Demos/build.gradle +++ b/samples/Support4Demos/build.gradle
@@ -27,6 +27,8 @@ implementation(project(":coordinatorlayout:coordinatorlayout")) implementation("com.google.android.material:material:1.6.0") implementation(project(":appcompat:appcompat")) + api(project(":preference:preference")) + } android {
diff --git a/samples/Support4Demos/src/main/AndroidManifest.xml b/samples/Support4Demos/src/main/AndroidManifest.xml index 6865741..59c0a48 100644 --- a/samples/Support4Demos/src/main/AndroidManifest.xml +++ b/samples/Support4Demos/src/main/AndroidManifest.xml
@@ -438,6 +438,17 @@ </activity> <activity + android:name=".widget.PreferenceScreenWithCollapsingToolbarActivity" + android:exported="true" + android:theme="@style/Theme.AppCompat.Light" + android:label="@string/preference_screen_collapsing"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" /> + </intent-filter> + </activity> + + <activity android:name=".graphics.RoundedBitmapDrawableActivity" android:exported="true" android:label="Graphics/RoundedBitmapDrawable">
diff --git a/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenDetailsTemplateFragment.java b/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenDetailsTemplateFragment.java new file mode 100644 index 0000000..131dac5 --- /dev/null +++ b/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenDetailsTemplateFragment.java
@@ -0,0 +1,62 @@ +/* + * Copyright 2024 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.example.android.supportv4.widget; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.fragment.app.Fragment; + +import com.example.android.supportv4.R; + +import org.jspecify.annotations.NonNull; +import org.jspecify.annotations.Nullable; + +/** + * Simple LinearLayout with ~50 TextViews. + */ +public class PreferenceScreenDetailsTemplateFragment extends Fragment { + + @Override + public @NonNull View onCreateView( + @NonNull LayoutInflater inflater, + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState + ) { + View view = inflater.inflate( + R.layout.fragment_preference_screen_details_template, + container, + false + ); + + LinearLayout linearLayout = view.findViewById(R.id.linear_layout); + + for (int index = 1; index <= 50; index++) { + TextView textView = new TextView(getContext()); + textView.setText("TextView " + index); + textView.setFocusable(true); + textView.setFocusableInTouchMode(true); // For keyboard/touch based focus + textView.setPadding(16, 16, 16, 16); + + linearLayout.addView(textView); + } + return view; + } +}
diff --git a/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenWithCollapsingToolbarActivity.java b/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenWithCollapsingToolbarActivity.java new file mode 100644 index 0000000..676c9ee --- /dev/null +++ b/samples/Support4Demos/src/main/java/com/example/android/supportv4/widget/PreferenceScreenWithCollapsingToolbarActivity.java
@@ -0,0 +1,63 @@ +/* + * Copyright 2024 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.example.android.supportv4.widget; + +import android.os.Bundle; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.PreferenceFragmentCompat; + +import com.example.android.supportv4.R; +import com.google.android.material.appbar.CollapsingToolbarLayout; + +import org.jspecify.annotations.Nullable; + +/** + * This activity demonstrates the use of scrolling with a PreferenceScreen in the v4 support + * library along with a collapsing app bar (everything inside a CoordinatorLayout). See the + * associated layout file for details. + * Note: The PreferenceScreen fragment roughly demonstrates the Android Settings App (specifically + * the app screen). + */ +public class PreferenceScreenWithCollapsingToolbarActivity extends AppCompatActivity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_preference_screen_collapsing_toolbar); + + CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar_layout); + collapsingToolbar.setTitle( + getResources().getString(R.string.preference_screen_collapsing_appbar_title) + ); + collapsingToolbar.setContentScrimColor(getResources().getColor(R.color.color1)); + + MyPreferenceFragment fragment = new MyPreferenceFragment(); + getSupportFragmentManager().beginTransaction() + .replace(R.id.main_content, fragment) + .commit(); + } + + public static class MyPreferenceFragment extends PreferenceFragmentCompat { + @Override + public void onCreatePreferences( + @Nullable Bundle savedInstanceState, + @Nullable String rootKey + ) { + setPreferencesFromResource(R.xml.demo_pref_screen, rootKey); + } + } +}
diff --git a/samples/Support4Demos/src/main/res/layout/activity_preference_screen_collapsing_toolbar.xml b/samples/Support4Demos/src/main/res/layout/activity_preference_screen_collapsing_toolbar.xml new file mode 100644 index 0000000..4b2888f --- /dev/null +++ b/samples/Support4Demos/src/main/res/layout/activity_preference_screen_collapsing_toolbar.xml
@@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2024 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. +--> +<androidx.coordinatorlayout.widget.CoordinatorLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:fitsSystemWindows="true" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context="widget.PreferenceScreenWithCollapsingToolbarActivity"> + + <!-- App Bar --> + <com.google.android.material.appbar.AppBarLayout + android:layout_width="match_parent" + android:layout_height="200dp"> + + <com.google.android.material.appbar.CollapsingToolbarLayout + android:id="@+id/collapsing_toolbar_layout" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:expandedTitleMarginStart="48dp" + app:expandedTitleMarginEnd="64dp" + app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"> + + <View android:layout_width="match_parent" + android:layout_height="200dp" + android:background="@color/color2"/> + + <androidx.appcompat.widget.Toolbar + android:id="@+id/toolbar" + android:layout_width="match_parent" + android:layout_height="?attr/actionBarSize" + app:layout_collapseMode="pin" /> + </com.google.android.material.appbar.CollapsingToolbarLayout> + </com.google.android.material.appbar.AppBarLayout> + + <!-- Content --> + <LinearLayout + android:layout_height="match_parent" + android:layout_width="match_parent" + android:orientation="vertical" + app:layout_behavior="@string/appbar_scrolling_view_behavior"> + + <FrameLayout + android:id="@+id/main_content" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + </LinearLayout> +</androidx.coordinatorlayout.widget.CoordinatorLayout>
diff --git a/samples/Support4Demos/src/main/res/layout/fragment_preference_screen_details_template.xml b/samples/Support4Demos/src/main/res/layout/fragment_preference_screen_details_template.xml new file mode 100644 index 0000000..b5f4441 --- /dev/null +++ b/samples/Support4Demos/src/main/res/layout/fragment_preference_screen_details_template.xml
@@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2024 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. + --> +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".widget.PreferenceScreenDetailsTemplateFragment"> + + <LinearLayout + android:id="@+id/linear_layout" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" /> +</FrameLayout>
diff --git a/samples/Support4Demos/src/main/res/values/strings.xml b/samples/Support4Demos/src/main/res/values/strings.xml index 3ebe008..e9624cb 100644 --- a/samples/Support4Demos/src/main/res/values/strings.xml +++ b/samples/Support4Demos/src/main/res/values/strings.xml
@@ -13,7 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. --> - <resources> <string name="activity_sample_code">Support v4 Demos</string> @@ -233,9 +232,13 @@ <string name="nested_scroll">Widget/Nested Scrolling</string> <string name="nested_scroll_3_levels">Widget/Nested Scrolling (3 levels)</string> + <string name="nested_scroll_3_levels_collapsing">Widget/Nested Scrolling (3 levels) with Collapsing Toolbar</string> <string name="nested_scroll_3_levels_collapsing_appbar_title">Collapsing Appbar</string> + <string name="preference_screen_collapsing">Widget/PreferenceScreen with Collapsing Appbar</string> + <string name="preference_screen_collapsing_appbar_title">Collapsing Appbar</string> + <string name="nested_scroll_long_text">This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it. This is some long text. It just keeps going. Look at it. Scroll it. Scroll the nested version of it.</string> <string name="nested_scroll_short_text">This is shorter text. In fact, it was designed to be half as long as the long text, it is very close. This is shorter text. In fact, it was designed to be half as long as the long text, it is very close. This is shorter text. In fact, it was designed to be half as long as the long text, it is very close. This is shorter text. In fact, it was designed to be half as long as the long text, it is very close. This is shorter text. In fact, it was designed to be half as long as the long text, it is very close.</string> <string name="regular">regular</string>
diff --git a/samples/Support4Demos/src/main/res/xml/demo_pref_screen.xml b/samples/Support4Demos/src/main/res/xml/demo_pref_screen.xml new file mode 100644 index 0000000..2f5f7791 --- /dev/null +++ b/samples/Support4Demos/src/main/res/xml/demo_pref_screen.xml
@@ -0,0 +1,110 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2024 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. + --> +<android:PreferenceScreen + xmlns:android="http://schemas.android.com/apk/res/android" + android:key="apps_screen" + android:title="apps_dashboard_title"> + + <Preference + android:key="all_app_infos" + android:title="All Apps Demo" + android:summary="Summary Placeholder" + android:order="-999" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment" + /> + + <PreferenceCategory + android:key="recent_apps_category" + android:title="Recent App Category Title" + android:order="-998"> + <!-- Placeholder for a list of recent apps --> + <Preference + android:key="see_all_apps" + android:title="See all Apps Title" + android:icon="@drawable/ic_star_on" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment" + android:order="5"> + </Preference> + </PreferenceCategory> + + <PreferenceCategory + android:key="general_category" + android:title="Category Name General" + android:order="-997" + android:visibility="gone"/> + + <Preference + android:key="default_apps" + android:title="App Default Dashboard" + android:order="-996"/> + + <Preference + android:key="cloned_apps" + android:title="Cloned Apps Dashboard" + android:summary="Summary Placeholder" + android:order="-995" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment"> + </Preference> + + <PreferenceCategory + android:key="dashboard_tile_placeholder" + android:order="10"/> + + <Preference + android:key="contacts_storage" + android:title="Contacts Storage Settings" + android:summary="Summary Placeholder" + android:order="13" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment" + > + </Preference> + + <Preference + android:key="hibernated_apps" + android:title="Unused Apps" + android:summary="Summary Placeholder" + android:order="15" + /> + + <Preference + android:key="app_battery_usage" + android:order="17" + android:title="App Battery Usage" + android:summary="Summary Placeholder" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment"> + </Preference> + + <Preference + android:key="special_access" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment" + android:title="Special Access" + android:order="20"/> + + <PreferenceCategory + android:key="advanced_category" + android:title="Advanced Apps" + android:order="21"> + + <Preference + android:key="aspect_ratio_apps" + android:title="Aspect Ratio Experimental" + android:summary="Summary Placeholder" + android:order="22" + android:fragment="com.example.android.supportv4.widget.PreferenceScreenDetailsTemplateFragment"> + </Preference> + </PreferenceCategory> +</android:PreferenceScreen>