Introduction
Google announced Material Design guidelines for Android apps last year and since then it has been adopted by many application developers. Google only issued guidelines to implement a Material theme. The material theme is available to Lollipop devices and no backward compatibility was provided, developers were dependent on libraries made by other developers. Last month Google released the Android Design Support Library that provides support for a large number of UI design components in a single library. I will be showing you how to use components in that library.
This article assumes that you have a basic understanding of Android and have done some development using Android Studio.
Let's start by creating a new Project with an Activity inside it. It will have an Activity with its respective layout, let's call it MainActivity.java and its layout file activity_main.xml.
My demo app has the target SDK version set to 22 and minimum SDK to 15.
Let's just add the design support library to our project first. The library already depends on support library v4 and v7, so you don't need to add them separately.
Add the following line to your dependencies in your app's build.gradle file.
- dependencies {
- ….
- compile 'com.android.support:design:22.2.0'
- …
- }
Then we will add a Drawer (Left Menu) to the main activity that is now a standard in a large number of applications.
Add the following lines of code to add a drawer to your app.
1. style.xml: Add these lines to your theme,
- <!-- your app branding color for the app bar -->
- <item name="colorPrimary">#512da8</item>
- <!-- darker variant for the status bar and contextual app bars -->
- <item name="colorPrimaryDark">#311b92</item>
- <!-- theme UI controls like checkboxes and text fields -->
- <item name="colorAccent">#7e57c2</item>
2. activity_main.xml: Add DrawerLayout as the base layout, then you can define 2 layouts inside it, one for the front layout and the other for the left menu layout.
- <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/drawer_layout"
- android:layout_height="match_parent"
- android:layout_width="match_parent">
- <FrameLayout
- android:id="@+id/content_frame"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <ListView android:id="@+id/left_drawer"
- android:layout_width="240dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:choiceMode="singleChoice"
- android:divider="@android:color/transparent"
- android:dividerHeight="0dp"
- android:background="#ffffff"/>
- </android.support.v4.widget.DrawerLayout>
3. MainActivity.java: This is the basic code for getting the drawer layout and setting a ActionBarDrawerToggle button, this button opens and closes the drawer.
- public class MainActivity extends AppCompatActivity {
- private ActionBarDrawerToggle mDrawerToggle;
- private DrawerLayout mDrawerLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
- setTitle("Material Design Example");
- mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name);
- mDrawerLayout.setDrawerListener(mDrawerToggle);
- getSupportActionBar().setHomeButtonEnabled(true);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- // Sync the toggle state after onRestoreInstanceState has occurred.
- mDrawerToggle.syncState();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- mDrawerToggle.onConfigurationChanged(newConfig);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
At this moment our app looks as in the following,
Now let's start with adding the new components.


The first component we will add is the Floating Action Button (FAB). FAB is a small round button usually seen at the bottom-right corner of the screen, although they can be anywhere but need to be at the top of the screen all the time. FAB buttons should be used to promote an action like create, edit and so on. You don't need this button on every screen. For example, create a new mail button in the Gmail app.




Neeraj KumarPosted Jul 3, 2015, 2:55 PM
Gr8
Santhakumar MunuswamyPosted Jul 3, 2015, 2:29 PM
Thanks for nice article.:)