Skip to content

Commit

Permalink
[M3][Catalog] Add example of Preferences screen with Top App Bar
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 406825107
  • Loading branch information
paulfthomas authored and veganafro committed Nov 1, 2021
1 parent 33f1e3b commit 83edbc4
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions catalog/build.gradle
Expand Up @@ -15,6 +15,7 @@ dependencies {
api "androidx.recyclerview:recyclerview:1.2.1"
api 'androidx.window:window:1.0.0-beta02'
api "androidx.window:window-java:1.0.0-beta02"
api "androidx.preference:preference:1.1.1"

api 'com.google.guava:guava:27.0.1-android'

Expand Down
Expand Up @@ -81,6 +81,13 @@ public Intent createActivityIntent() {
return new Intent(getContext(), TopAppBarScrollingTransparentStatusDemoActivity.class);
}
});
additionalDemos.add(
new Demo(R.string.cat_topappbar_preferences_demo_title) {
@Override
public Fragment createFragment() {
return new TopAppBarPreferencesFragment();
}
});
additionalDemos.addAll(getCollapsingToolbarDemos());
additionalDemos.add(getToolbarDemo());
additionalDemos.addAll(getActionBarDemos());
Expand Down
@@ -0,0 +1,89 @@
/*
* Copyright 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
*
* https://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 io.material.catalog.topappbar;

import io.material.catalog.R;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceFragmentCompat;
import io.material.catalog.feature.DemoFragment;
import io.material.catalog.feature.DemoUtils;

/** A fragment that displays a preferences Top App Bar demo for the Catalog app. */
public class TopAppBarPreferencesFragment extends DemoFragment {

@Override
public void onCreate(@Nullable Bundle bundle) {
super.onCreate(bundle);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
View view =
layoutInflater.inflate(R.layout.cat_topappbar_preferences_fragment, viewGroup, false);

Toolbar toolbar = view.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);

getChildFragmentManager()
.beginTransaction()
.replace(R.id.cat_topappbar_preferences_container, new PreferencesFragment())
.commit();

return view;
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
menuInflater.inflate(R.menu.cat_topappbar_menu, menu);
super.onCreateOptionsMenu(menu, menuInflater);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return DemoUtils.showSnackbar(getActivity(), item) || super.onOptionsItemSelected(item);
}

@Override
public boolean shouldShowDefaultDemoActionBar() {
return false;
}

/** Example preferences fragment. */
public static class PreferencesFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @NonNull String rootKey) {
setPreferencesFromResource(R.xml.cat_topappbar_preferences, rootKey);
}
}
}
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 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
https://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:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="?attr/catalogToolbarWithCloseButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/cat_topappbar_preferences_toolbar_title" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:id="@+id/cat_topappbar_preferences_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Expand Up @@ -28,6 +28,8 @@
<string name="cat_topappbar_collapsing_multiline_title" translatable="false">Collapsing Demo (Multiline title)</string>
<string name="cat_topappbar_action_bar_title" translatable="false">Action Bar Demo</string>
<string name="cat_topappbar_dark_action_bar_title" translatable="false">Dark Action Bar Demo</string>
<string name="cat_topappbar_preferences_demo_title" translatable="false">Preferences Demo</string>
<string name="cat_topappbar_preferences_toolbar_title" translatable="false">Preferences</string>
<string name="cat_topappbar_lorem_ipsum" translatable="false">
\t\tLorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in scelerisque sem. Mauris volutpat, dolor id interdum ullamcorper, risus dolor egestas lectus, sit amet mattis purus dui nec risus. Maecenas non sodales nisi, vel dictum dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse blandit eleifend diam, vel rutrum tellus vulputate quis. Aliquam eget libero aliquet, imperdiet nisl a, ornare ex. Sed rhoncus est ut libero porta lobortis. Fusce in dictum tellus.
\n\n\t\tSuspendisse interdum ornare ante. Aliquam nec cursus lorem. Morbi id magna felis. Vivamus egestas, est a condimentum egestas, turpis nisl iaculis ipsum, in dictum tellus dolor sed neque. Morbi tellus erat, dapibus ut sem a, iaculis tincidunt dui. Interdum et malesuada fames ac ante ipsum primis in faucibus. Curabitur et eros porttitor, ultricies urna vitae, molestie nibh. Phasellus at commodo eros, non aliquet metus. Sed maximus nisl nec dolor bibendum, vel congue leo egestas.
Expand Down
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 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
https://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.
-->
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:key="category1"
app:title="Category 1">

<Preference
app:key="setting1"
app:summary="Summary for Setting 1"
app:title="Setting 1" />

<EditTextPreference
app:key="setting2"
app:summary="Summary for Setting 2"
app:title="Setting 2" />

<SwitchPreferenceCompat
app:key="setting3"
app:summary="Summary for Setting 3"
app:title="Setting 3" />

<CheckBoxPreference
app:key="setting4"
app:summary="Summary for Setting 4"
app:title="Setting 4" />
</PreferenceCategory>

<PreferenceCategory
app:key="category2"
app:title="Category 2">

<Preference
app:key="setting5"
app:summary="Summary for Setting 5"
app:title="Setting 5" />

<EditTextPreference
app:key="setting6"
app:summary="Summary for Setting 6"
app:title="Setting 6" />

<SwitchPreferenceCompat
app:key="setting7"
app:summary="Summary for Setting 7"
app:title="Setting 7" />

<CheckBoxPreference
app:key="setting8"
app:summary="Summary for Setting 8"
app:title="Setting 8" />
</PreferenceCategory>

<PreferenceCategory
app:key="category3"
app:title="Category 3">

<Preference
app:key="setting9"
app:summary="Summary for Setting 9"
app:title="Setting 9" />

<EditTextPreference
app:key="setting10"
app:summary="Summary for Setting 10"
app:title="Setting 10" />

<SwitchPreferenceCompat
app:key="setting11"
app:summary="Summary for Setting 11"
app:title="Setting 11" />

<CheckBoxPreference
app:key="setting12"
app:summary="Summary for Setting 12"
app:title="Setting 12" />
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 83edbc4

Please sign in to comment.