HarshitVaish
10/1/2016 - 7:21 AM

Navigation Drawer implementation

Navigation Drawer implementation

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Base.AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>


</resources>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/group_1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/item_1"
            android:checkable="true"
            android:icon="@mipmap/ic_launcher"
            android:title="Item 1" />
        <item
            android:id="@+id/item_2"
            android:checkable="true"
            android:icon="@mipmap/ic_launcher"
            android:title="Item 2" />
    </group>
    <group android:id="@+id/group_2">
        <item
            android:id="@+id/item_3"
            android:checkable="true"
            android:icon="@mipmap/ic_launcher"
            android:title="Item 3" />
        <item
            android:id="@+id/item_4"
            android:checkable="true"
            android:icon="@mipmap/ic_launcher"
            android:title="Item 4" />
    </group>
    <item
        android:id="@+id/group_3_header"
        android:title="Group 3 title">
        <menu>
            <item
                android:id="@+id/item_5"
                android:checkable="true"
                android:icon="@mipmap/ic_launcher"
                android:title="Item 3" />
            <item
                android:id="@+id/item_6"
                android:checkable="true"
                android:icon="@mipmap/ic_launcher"
                android:title="Item 4" />
        </menu>
    </item>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="190dp"
    android:background="@mipmap/ic_launcher">

    <LinearLayout
        android:paddingTop="15dp"
        android:paddingLeft="15dp"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="56dp">
        <TextView
            android:id="@+id/text_view_name"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:text="FirstName MiddleName"
            android:textColor="#000000"
            android:gravity="bottom"
            android:textSize="14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/text_view_email"
            android:layout_weight="1"
            android:textSize="14sp"
            android:fontFamily="sans-serif"
            android:text="firstname.lastname@gmail.com"
            android:textColor="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>



</RelativeLayout>
dependencies {
    compile 'com.android.support:design:24.2.1'
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

    
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/header"
        app:itemIconTint="#8C000000"
        app:itemTextColor="#E6000000"
        app:menu="@menu/menu_navigation"
        />

    <!--Guide lines
        Item tint color: Applies color to icons of the navigation menu
        Value 54% black
        Item text color: Applies color to text in the navigation menu
        Value 87% black
    -->

</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private TextView mTextMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item_1:
                        // Code here
                        break;
                    case R.id.item_2:
                        // Code here
                        break;
                    case R.id.item_3:
                        // Code here
                        break;
                }
                return true;
            }
        });

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.drawer_open, R.string.drawer_closed);

        drawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();

    }

    private void hideDrawer(){
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    private void showDrawer(){
        drawerLayout.openDrawer(GravityCompat.START);
    }

    @Override
    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            hideDrawer();
        } else {
            super.onBackPressed();
        }
    }

}