More than Just Lines on a Map: Best Practices for U.S Bike Routes
Getting started with android app and material design toolbar and navigation drawer
1. Surviving w/ Android - Step by Step guide about
Android and Internet of things
Develop Android App And Internet Of Things Projects With Step By Step Tutorial, Tips And In-Depth
Code Description.
Topics covered
Android material design
Android tooolbar
Android navigation drawer
Getting Started With Android App And Material
Design:Toolbar And Navigation Drawer
By Francesco Azzola , October 27, 2015 0 Comments
How to develop an Android app with material design guidelines
using Toolbar and Navigation drawer
Material design is a set of rule built by google that guide
how to develop an Android app. They can be applied not
only on the Android app but also on web site design. In the
process of development of an app, Android provides some
libraries that help developers to implement the main
material guide line rules. The most important libraries are:
com.android.support:appcompat-v7:23.1.0
com.android.support:design:23.1.0
After all, these two libraries are imported by default when a developer starts a new project
using Android Studio.
One important aspect in an app is represented by color schema. Material design rules
describes how to chose colours.
Let us suppose we create a simple Android project and let us follow the main step to
implement a Android app following Material design rules.
Material Design:Colours
The first step, is choosing the colour schema for our app. To this purpose there is a great
website that can be used to create the colour schema according to material design rules.
After the colours are selected we can download colors.xml:
!
"
Home About me Contact me Advertise
2. You can select the schema you like. The first result is shown in the picture below:
Now it is time to create our theme that uses the colours we selected before. The app should
support the largest number of smart phones not only those running Lollipop or later.
For this reason it is necessary to create two themes one for the devices that run Android 5 or
later and those that run pre-lollipop version.
So let us create two directory under values:
style
style-v21
The first one is used by all smart phones running pre-Lollipop version while the second folder
is used by smart phones with OS starting from Lollipop.
In the first directory let us style.xml:
1
2
3
4
5
6
7
8
9
10
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#03A9F4</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#B6B6B6</color>
</resources>
1
2
3
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
<item name="colorPrimary">@color/primary</item>
?
?
3. while in the second directory we simply add:
Finally in the Manifest.xml modify the file:
Android Toolbar
One of the most important component in developing an Android app is the Toolbar. The
toolbar plays the role that was played before by Android action bar.The toolbar can be used to
hold:
Navigation button
App tile and subtitle
Action menu
Brand logo
According to material design the Toolbar have the primary color we selected before. How to
add it to Android app?
4
5
6
7
8
<item name="colorPrimaryDark">@color/primary_dark</item
<item name="colorAccent">@color/accent</item>
</style>
<style name="MyAppTheme" parent="@style/AppTheme" />
</resources>
1
2
3
4
5
6
7
8
9
<resources>
<style name="MyAppTheme" parent="AppTheme">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</
<item name="android:windowAllowReturnTransitionOverlap">true</
<item name="android:windowSharedElementEnterTransition">@android:
<item name="android:windowSharedElementExitTransition">@android:t
</style>
</resources>
1
2
3
4
<application
android:theme="@style/MyAppTheme" >
...
</application>
1
2
3
4
5
6
7
8
9
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr
xmlns:tools="http://schemas.android.com/tools" android:layout_widt
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
<include layout="@layout/toolbar" />
</RelativeLayout>
?
?
?
4. where toolbar layout is:
Notice at line 5 we set the default height of the toolbar using ?attr/actionBarSize
and at line 6 the toolbar background.
In the activity it is necessary to set the toolbar:
Running the example we get:
1
2
3
4
5
6
7
8
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.andro
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolBar();
}
...
private void setToolBar() {
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
ab.setDisplayHomeAsUpEnabled(true);
}
?
?
5. Add Action Menu To Toolbar
Once the is configured correctly, it is possible to add action menu or menu items that appear
on the Toolbar, to do it under res/menu add a file called main_menu.xml :
Now in the activity there is:
Running the example the app looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_settings"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences"
app:showAsAction="always"
android:orderInCategory="100"/>
<item android:id="@+id/menu_help"
android:title="Help"
android:icon="@android:drawable/ic_menu_help"
app:showAsAction="ifRoom"
android:orderInCategory="110" />
<item android:id="@+id/menu_compass"
android:title="Compass"
android:icon="@android:drawable/ic_menu_compass"
app:showAsAction="never"
android:orderInCategory="105"/>
</menu>
1
2
3
4
5
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
?
?
6. When user select one of this item the app should be detect it and take the right action, to do it
it is necessary to override a method:
In this case we simply show a info message using a Snackbar .
Android Navigation Drawer
Navigation drawer is one of the most import UI pattern introduced by Google in developing
Android app.Navigation drawer is a side menu that helps to organise the navigation inside
the app. It is a uniform way to access different pages and information inside the app. You can
refer to official google page to know more. The implementation is very easy. The custom
view that represents the navigation drawer must be the first element in the layout:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
String btnName = null;
switch(itemId) {
case R.id.menu_settings:
btnName = "Settings";
break;
case R.id.menu_compass:
btnName = "Compass";
break;
case R.id.menu_help:
btnName = "Help";
break;
}
Snackbar.make(layout, "Button " + btnName, Snackbar.LENGTH_SHORT).s
return true;
}
?
?
7. In this case the toolbar is inside a LinearLayout but the way the app handles it is the
same shown before. In this case there is a FrameLayout to hold the page content shown
through fragments. The NavigationView is the "real" menu of our app. The menu items
are written in nav_items .
To handle when user clicks on an item is very easy, it is necessary to write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<!-- Let's add fragment -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_items" />
</android.support.v4.widget.DrawerLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<group android:checkableBehavior="single">
<item android:id="@+id/fab"
android:title="Floating Action Button"
android:icon="@drawable/ic_action_fab" />
<item android:id="@+id/star"
android:title="Star"
android:icon="@drawable/ic_action_star" />
<item android:id="@+id/uploadr"
android:title="Star"
android:icon="@drawable/ic_action_upload" />
</group>
</menu>
1
2
3
4
5
6
private void setNavigationDrawer() {
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navView = (NavigationView) findViewById(R.id.navig
navView.setNavigationItemSelectedListener(new NavigationView.OnNa
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
?
?
8. We simply add a listener to know when one of the menu item is pressed by user and then set
the right fragment. The last step is opening the drawer when user clicks on the home icon, to
do it:
Running the example app we have:
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Fragment frag = null;
int itemId = menuItem.getItemId();
if (itemId == R.id.fab) {
frag = new Fragment1();
}
else if (itemId == R.id.star) {
frag = new Fragment2();
}
if (frag != null) {
FragmentTransaction transaction = getSupportFragmentM
transaction.replace(R.id.frame, frag);
transaction.commit();
dLayout.closeDrawers();
return true;
}
return false;
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
String btnName = null;
switch(itemId) {
...
// Android home
case android.R.id.home: {
dLayout.openDrawer(GravityCompat.START);
return true;
}
}
.....
}
?