-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[M3][Catalog] Add example of Preferences screen with Top App Bar
PiperOrigin-RevId: 406825107
- Loading branch information
1 parent
33f1e3b
commit 83edbc4
Showing
6 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
catalog/java/io/material/catalog/topappbar/TopAppBarPreferencesFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_preferences_fragment.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
catalog/java/io/material/catalog/topappbar/res/xml/cat_topappbar_preferences.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |