Skip to content

Commit

Permalink
[Catalog] Add Navigation Drawer demo
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 429379636
  • Loading branch information
dsn5ft authored and hunterstich committed Feb 23, 2022
1 parent bdbf052 commit 2ce5f3b
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions catalog/build.gradle
Expand Up @@ -69,6 +69,7 @@ def srcDirs = [
'menu',
'musicplayer',
'navigationrail',
'navigationdrawer',
'preferences',
'progressindicator',
'radiobutton',
Expand Down
4 changes: 4 additions & 0 deletions catalog/java/io/material/catalog/AndroidManifest.xml
Expand Up @@ -46,6 +46,10 @@
android:name=".elevation.ElevationOverlayDemoActivity"
android:exported="true"
android:label="@string/cat_elevation_overlay_title" />
<activity
android:name=".navigationdrawer.NavigationDrawerDemoActivity"
android:exported="true"
android:label="@string/cat_navigationdrawer_title" />
<activity
android:name=".topappbar.TopAppBarActionBarDemoActivity"
android:exported="true"
Expand Down
@@ -0,0 +1,86 @@
/*
* 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.navigationdrawer;

import io.material.catalog.R;

import android.os.Bundle;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import io.material.catalog.feature.DemoActivity;

/** A fragment that displays the main Navigation Drawer demo for the Catalog app. */
public class NavigationDrawerDemoActivity extends DemoActivity {

private DrawerLayout drawerLayout;

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

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

drawerLayout = view.findViewById(R.id.drawer);
drawerLayout.addDrawerListener(
new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.cat_navigationdrawer_button_show_content_description,
R.string.cat_navigationdrawer_button_hide_content_description));

NavigationView navigationView = view.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(
menuItem -> {
navigationView.setCheckedItem(menuItem);
drawerLayout.closeDrawer(Gravity.START);
return true;
});

return view;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
drawerLayout.openDrawer(Gravity.START);
return true;
}

return super.onOptionsItemSelected(menuItem);
}

@Override
protected boolean shouldShowDefaultDemoActionBar() {
return false;
}
}
@@ -0,0 +1,76 @@
/*
* Copyright 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.
*/

package io.material.catalog.navigationdrawer;

import io.material.catalog.R;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import dagger.Provides;
import dagger.android.ContributesAndroidInjector;
import dagger.multibindings.IntoSet;
import io.material.catalog.application.scope.ActivityScope;
import io.material.catalog.application.scope.FragmentScope;
import io.material.catalog.feature.Demo;
import io.material.catalog.feature.DemoLandingFragment;
import io.material.catalog.feature.FeatureDemo;

/** A landing fragment that links to navigation drawer demos for the Catalog app. */
public class NavigationDrawerFragment extends DemoLandingFragment {

@Override
public int getTitleResId() {
return R.string.cat_navigationdrawer_title;
}

@Override
public int getDescriptionResId() {
return R.string.cat_navigationdrawer_description;
}

@NonNull
@Override
public Demo getMainDemo() {
return new Demo() {
@Override
public Intent createActivityIntent() {
return new Intent(requireContext(), NavigationDrawerDemoActivity.class);
}
};
}

/** The Dagger module for {@link NavigationDrawerFragment} dependencies. */
@dagger.Module
public abstract static class Module {
@FragmentScope
@ContributesAndroidInjector
abstract NavigationDrawerFragment contributeInjector();

@IntoSet
@Provides
@ActivityScope
static FeatureDemo provideFeatureDemo() {
return new FeatureDemo(R.string.cat_navigationdrawer_title, R.drawable.ic_side_drawer) {
@Override
public Fragment createFragment() {
return new NavigationDrawerFragment();
}
};
}
}
}
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 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
~
~ 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.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="?attr/catalogToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="@drawable/ic_drawer_menu_24px"
app:title="@string/cat_navigationdrawer_title"/>
</com.google.android.material.appbar.AppBarLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/cat_navigationdrawer_instructions"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:padding="64dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/cat_navigationdrawer_header"
app:menu="@menu/cat_navigationdrawer_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 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
~
~ 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="8dp"
android:gravity="bottom"
android:minHeight="174dp"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="12dp"
android:scaleType="centerCrop"
android:contentDescription="@string/cat_navigationdrawer_header_image_content_description"
app:srcCompat="@drawable/logo_avatar_anonymous_40dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cat_navigationdrawer_header_title"
android:textAppearance="?attr/textAppearanceTitleLarge" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cat_navigationdrawer_header_subtitle"
android:textAppearance="?attr/textAppearanceBodyMedium" />
</LinearLayout>
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 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.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/main_item"
android:title="@string/cat_navigationdrawer_header_mail">
<menu>
<item
android:id="@+id/search_item"
android:icon="@drawable/ic_search_24px"
android:title="@string/cat_navigationdrawer_search"
android:checkable="true"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/rotation_item"
android:icon="@drawable/ic_3d_rotation_24px"
android:title="@string/cat_navigationdrawer_3d"
android:checkable="true"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/accelerator_item"
android:icon="@drawable/ic_accelerator_24px"
android:title="@string/cat_navigationdrawer_accelerator"
android:checkable="true"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/dashboard_item"
android:icon="@drawable/ic_dashboard_24px"
android:title="@string/cat_navigationdrawer_dashboard"
android:checkable="true"
app:showAsAction="ifRoom"/>
</menu>
</item>
<item
android:id="@+id/labels_item"
android:title="@string/cat_navigationdrawer_header_labels">
<menu>
<item
android:id="@+id/label_one"
android:icon="@drawable/ic_info_outline_24px"
android:title="@string/cat_navigationdrawer_label"
android:checkable="true"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/label_two"
android:icon="@drawable/ic_info_outline_24px"
android:title="@string/cat_navigationdrawer_label"
android:checkable="true"
app:showAsAction="ifRoom"/>
</menu>
</item>
</menu>

0 comments on commit 2ce5f3b

Please sign in to comment.