Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open the Navigation drawer by tapping menu icon of Action Bar.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 313
    Comment on it

    Navigation drawer partially covers the screen when user swipes from left to right or eigter clicking on the menu icon at the top-left of the action bar. Navigation drawer is basically the hidden place where you can put navigation options.

    activity_main.xml

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- Frame Layout -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/top1">
    
        </FrameLayout>
    
        <!-- Navigation drawer -->
        <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/drawe_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/left_drawer_backgroundcolor"
            android:orientation="vertical">
    
            <LinearLayout
                android:id="@+id/llMyFavourites"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/ivmargin_bottom"
                    android:src="@drawable/markfav"
                    android:layout_gravity="center_horizontal" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tvMyFavourites_text"
                    android:textSize="@dimen/tvtext_size"
                    android:textColor="@color/tvtext_color"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"/>
    
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/llOfflineMode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:textAlignment="center"
                android:layout_marginTop="@dimen/addtoOffline_margintop">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/ivmargin_bottom"
                    android:src="@drawable/mode"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tvOfflineMode_text"
                    android:textSize="@dimen/tvtext_size"
                    android:textColor="@color/tvtext_color"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal" />
    
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/llContactUs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="30dp">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/ivmargin_bottom"
                    android:src="@drawable/contact"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tvContactUs_text"
                    android:textSize="@dimen/tvtext_size"
                    android:textColor="@color/tvtext_color"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/llMyAccount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="10dp">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/ivmargin_bottom"
                    android:src="@drawable/myaccount"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tvMyAccount_text"
                    android:textSize="@dimen/tvtext_size"
                    android:textColor="@color/tvtext_color"
                    android:layout_gravity="center_horizontal" />
    
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/llLogout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="30dp">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/ivmargin_bottom"
                    android:src="@drawable/logout"
                    android:layout_gravity="center_horizontal"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tvLogout_text"
                    android:textSize="@dimen/tvtext_size"
                    android:textColor="@color/tvtext_color"
                    android:layout_gravity="center_horizontal" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
        private ActionBarDrawerToggle drawerToggle;
        private DrawerLayout drawerLayout;
        private FrameLayout frameLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            frameLayout = (FrameLayout) findViewById(R.id.content_frame);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    
            drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,R.string.opendrawer, R.string.closedrawer) {
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                }
    
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                }
    
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset) {
                    super.onDrawerSlide(drawerView, slideOffset);
                    frameLayout.setTranslationX(slideOffset * drawerView.getWidth());
                    drawerLayout.bringChildToFront(drawerView);
                    drawerLayout.requestLayout();
                }
            };
    
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            drawerLayout.setDrawerListener(drawerToggle);
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            drawerToggle.syncState();
        }
    
        @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.
            if (drawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: