Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Make Navigation Drawer in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 146
    Comment on it

    Using below code you can create a android DrawerLayout, a FrameLayout and Linearlayout. The Linerlayout is divided in three linerlayout for using image and text. In this Navigation Drawer when I will click navigation menu the navigation menu will open without hiding backgarund image. Backgraund imgae will sliding to right side and navigation menu will esily openup on the device. Below example will clearly describe how to make Navigation Drawer .

    Step(1)-Create HomeScreenActivity -

    public class HomeScreenActivity extends AppCompatActivity {
        private LinearLayout llContactUs, llLogout, llOfflineMode;
        private FrameLayout frameLayout;
        private ActionBarDrawerToggle drawerToggle;
        private DrawerLayout drawerLayout;
        private LinearLayout left_drawer;
        private SearchView mSearchView;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_screen);
    
            frameLayout = (FrameLayout) findViewById(R.id.content_frame);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            llContactUs = (LinearLayout) findViewById(R.id.llContactUs);
            llLogout = (LinearLayout) findViewById(R.id.llLogout);
            left_drawer = (LinearLayout) findViewById(R.id.left_drawer);
            llOfflineMode = (LinearLayout) findViewById(R.id.llOfflineMode);
    
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.draweropen, R.string.drawerclose) {
                @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();
                }
            };
    
            drawerToggle.setDrawerIndicatorEnabled(true);
            drawerLayout.setDrawerListener(drawerToggle);
    
            llOfflineMode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(HomeScreenActivity.this, OfflinePublicationListActivity.class);
                    intent.putExtra("offline",true);
                    startActivity(intent);
                }
            });
    
            llContactUs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(HomeScreenActivity.this, ContactusActivity.class);
                    startActivity(intent);
                }
            });
    
            llLogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferenceManager preferenceManager = SharedPreferenceManager.getInstance();
                    preferenceManager.clearCredentials(HomeScreenActivity.this);
                    preferenceManager.setLoginCheck(HomeScreenActivity.this, false);
                    Intent intent = new Intent(HomeScreenActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
    
            addFirstFragment();
        }
    
            @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            drawerToggle.syncState();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            // Pass any configuration change to the drawer toggles
            drawerToggle.onConfigurationChanged(newConfig);
        }
    
        /**
         @Function Name     :   addFirstFragment
         @Author Name       :   Mr. Sombir Singh Bisht
         @Date              :   Aug, 25 2015
         @Param             :   NA
         @Purpose           :   To add fragment to the Home Screen Activity.
         */
        private void addFirstFragment() {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            SelectRegionFragment selectRegionFragment = new SelectRegionFragment();
            fragmentTransaction.add(R.id.content_frame, selectRegionFragment);
            fragmentTransaction.commit();
        }
    
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_home_screen, menu);
    
            mSearchView=(SearchView)menu.findItem(R.id.search).getActionView();
    
            initializeSearchView();
            return true;
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            return super.onPrepareOptionsMenu(menu);
        }
    
        private void initializeSearchView(){
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
    
                    System.out.println("This is the text to submit"+query);
                    searchText(query);
                    return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
        }
    
        private void searchText(String query){
            Intent intent = new Intent(this, PublicationsListActivity.class);
            intent.putExtra("query",query);
            startActivity(intent);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Pass the event to ActionBarDrawerToggle, if it returns
            // true, then it has handled the app icon touch event
            if (drawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            // Handle your other action bar items...
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    Step(2)-HomeScreenActivity layout-

    <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">
    
        </FrameLayout>
    
        <!-- Navigation drawer -->
        <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/drawer_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"
                android:visibility="gone">
    
                <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:visibility="gone">
    
                <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>
    

 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: