Skip to content

Commit e1c0c71

Browse files
committed
add CoordinatorLayout to implement BottomNavigationView as per material design guidelines
Added BottomNavigationBehavior.java to define hide/show behavior for BottomNavigationView
1 parent 14b9694 commit e1c0c71

File tree

2 files changed

+180
-95
lines changed

2 files changed

+180
-95
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.dhananjay.dailygoals;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.design.widget.BottomNavigationView;
6+
import android.support.design.widget.CoordinatorLayout;
7+
import android.support.v4.view.ViewCompat;
8+
import android.util.AttributeSet;
9+
import android.util.Log;
10+
import android.view.View;
11+
12+
import static java.lang.Math.max;
13+
import static java.lang.Math.min;
14+
15+
16+
public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
17+
18+
/**
19+
* Default constructor for instantiating the behavior
20+
*/
21+
public BottomNavigationBehavior() {
22+
super();
23+
}
24+
25+
/**
26+
* Default constructor for inflating behaviors from the layout
27+
*
28+
* @param context
29+
* @param attrs
30+
*/
31+
32+
public BottomNavigationBehavior(Context context, AttributeSet attrs) {
33+
super(context, attrs);
34+
}
35+
36+
37+
/**
38+
* @param coordinatorLayout the CoordinatorLayout with which the behavior is associated
39+
* @param child the child view of the CoordinatorLayout this behavior is associated with
40+
* @param directTargetChild the child view of the CoordinatorLayout that either is or contains the view that is the target of nested
41+
* scrolling operation
42+
* @param target the descendant of the CoordinatorLayout initiating the scroll operation
43+
* @param axes the axis of scrolling - horizontal or vertical
44+
* @param type type of input event that caused the scrolling
45+
* @return true if behavior wishes to accept this nested scroll
46+
*/
47+
@Override
48+
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
49+
Log.v("Tag", target.getClass().toString());
50+
return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
51+
}
52+
53+
54+
/**
55+
* @param coordinatorLayout
56+
* @param child
57+
* @param target
58+
* @param dx the horizontal number of pixels that the user attempted to scroll
59+
* @param dy the vertical number of pixels that the user attempted to scroll
60+
* @param consumed an integer array where consumed[0] should be set to dx , consumer[1] should be set
61+
* to dy
62+
* @param type
63+
*/
64+
@Override
65+
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
66+
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
67+
child.setTranslationY(max(0f, min(((float) child.getHeight()), child.getTranslationY() + dy)));
68+
}
69+
70+
}
Lines changed: 110 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,138 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ScrollView
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
54
xmlns:tools="http://schemas.android.com/tools"
65
android:layout_width="match_parent"
76
android:layout_height="match_parent"
8-
android:orientation="vertical"
97
android:background="@drawable/dt"
8+
android:orientation="vertical"
109
android:scaleType="centerCrop"
1110
tools:context="com.example.dhananjay.dailygoals.MainActivity">
1211

13-
<LinearLayout
12+
<android.support.v4.widget.NestedScrollView
1413
android:layout_width="match_parent"
15-
android:layout_height="match_parent"
16-
android:orientation="vertical">
17-
<TextView
18-
android:layout_width="match_parent"
19-
android:layout_height="wrap_content"
20-
android:gravity="center"
21-
android:text="PLAN FOR"
22-
android:textSize="30sp"
23-
android:padding="20dp"
24-
android:textColor="#ffffff"
25-
android:fontFamily="cursive"/>
14+
android:layout_height="match_parent">
2615

27-
28-
<RelativeLayout
16+
<LinearLayout
2917
android:layout_width="match_parent"
30-
android:layout_height="wrap_content"
31-
android:layout_weight="1">
18+
android:layout_height="match_parent"
19+
android:orientation="vertical">
3220

33-
<android.support.v7.widget.RecyclerView
34-
android:id="@+id/recyclerView"
21+
<TextView
3522
android:layout_width="match_parent"
36-
android:layout_height="match_parent"
37-
android:layout_margin="16dp"
38-
android:elevation="10dp">
39-
</android.support.v7.widget.RecyclerView>
40-
</RelativeLayout>
41-
<TextView
42-
android:layout_width="match_parent"
43-
android:layout_height="wrap_content"
44-
android:gravity="center"
45-
android:text="ONGOING..."
46-
android:textSize="30sp"
47-
android:padding="20dp"
48-
android:textColor="#ffffff"
49-
android:fontFamily="cursive"/>
23+
android:layout_height="wrap_content"
24+
android:fontFamily="cursive"
25+
android:gravity="center"
26+
android:padding="20dp"
27+
android:text="PLAN FOR"
28+
android:textColor="#ffffff"
29+
android:textSize="30sp" />
5030

5131

52-
<RelativeLayout
53-
android:layout_width="match_parent"
54-
android:layout_height="wrap_content"
55-
android:layout_weight="1">
32+
<RelativeLayout
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:layout_weight="1">
36+
37+
<android.support.v7.widget.RecyclerView
38+
android:id="@+id/recyclerView"
39+
android:layout_width="match_parent"
40+
android:layout_height="match_parent"
41+
android:layout_margin="16dp"
42+
android:elevation="10dp">
43+
</android.support.v7.widget.RecyclerView>
44+
</RelativeLayout>
45+
46+
<TextView
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:fontFamily="cursive"
50+
android:gravity="center"
51+
android:padding="20dp"
52+
android:text="ONGOING..."
53+
android:textColor="#ffffff"
54+
android:textSize="30sp" />
55+
5656

57-
<android.support.v7.widget.RecyclerView
58-
android:id="@+id/recyclerView1"
57+
<RelativeLayout
5958
android:layout_width="match_parent"
60-
android:layout_height="match_parent"
61-
android:layout_margin="16dp"
62-
android:elevation="10dp">
63-
</android.support.v7.widget.RecyclerView>
64-
</RelativeLayout>
65-
<TextView
66-
android:layout_width="match_parent"
67-
android:layout_height="wrap_content"
68-
android:gravity="center"
69-
android:text="COMPLETED/MISSED"
70-
android:textSize="30sp"
71-
android:padding="20dp"
72-
android:textColor="#ffffff"
73-
android:fontFamily="cursive"/>
59+
android:layout_height="wrap_content"
60+
android:layout_weight="1">
61+
62+
<android.support.v7.widget.RecyclerView
63+
android:id="@+id/recyclerView1"
64+
android:layout_width="match_parent"
65+
android:layout_height="match_parent"
66+
android:layout_margin="16dp"
67+
android:elevation="10dp">
68+
</android.support.v7.widget.RecyclerView>
69+
</RelativeLayout>
70+
71+
<TextView
72+
android:layout_width="match_parent"
73+
android:layout_height="wrap_content"
74+
android:fontFamily="cursive"
75+
android:gravity="center"
76+
android:padding="20dp"
77+
android:text="COMPLETED/MISSED"
78+
android:textColor="#ffffff"
79+
android:textSize="30sp" />
7480

7581

76-
<RelativeLayout
77-
android:layout_width="match_parent"
78-
android:layout_height="wrap_content"
79-
android:layout_weight="1">
82+
<RelativeLayout
83+
android:layout_width="match_parent"
84+
android:layout_height="wrap_content"
85+
android:layout_weight="1">
86+
87+
<android.support.v7.widget.RecyclerView
88+
android:id="@+id/recyclerView2"
89+
android:layout_width="match_parent"
90+
android:layout_height="match_parent"
91+
android:layout_margin="16dp"
92+
android:elevation="10dp">
93+
</android.support.v7.widget.RecyclerView>
94+
</RelativeLayout>
95+
96+
<TextView
97+
android:layout_width="match_parent"
98+
android:layout_height="wrap_content"
99+
android:fontFamily="cursive"
100+
android:gravity="center"
101+
android:padding="20dp"
102+
android:text="ACHIEVEMENTS"
103+
android:textColor="#ffffff"
104+
android:textSize="30sp" />
80105

81-
<android.support.v7.widget.RecyclerView
82-
android:id="@+id/recyclerView2"
106+
107+
<RelativeLayout
83108
android:layout_width="match_parent"
84-
android:layout_height="match_parent"
85-
android:layout_margin="16dp"
86-
android:elevation="10dp">
87-
</android.support.v7.widget.RecyclerView>
88-
</RelativeLayout>
89-
<TextView
90-
android:layout_width="match_parent"
91-
android:layout_height="wrap_content"
92-
android:gravity="center"
93-
android:text="ACHIEVEMENTS"
94-
android:textSize="30sp"
95-
android:padding="20dp"
96-
android:textColor="#ffffff"
97-
android:fontFamily="cursive"/>
109+
android:layout_height="wrap_content"
110+
android:layout_weight="1">
98111

112+
<android.support.v7.widget.RecyclerView
113+
android:id="@+id/recyclerView3"
114+
android:layout_width="match_parent"
115+
android:layout_height="match_parent"
116+
android:layout_margin="16dp"
117+
android:elevation="10dp">
118+
</android.support.v7.widget.RecyclerView>
119+
</RelativeLayout>
99120

100-
<RelativeLayout
101-
android:layout_width="match_parent"
102-
android:layout_height="wrap_content"
103-
android:layout_weight="1">
121+
</LinearLayout>
104122

105-
<android.support.v7.widget.RecyclerView
106-
android:id="@+id/recyclerView3"
107-
android:layout_width="match_parent"
108-
android:layout_height="match_parent"
109-
android:layout_margin="16dp"
110-
android:elevation="10dp">
111-
</android.support.v7.widget.RecyclerView>
112-
</RelativeLayout>
123+
</android.support.v4.widget.NestedScrollView>
124+
125+
<android.support.design.widget.BottomNavigationView
126+
android:id="@+id/bottom_navigation_view"
127+
android:layout_width="match_parent"
128+
android:layout_height="wrap_content"
129+
android:layout_gravity="bottom"
130+
android:background="@android:color/darker_gray"
131+
app:itemIconTint="@android:color/white"
132+
app:itemTextColor="@android:color/white"
133+
app:layout_behavior="com.example.dhananjay.dailygoals.BottomNavigationBehavior"
134+
app:menu="@menu/navigation" />
113135

114-
<android.support.design.widget.BottomNavigationView
115-
android:layout_width="match_parent"
116-
android:layout_height="60dp"
117-
app:itemIconTint="@android:color/white"
118-
app:itemTextColor="@android:color/white"
119-
app:menu="@menu/navigation"/>
120136

121-
</LinearLayout>
122-
</ScrollView>
137+
</android.support.design.widget.CoordinatorLayout>
123138

0 commit comments

Comments
 (0)