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
    • 160
    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 -

    1. public class HomeScreenActivity extends AppCompatActivity {
    2. private LinearLayout llContactUs, llLogout, llOfflineMode;
    3. private FrameLayout frameLayout;
    4. private ActionBarDrawerToggle drawerToggle;
    5. private DrawerLayout drawerLayout;
    6. private LinearLayout left_drawer;
    7. private SearchView mSearchView;
    8.  
    9. protected void onCreate(Bundle savedInstanceState) {
    10. super.onCreate(savedInstanceState);
    11. setContentView(R.layout.activity_home_screen);
    12.  
    13. frameLayout = (FrameLayout) findViewById(R.id.content_frame);
    14. drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    15. llContactUs = (LinearLayout) findViewById(R.id.llContactUs);
    16. llLogout = (LinearLayout) findViewById(R.id.llLogout);
    17. left_drawer = (LinearLayout) findViewById(R.id.left_drawer);
    18. llOfflineMode = (LinearLayout) findViewById(R.id.llOfflineMode);
    19.  
    20. getSupportActionBar().setHomeButtonEnabled(true);
    21. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    22.  
    23. drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.draweropen, R.string.drawerclose) {
    24. @Override
    25. public void onDrawerClosed(View drawerView) {
    26. super.onDrawerClosed(drawerView);
    27. }
    28.  
    29. @Override
    30. public void onDrawerOpened(View drawerView) {
    31. super.onDrawerOpened(drawerView);
    32. }
    33.  
    34. @Override
    35. public void onDrawerSlide(View drawerView, float slideOffset) {
    36. super.onDrawerSlide(drawerView, slideOffset);
    37. frameLayout.setTranslationX(slideOffset * drawerView.getWidth());
    38. drawerLayout.bringChildToFront(drawerView);
    39. drawerLayout.requestLayout();
    40. }
    41. };
    42.  
    43. drawerToggle.setDrawerIndicatorEnabled(true);
    44. drawerLayout.setDrawerListener(drawerToggle);
    45.  
    46. llOfflineMode.setOnClickListener(new View.OnClickListener() {
    47. @Override
    48. public void onClick(View v) {
    49. Intent intent = new Intent(HomeScreenActivity.this, OfflinePublicationListActivity.class);
    50. intent.putExtra("offline",true);
    51. startActivity(intent);
    52. }
    53. });
    54.  
    55. llContactUs.setOnClickListener(new View.OnClickListener() {
    56. @Override
    57. public void onClick(View v) {
    58. Intent intent = new Intent(HomeScreenActivity.this, ContactusActivity.class);
    59. startActivity(intent);
    60. }
    61. });
    62.  
    63. llLogout.setOnClickListener(new View.OnClickListener() {
    64. @Override
    65. public void onClick(View v) {
    66. SharedPreferenceManager preferenceManager = SharedPreferenceManager.getInstance();
    67. preferenceManager.clearCredentials(HomeScreenActivity.this);
    68. preferenceManager.setLoginCheck(HomeScreenActivity.this, false);
    69. Intent intent = new Intent(HomeScreenActivity.this, LoginActivity.class);
    70. startActivity(intent);
    71. finish();
    72. }
    73. });
    74.  
    75. addFirstFragment();
    76. }
    77.  
    78. @Override
    79. protected void onPostCreate(Bundle savedInstanceState) {
    80. super.onPostCreate(savedInstanceState);
    81. // Sync the toggle state after onRestoreInstanceState has occurred.
    82. drawerToggle.syncState();
    83. }
    84.  
    85. @Override
    86. public void onConfigurationChanged(Configuration newConfig) {
    87. super.onConfigurationChanged(newConfig);
    88. // Pass any configuration change to the drawer toggles
    89. drawerToggle.onConfigurationChanged(newConfig);
    90. }
    91.  
    92. /**
    93. @Function Name : addFirstFragment
    94. @Author Name : Mr. Sombir Singh Bisht
    95. @Date : Aug, 25 2015
    96. @Param : NA
    97. @Purpose : To add fragment to the Home Screen Activity.
    98. */
    99. private void addFirstFragment() {
    100. FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    101. SelectRegionFragment selectRegionFragment = new SelectRegionFragment();
    102. fragmentTransaction.add(R.id.content_frame, selectRegionFragment);
    103. fragmentTransaction.commit();
    104. }
    105.  
    106. public boolean onCreateOptionsMenu(Menu menu) {
    107. // Inflate the menu; this adds items to the action bar if it is present.
    108. getMenuInflater().inflate(R.menu.menu_home_screen, menu);
    109.  
    110. mSearchView=(SearchView)menu.findItem(R.id.search).getActionView();
    111.  
    112. initializeSearchView();
    113. return true;
    114. }
    115.  
    116. @Override
    117. public boolean onPrepareOptionsMenu(Menu menu) {
    118. return super.onPrepareOptionsMenu(menu);
    119. }
    120.  
    121. private void initializeSearchView(){
    122. mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    123. @Override
    124. public boolean onQueryTextSubmit(String query) {
    125.  
    126. System.out.println("This is the text to submit"+query);
    127. searchText(query);
    128. return false;
    129. }
    130.  
    131. @Override
    132. public boolean onQueryTextChange(String newText) {
    133. return false;
    134. }
    135. });
    136. }
    137.  
    138. private void searchText(String query){
    139. Intent intent = new Intent(this, PublicationsListActivity.class);
    140. intent.putExtra("query",query);
    141. startActivity(intent);
    142. }
    143.  
    144. @Override
    145. public boolean onOptionsItemSelected(MenuItem item) {
    146. // Pass the event to ActionBarDrawerToggle, if it returns
    147. // true, then it has handled the app icon touch event
    148. if (drawerToggle.onOptionsItemSelected(item)) {
    149. return true;
    150. }
    151. // Handle your other action bar items...
    152.  
    153. return super.onOptionsItemSelected(item);
    154. }
    155. }

    Step(2)-HomeScreenActivity layout-

    1. <android.support.v4.widget.DrawerLayout
    2. xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:id="@+id/drawer_layout"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent">
    6.  
    7. <!-- Frame Layout -->
    8. <FrameLayout
    9. android:id="@+id/content_frame"
    10. android:layout_width="match_parent"
    11. android:layout_height="match_parent">
    12.  
    13. </FrameLayout>
    14.  
    15. <!-- Navigation drawer -->
    16. <LinearLayout
    17. android:id="@+id/left_drawer"
    18. android:layout_width="@dimen/drawer_width"
    19. android:layout_height="match_parent"
    20. android:layout_gravity="start"
    21. android:background="@color/left_drawer_backgroundcolor"
    22. android:orientation="vertical">
    23.  
    24. <LinearLayout
    25. android:id="@+id/llMyFavourites"
    26. android:layout_width="match_parent"
    27. android:layout_height="wrap_content"
    28. android:orientation="vertical"
    29. android:visibility="gone">
    30.  
    31. <ImageView
    32. android:layout_width="wrap_content"
    33. android:layout_height="wrap_content"
    34. android:layout_marginBottom="@dimen/ivmargin_bottom"
    35. android:src="@drawable/markfav"
    36. android:layout_gravity="center_horizontal" />
    37.  
    38. <TextView
    39. android:layout_width="wrap_content"
    40. android:layout_height="wrap_content"
    41. android:text="@string/tvMyFavourites_text"
    42. android:textSize="@dimen/tvtext_size"
    43. android:textColor="@color/tvtext_color"
    44. android:layout_gravity="center_horizontal"
    45. android:gravity="center_horizontal"/>
    46.  
    47. </LinearLayout>
    48.  
    49. <LinearLayout
    50. android:id="@+id/llOfflineMode"
    51. android:layout_width="match_parent"
    52. android:layout_height="wrap_content"
    53. android:orientation="vertical"
    54. android:textAlignment="center"
    55. android:layout_marginTop="@dimen/addtoOffline_margintop">
    56.  
    57. <ImageView
    58. android:layout_width="wrap_content"
    59. android:layout_height="wrap_content"
    60. android:layout_marginBottom="@dimen/ivmargin_bottom"
    61. android:src="@drawable/mode"
    62. android:layout_gravity="center_horizontal"/>
    63.  
    64. <TextView
    65. android:layout_width="wrap_content"
    66. android:layout_height="wrap_content"
    67. android:text="@string/tvOfflineMode_text"
    68. android:textSize="@dimen/tvtext_size"
    69. android:textColor="@color/tvtext_color"
    70. android:textAlignment="center"
    71. android:layout_gravity="center_horizontal" />
    72.  
    73. </LinearLayout>
    74.  
    75. <LinearLayout
    76. android:id="@+id/llContactUs"
    77. android:layout_width="match_parent"
    78. android:layout_height="wrap_content"
    79. android:orientation="vertical"
    80. android:layout_marginTop="30dp">
    81.  
    82. <ImageView
    83. android:layout_width="wrap_content"
    84. android:layout_height="wrap_content"
    85. android:layout_marginBottom="@dimen/ivmargin_bottom"
    86. android:src="@drawable/contact"
    87. android:layout_gravity="center_horizontal"/>
    88.  
    89. <TextView
    90. android:layout_width="wrap_content"
    91. android:layout_height="wrap_content"
    92. android:text="@string/tvContactUs_text"
    93. android:textSize="@dimen/tvtext_size"
    94. android:textColor="@color/tvtext_color"
    95. android:layout_gravity="center_horizontal" />
    96. </LinearLayout>
    97.  
    98. <LinearLayout
    99. android:id="@+id/llMyAccount"
    100. android:layout_width="match_parent"
    101. android:layout_height="wrap_content"
    102. android:orientation="vertical"
    103. android:visibility="gone">
    104.  
    105. <ImageView
    106. android:layout_width="wrap_content"
    107. android:layout_height="wrap_content"
    108. android:layout_marginBottom="@dimen/ivmargin_bottom"
    109. android:src="@drawable/myaccount"
    110. android:layout_gravity="center_horizontal"/>
    111.  
    112. <TextView
    113. android:layout_width="wrap_content"
    114. android:layout_height="wrap_content"
    115. android:text="@string/tvMyAccount_text"
    116. android:textSize="@dimen/tvtext_size"
    117. android:textColor="@color/tvtext_color"
    118. android:layout_gravity="center_horizontal" />
    119.  
    120. </LinearLayout>
    121.  
    122. <LinearLayout
    123. android:id="@+id/llLogout"
    124. android:layout_width="match_parent"
    125. android:layout_height="wrap_content"
    126. android:orientation="vertical"
    127. android:layout_marginTop="30dp">
    128.  
    129. <ImageView
    130. android:layout_width="wrap_content"
    131. android:layout_height="wrap_content"
    132. android:layout_marginBottom="@dimen/ivmargin_bottom"
    133. android:src="@drawable/logout"
    134. android:layout_gravity="center_horizontal"/>
    135.  
    136. <TextView
    137. android:layout_width="wrap_content"
    138. android:layout_height="wrap_content"
    139. android:text="@string/tvLogout_text"
    140. android:textSize="@dimen/tvtext_size"
    141. android:textColor="@color/tvtext_color"
    142. android:layout_gravity="center_horizontal" />
    143.  
    144. </LinearLayout>
    145.  
    146. </LinearLayout>
    147.  
    148. </android.support.v4.widget.DrawerLayout>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: