Wednesday, 23 August 2017

DoubleSideDrawer Android Example

activity_main.xml


<?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:openDrawer="start">

    <include        
      layout="@layout/app_bar_main"        
      android:layout_width="match_parent"        
      android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView        
       android:id="@+id/nav_view"        
       android:layout_width="wrap_content"         
       android:layout_height="match_parent"        
       android:layout_gravity="start"   
       android:fitsSystemWindows="true"  
       app:headerLayout="@layout/nav_header_main"   
       app:menu="@menu/activity_main_drawer" />

    <android.support.design.widget.NavigationView     
        android:id="@+id/nav_right_view"  
        android:layout_width="wrap_content"  
        android:layout_height="match_parent"    
        android:layout_gravity="end"  
        android:fitsSystemWindows="true"   
        app:headerLayout="@layout/nav_header_main"  
        app:menu="@menu/activity_right_drawer" />
    </android.support.v4.widget.DrawerLayout>




app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.doublesidedrawerexample.MainActivity">

    <android.support.design.widget.AppBarLayout      
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar          
            android:id="@+id/toolbar"      
            android:layout_width="match_parent"  
            android:layout_height="?attr/actionBarSize"   
            android:background="?attr/colorPrimary"  
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>


content_main.xml
<?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 
              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/content_main"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin" 
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              tools:context="com.doublesidedrawerexample.MainActivity"
              tools:showIn="@layout/app_bar_main">

    <TextView     
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content" 
        android:text="Hello World!" />
</RelativeLayout>


nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
               xmlns:app="http://schemas.android.com/apk/res-auto"
               android:layout_width="match_parent"
               android:layout_height="@dimen/nav_header_height"
               android:background="@drawable/side_nav_bar"
               android:gravity="bottom"
               android:orientation="vertical"
               android:paddingBottom="@dimen/activity_vertical_margin"
               android:paddingLeft="@dimen/activity_horizontal_margin"
               android:paddingRight="@dimen/activity_horizontal_margin"
               android:paddingTop="@dimen/activity_vertical_margin"
               android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>



activity_main_drawer.xml
<?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/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item            
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>


activity_right_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="Account">
            <menu>
                <item
                    android:id="@+id/nav_settings"
                    android:icon="@drawable/ic_menu_share"
                    android:title="Settings" />
                <item
                    android:id="@+id/nav_logout"
                    android:icon="@drawable/ic_menu_send"
                    android:title="Logout" />
            </menu>
        </item>

        <item
            android:id="@+id/nav_help"
            android:icon="@drawable/ic_menu_camera"
            android:title="Help" />
        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_menu_gallery"
            android:title="About" />
    </group>

</menu>



main.xml
<?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/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_openRight"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:icon="@drawable/ic_menu_manage"
        app:showAsAction="always" />
</menu>


side_nav_bar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="135"
        android:centerColor="#4CAF50"
        android:endColor="#2E7D32"
        android:startColor="#81C784"
        android:type="linear" />
</shape>

MainActivity.class
package com.doublesidedrawerexample;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NavigationView navigationView,rightNavigationView;
    DrawerLayout drawer;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        rightNavigationView = (NavigationView) findViewById(R.id.nav_right_view);
        rightNavigationView.setNavigationItemSelectedListener(this);
    }

    @Override    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // 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;
        }
        if (id == R.id.action_openRight) {
            drawer.openDrawer(GravityCompat.END);
        /*Opens the Right Drawer*/
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.        
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action        
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }
        else if (id == R.id.nav_settings) {

        } else if (id == R.id.nav_logout) {

        } else if (id == R.id.nav_help) {

        } else if (id == R.id.nav_about) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}


build.gradle
compile 'com.android.support:design:24.2.1'

No comments:

Post a Comment