Skip to content

Commit e13610e

Browse files
raajkumarsdsn5ft
authored andcommitted
[Navigation Rail] Added Navigation Rail component to the library.
PiperOrigin-RevId: 355235369
1 parent 9c2c419 commit e13610e

File tree

20 files changed

+869
-2
lines changed

20 files changed

+869
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.material.catalog.navigationrail;
17+
18+
import io.material.catalog.R;
19+
20+
import android.os.Bundle;
21+
import android.view.LayoutInflater;
22+
import android.view.View;
23+
import android.view.ViewGroup;
24+
import androidx.annotation.Nullable;
25+
import io.material.catalog.feature.DemoFragment;
26+
27+
/** A fragment that displays the main Navigation Rail demo for the Catalog app. */
28+
public class NavigationRailDemoFragment extends DemoFragment {
29+
30+
@Override
31+
public View onCreateDemoView(LayoutInflater layoutInflater,
32+
@Nullable ViewGroup viewGroup,
33+
@Nullable Bundle bundle) {
34+
return layoutInflater.inflate(R.layout.nav_rail_main_demo, viewGroup, false);
35+
}
36+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.material.catalog.navigationrail;
17+
18+
import io.material.catalog.R;
19+
20+
import androidx.fragment.app.Fragment;
21+
import androidx.annotation.Nullable;
22+
import dagger.Provides;
23+
import dagger.android.ContributesAndroidInjector;
24+
import dagger.multibindings.IntoSet;
25+
import io.material.catalog.application.scope.ActivityScope;
26+
import io.material.catalog.application.scope.FragmentScope;
27+
import io.material.catalog.feature.Demo;
28+
import io.material.catalog.feature.DemoLandingFragment;
29+
import io.material.catalog.feature.FeatureDemo;
30+
31+
/** A landing fragment that links to Navigation Rail demos for the Catalog app. */
32+
public class NavigationRailFragment extends DemoLandingFragment {
33+
34+
@Override
35+
public int getTitleResId() {
36+
return R.string.cat_nav_rail_title;
37+
}
38+
39+
@Override
40+
public int getDescriptionResId() {
41+
return R.string.cat_nav_rail_description;
42+
}
43+
44+
@Override
45+
public Demo getMainDemo() {
46+
return new Demo() {
47+
@Nullable
48+
@Override
49+
public Fragment createFragment() {
50+
return new NavigationRailDemoFragment();
51+
}
52+
};
53+
}
54+
55+
/** The Dagger module for {@link NavigationRailFragment} dependencies. */
56+
@dagger.Module
57+
public abstract static class Module {
58+
@FragmentScope
59+
@ContributesAndroidInjector
60+
abstract NavigationRailFragment contributeInjector();
61+
62+
@IntoSet
63+
@Provides
64+
@ActivityScope
65+
static FeatureDemo provideFeatureDemo() {
66+
return new FeatureDemo(R.string.cat_nav_rail_title, R.drawable.ic_bottomnavigation) {
67+
@Override
68+
public Fragment createFragment() {
69+
return new NavigationRailFragment();
70+
}
71+
};
72+
}
73+
}
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2020 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<androidx.constraintlayout.widget.ConstraintLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent" >
23+
24+
<TextView
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:text="Navigation Rail Coming Soon"
28+
app:layout_constraintStart_toStartOf="parent"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintTop_toTopOf="parent"
31+
app:layout_constraintBottom_toBottomOf="parent"/>
32+
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2020 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<resources>
18+
<string name="cat_nav_rail_title">Navigation Rail</string>
19+
<string name="cat_nav_rail_description">
20+
Navigation Rails make it easy to explore and switch between top-level views in a single
21+
tap. This navigational element resides at the bottom of the screen, and provides direct access
22+
for three to five destinations within an app.
23+
</string>
24+
</resources>

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def srcDirs = [
5959
'com/google/android/material/math',
6060
'com/google/android/material/menu',
6161
'com/google/android/material/navigation',
62+
'com/google/android/material/navigationrail',
6263
'com/google/android/material/progressindicator',
6364
'com/google/android/material/radiobutton',
6465
'com/google/android/material/resources',

lib/java/com/google/android/material/dialog/res/values/themes_base_bridge.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
6262
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>
6363
<item name="materialCardViewStyle">@style/Widget.MaterialComponents.CardView</item>
64+
<item name="navigationRailStyle">@style/Widget.MaterialComponents.NavigationRailView</item>
6465
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
6566

6667
<!-- Type styles -->
@@ -141,6 +142,7 @@
141142
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
142143
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>
143144
<item name="materialCardViewStyle">@style/Widget.MaterialComponents.CardView</item>
145+
<item name="navigationRailStyle">@style/Widget.MaterialComponents.NavigationRailView</item>
144146
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
145147

146148
<!-- Type styles -->

lib/java/com/google/android/material/navigation/res-public/values/public.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<resources>
2020
<public name="navigationViewStyle" type="attr"/>
2121

22-
<public name="headerLayout" type="attr"/>
2322
<public name="itemBackground" type="attr"/>
2423
<public name="itemHorizontalPadding" type="attr"/>
2524
<public name="itemTextAppearance" type="attr"/>

lib/java/com/google/android/material/navigation/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<attr name="itemBackground" format="reference"/>
8181
<attr name="itemTextAppearance" format="reference"/>
8282
<!-- Layout resource to inflate as the header -->
83-
<attr name="headerLayout" format="reference"/>
83+
<attr name="headerLayout"/>
8484
<!-- Horizontal padding (left and right) of navigation items, around the icon & text -->
8585
<attr name="itemHorizontalPadding" format="dimension"/>
8686
<!-- Padding between the icon and the text for navigation items that display an icon -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.navigationrail;
18+
19+
import com.google.android.material.R;
20+
21+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
22+
23+
import android.content.Context;
24+
import androidx.annotation.DimenRes;
25+
import androidx.annotation.LayoutRes;
26+
import androidx.annotation.NonNull;
27+
import androidx.annotation.RestrictTo;
28+
import com.google.android.material.navigation.NavigationBarItemView;
29+
30+
/** @hide For internal use only. */
31+
@RestrictTo(LIBRARY_GROUP)
32+
final class NavigationRailItemView extends NavigationBarItemView {
33+
public NavigationRailItemView(@NonNull Context context) {
34+
super(context);
35+
}
36+
37+
@Override
38+
@LayoutRes
39+
protected int getItemLayoutResId() {
40+
return R.layout.mtrl_navigation_rail_item;
41+
}
42+
43+
@Override
44+
@DimenRes
45+
protected int getItemDefaultMarginResId() {
46+
return R.dimen.mtrl_navigation_rail_margin;
47+
}
48+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.navigationrail;
18+
19+
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
20+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
21+
import static com.google.android.material.navigationrail.NavigationRailView.DEFAULT_MENU_GRAVITY;
22+
import static java.lang.Math.min;
23+
24+
import android.content.Context;
25+
import androidx.appcompat.view.menu.MenuBuilder;
26+
import android.view.Gravity;
27+
import android.view.View;
28+
import android.view.ViewGroup;
29+
import android.widget.FrameLayout;
30+
import androidx.annotation.NonNull;
31+
import androidx.annotation.RestrictTo;
32+
import com.google.android.material.navigation.NavigationBarItemView;
33+
import com.google.android.material.navigation.NavigationBarMenuView;
34+
35+
/** @hide For internal use only. */
36+
@RestrictTo(LIBRARY_GROUP)
37+
public class NavigationRailMenuView extends NavigationBarMenuView {
38+
39+
private final FrameLayout.LayoutParams layoutParams =
40+
new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
41+
42+
public NavigationRailMenuView(@NonNull Context context) {
43+
super(context);
44+
45+
layoutParams.gravity = DEFAULT_MENU_GRAVITY;
46+
setLayoutParams(layoutParams);
47+
}
48+
49+
@Override
50+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
51+
int childHeightSpec = makeSharedHeightSpec(widthMeasureSpec, heightMeasureSpec);
52+
53+
int childCount = getChildCount();
54+
int maxWidth = 0;
55+
int totalHeight = 0;
56+
for (int i = 0; i < childCount; i++) {
57+
final View child = getChildAt(i);
58+
if (child.getVisibility() != GONE) {
59+
child.measure(widthMeasureSpec, childHeightSpec);
60+
ViewGroup.LayoutParams params = child.getLayoutParams();
61+
params.width = child.getMeasuredWidth();
62+
params.height = child.getMeasuredHeight();
63+
totalHeight += params.height;
64+
if (params.width > maxWidth) {
65+
maxWidth = params.width;
66+
}
67+
}
68+
}
69+
70+
// Set view to use a fixed width, but wrap all item heights
71+
setMeasuredDimension(
72+
View.resolveSizeAndState(
73+
maxWidth,
74+
MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.EXACTLY),
75+
/* childMeasuredState= */ 0),
76+
View.resolveSizeAndState(
77+
totalHeight,
78+
MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY),
79+
/* childMeasuredState= */ 0));
80+
}
81+
82+
@Override
83+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
84+
final int count = getChildCount();
85+
final int width = right - left;
86+
int used = 0;
87+
for (int i = 0; i < count; i++) {
88+
final View child = getChildAt(i);
89+
if (child.getVisibility() != GONE) {
90+
int childHeight = child.getMeasuredHeight();
91+
child.layout(/* l= */ 0, used, width, childHeight + used);
92+
used += childHeight;
93+
}
94+
}
95+
}
96+
97+
@Override
98+
@NonNull
99+
protected NavigationBarItemView createNavigationBarItemView(@NonNull Context context) {
100+
return new NavigationRailItemView(context);
101+
}
102+
103+
private int makeSharedHeightSpec(int parentWidthSpec, int parentHeightSpec) {
104+
MenuBuilder menu = getMenu();
105+
int visibleCount = menu.getVisibleItems().size();
106+
int maxHeight = MeasureSpec.getSize(parentHeightSpec);
107+
int maxAvailable = maxHeight / (visibleCount == 0 ? 1 : visibleCount);
108+
109+
return MeasureSpec.makeMeasureSpec(
110+
min(MeasureSpec.getSize(parentWidthSpec), maxAvailable), MeasureSpec.EXACTLY);
111+
}
112+
113+
void setMenuGravity(int gravity) {
114+
if (layoutParams.gravity != gravity) {
115+
layoutParams.gravity = gravity;
116+
setLayoutParams(layoutParams);
117+
}
118+
}
119+
120+
int getMenuGravity() {
121+
return layoutParams.gravity;
122+
}
123+
124+
boolean isTopGravity() {
125+
return (layoutParams.gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.TOP;
126+
}
127+
}

0 commit comments

Comments
 (0)