Selamat siang, apa anda sudah makan siang? jika belum makanlah dulu. kalau sudah makan baru buka android studio dan ikuti postingan ini. Pada tutorial kali ini saya akan membagikan cara membuat sliding menu navigation drawer pada android studio. Pernahkah anda melihat Menu Sliding Navigation drawer ini, pernah, menu ini ada pada aplikasi android antara lain BBM, Google Play Store, Google maps, dll. Pada tutorial ini kita akan membuat menu sliding navigation drawer menggunakan sofware android studio. Jika tidak ada, anda bisa mendownload disini.

Untuk membuat menu ini, anda harus mengikuti betul-betul tutorial ini jangan sampai kelewatan langkah-langkahnya. Ok sekarang kita mulai membuat menu sliding navigation drawer. Pertama-tama anda harus membuat project baru dan ikuti langkah dibawah ini. Pada folder res/layout buatlah file xml dengan nama file yang telah saya buat dibawah ini kemudian copy dan paste kan kode tersebut, selanjutnya cek pesan error. Pada file activity_main.xml merupakan file xml layout tapilan awal aplikasi yang akan kita buat.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? xml version = "1.0" encoding = "utf-8" ?> 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.v4.widget.DrawerLayout > |
Selanjutnya file layout app_bar_main.xml yaitu layout yang akan mengatur tampilan pada menu bar dalam tampilan menu sliding navigation.
app_bar_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <? xml version = "1.0" encoding = "utf-8" ?> < android.support.design.widget.CoordinatorLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:fitsSystemWindows = "true" tools:context = "com.mr.navigationdrawer.activity.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 > < FrameLayout android:id = "@+id/frame" android:layout_width = "match_parent" android:layout_height = "match_parent" app:layout_behavior = "@string/appbar_scrolling_view_behavior" ></ FrameLayout > < android.support.design.widget.FloatingActionButton android:id = "@+id/fab" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "bottom|end" android:layout_margin = "@dimen/fab_margin" app:backgroundTint = "@color/colorPrimary" app:srcCompat = "@android:drawable/ic_dialog_email" /> </ android.support.design.widget.CoordinatorLayout > |
nav_header_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <? xml version = "1.0" encoding = "utf-8" ?> android:id = "@+id/view_container" android:layout_width = "match_parent" android:layout_height = "@dimen/nav_header_height" android:gravity = "bottom" android:orientation = "vertical" android:theme = "@style/ThemeOverlay.AppCompat.Dark" > < ImageView android:id = "@+id/img_header_bg" android:layout_width = "match_parent" android:layout_height = "match_parent" android:scaleType = "fitXY" android:src = "@color/colorPrimary" /> < LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" android:orientation = "vertical" android:padding = "@dimen/activity_horizontal_margin" > < ImageView android:id = "@+id/img_profile" android:layout_width = "@dimen/profile_width" android:layout_height = "@dimen/profile_height" android:paddingTop = "@dimen/nav_header_vertical_spacing" app:srcCompat = "@android:drawable/sym_def_app_icon" /> < TextView android:id = "@+id/name" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:paddingTop = "@dimen/nav_header_vertical_spacing" android:textAppearance = "@style/TextAppearance.AppCompat.Body1" /> < TextView android:id = "@+id/website" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ LinearLayout > </ RelativeLayout > |
menu_dot.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center_vertical" android:orientation = "vertical" > < LinearLayout android:layout_width = "6dp" android:layout_height = "6dp" android:background = "@drawable/bg_circle" ></ LinearLayout > </ LinearLayout > |
fragment_home.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.mr.navigationdrawer.fragment.HomeFragment" > < ImageView android:layout_width = "100dp" android:layout_height = "100dp" android:layout_gravity = "center" android:alpha = "0.3" android:src = "@drawable/ic_home_black_24dp" /> </ FrameLayout > |
fragment_photos.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.mr.navigationdrawer.fragment.PhotosFragment" > < ImageView android:layout_width = "100dp" android:layout_height = "100dp" android:layout_gravity = "center" android:alpha = "0.3" android:src = "@drawable/ic_photo_library_black_24dp" /> </ FrameLayout > |
activity_main_drawer.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <? xml version = "1.0" encoding = "utf-8" ?> < group android:checkableBehavior = "single" > < item android:id = "@+id/nav_home" android:icon = "@drawable/ic_home_black_24dp" android:title = "@string/nav_home" /> < item android:id = "@+id/nav_photos" android:icon = "@drawable/ic_photo_library_black_24dp" android:title = "@string/nav_photos" /> < item android:id = "@+id/nav_movies" android:icon = "@drawable/ic_local_movies_black_24dp" android:title = "@string/nav_movies" /> < item android:id = "@+id/nav_notifications" android:icon = "@drawable/ic_notifications_black_24dp" android:title = "@string/nav_notifications" /> < item android:id = "@+id/nav_settings" android:icon = "@drawable/ic_settings_black_24dp" android:title = "@string/nav_settings" /> </ group > < item android:title = "Other" > < menu > < item android:id = "@+id/nav_about_us" android:title = "@string/nav_about_us" /> < item android:id = "@+id/nav_privacy_policy" android:title = "@string/privacy_policy" /> </ menu > </ item > </ menu > |
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < item android:id = "@+id/action_logout" android:orderInCategory = "100" android:title = "@string/action_logout" app:showAsAction = "never" /> </ menu > |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | < resources > < string name = "app_name" >Navigation Drawer</ string > < string name = "navigation_drawer_open" >Open navigation drawer</ string > < string name = "navigation_drawer_close" >Close navigation drawer</ string > < string name = "openDrawer" >open_drawer</ string > < string name = "closeDrawer" >close_drawer</ string > <!-- Navigation Drawer menu labels --> < string name = "nav_home" >Import</ string > < string name = "nav_photos" >Gallery</ string > < string name = "nav_movies" >Slideshow</ string > < string name = "nav_notifications" >Tools</ string > < string name = "nav_settings" >Settings</ string > < string name = "nav_about_us" >About Us</ string > <!-- Toolbar titles when navigation menu item is selected --> < string-array name = "nav_item_activity_titles" > < item >Import</ item > < item >Gallery</ item > < item >Slideshow</ item > < item >Tools</ item > < item >Settings</ item > </ string-array > < string name = "activity_title_about_us" >About Us</ string > < string name = "activity_title_privacy_policy" >Privacy Policy</ string > < string name = "action_clear_all" >Clear All</ string > < string name = "action_logout" >Logout</ string > < string name = "action_mark_all_read" >Mark All as Read</ string > < string name = "privacy_policy" >Privacy Policy</ string > </ resources > |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | package com.mr.navigationdrawer.activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; 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; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.mr.navigationdrawer.R; import com.mr.navigationdrawer.fragment.HomeFragment; import com.mr.navigationdrawer.fragment.MoviesFragment; import com.mr.navigationdrawer.fragment.NotificationsFragment; import com.mr.navigationdrawer.fragment.PhotosFragment; import com.mr.navigationdrawer.fragment.SettingsFragment; public class MainActivity extends AppCompatActivity { private NavigationView navigationView; private DrawerLayout drawer; private View navHeader; private ImageView imgNavHeaderBg, imgProfile; private TextView txtName, txtWebsite; private Toolbar toolbar; private FloatingActionButton fab; // urls to load navigation header background image // and profile image // index to identify current nav menu item public static int navItemIndex = 0 ; // tags used to attach the fragments private static final String TAG_HOME = "Import" ; private static final String TAG_PHOTOS = "Gallery" ; private static final String TAG_MOVIES = "Slideshow" ; private static final String TAG_NOTIFICATIONS = "Tools" ; private static final String TAG_SETTINGS = "settings" ; public static String CURRENT_TAG = TAG_HOME; // toolbar titles respected to selected nav menu item private String [] activityTitles; // flag to load home fragment when user presses back key private boolean shouldLoadHomeFragOnBackPress = true ; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mHandler = new Handler(); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); navigationView = (NavigationView) findViewById(R.id.nav_view); fab = (FloatingActionButton) findViewById(R.id.fab); // Navigation view header navHeader = navigationView.getHeaderView( 0 ); txtName = (TextView) navHeader.findViewById(R.id.name); txtWebsite = (TextView) navHeader.findViewById(R.id.website); imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg); imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile); // load toolbar titles from string resources activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles); fab.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action" , Snackbar.LENGTH_LONG) .setAction( "Action" , null ).show(); } }); // load nav menu header data loadNavHeader(); // initializing navigation menu setUpNavigationView(); if (savedInstanceState == null ) { navItemIndex = 0 ; CURRENT_TAG = TAG_HOME; loadHomeFragment(); } } /*** * Load navigation menu header information * like background image, profile image * name, website, notifications action view (dot) */ private void loadNavHeader() { // name, website txtName.setText( "Mr R" ); txtWebsite.setText( "www.oplive-mr-r.blogspot.com" ); // showing dot next to notifications label navigationView.getMenu().getItem( 3 ).setActionView(R.layout.menu_dot); } /*** * Returns respected fragment that user * selected from navigation menu */ private void loadHomeFragment() { // selecting appropriate nav menu item selectNavMenu(); // set toolbar title setToolbarTitle(); // if user select the current navigation menu again, don't do anything // just close the navigation drawer if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null ) { drawer.closeDrawers(); // show or hide the fab button toggleFab(); return ; } // Sometimes, when fragment has huge data, screen seems hanging // when switching between navigation menus // So using runnable, the fragment is loaded with cross fade effect // This effect can be seen in GMail app Runnable mPendingRunnable = new Runnable() { @Override public void run() { // update the main content by replacing fragments Fragment fragment = getHomeFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); fragmentTransaction. replace (R.id.frame, fragment, CURRENT_TAG); fragmentTransaction.commitAllowingStateLoss(); } }; // If mPendingRunnable is not null, then add to the message queue if (mPendingRunnable != null ) { mHandler.post(mPendingRunnable); } // show or hide the fab button toggleFab(); //Closing drawer on item click drawer.closeDrawers(); // refresh toolbar menu invalidateOptionsMenu(); } private Fragment getHomeFragment() { switch (navItemIndex) { case 0 : // home HomeFragment homeFragment = new HomeFragment(); return homeFragment; case 1 : // photos PhotosFragment photosFragment = new PhotosFragment(); return photosFragment; case 2 : // movies fragment MoviesFragment moviesFragment = new MoviesFragment(); return moviesFragment; case 3 : // notifications fragment NotificationsFragment notificationsFragment = new NotificationsFragment(); return notificationsFragment; case 4 : // settings fragment SettingsFragment settingsFragment = new SettingsFragment(); return settingsFragment; default: return new HomeFragment(); } } private void setToolbarTitle() { getSupportActionBar().setTitle(activityTitles[navItemIndex]); } private void selectNavMenu() { navigationView.getMenu().getItem(navItemIndex).setChecked( true ); } private void setUpNavigationView() { //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { // This method will trigger on item Click of navigation menu @Override public boolean onNavigationItemSelected(MenuItem menuItem) { //Check to see which item was being clicked and perform appropriate action switch (menuItem.getItemId()) { //Replacing the main content with ContentFragment Which is our Inbox View; case R.id.home: navItemIndex = 0 ; CURRENT_TAG = TAG_HOME; break ; case R.id.nav_photos: navItemIndex = 1 ; CURRENT_TAG = TAG_PHOTOS; break ; case R.id.nav_movies: navItemIndex = 2 ; CURRENT_TAG = TAG_MOVIES; break ; case R.id.nav_notifications: navItemIndex = 3 ; CURRENT_TAG = TAG_NOTIFICATIONS; break ; case R.id.nav_settings: navItemIndex = 4 ; CURRENT_TAG = TAG_SETTINGS; break ; case R.id.nav_about_us: // launch new intent instead of loading fragment startActivity( new Intent(MainActivity. this , AboutUsActivity. class )); drawer.closeDrawers(); return true ; case R.id.nav_privacy_policy: // launch new intent instead of loading fragment startActivity( new Intent(MainActivity. this , PrivacyPolicyActivity. class )); drawer.closeDrawers(); return true ; default: navItemIndex = 0 ; } //Checking if the item is in checked state or not, if not make it in checked state if (menuItem.isChecked()) { menuItem.setChecked( false ); } else { menuItem.setChecked( true ); } menuItem.setChecked( true ); loadHomeFragment(); return true ; } }); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle( this , drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) { @Override public void onDrawerClosed(View drawerView) { // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank super .onDrawerClosed(drawerView); } @Override public void onDrawerOpened(View drawerView) { // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank super .onDrawerOpened(drawerView); } }; //Setting the actionbarToggle to drawer layout drawer.setDrawerListener(actionBarDrawerToggle); //calling sync state is necessary or else your hamburger icon wont show up actionBarDrawerToggle.syncState(); } @Override public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawers(); return ; } // This code loads home fragment when back key is pressed // when user is in other fragment than home if (shouldLoadHomeFragOnBackPress) { // checking if user is on other navigation menu // rather than home if (navItemIndex != 0 ) { navItemIndex = 0 ; CURRENT_TAG = TAG_HOME; loadHomeFragment(); return ; } } super .onBackPressed(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // show menu only when home fragment is selected if (navItemIndex == 0 ) { getMenuInflater().inflate(R.menu.main, menu); } // when fragment is notifications, load the menu created for notifications if (navItemIndex == 3 ) { getMenuInflater().inflate(R.menu.notifications, 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_logout) { Toast.makeText(getApplicationContext(), "Logout user!" , Toast.LENGTH_LONG).show(); return true ; } // user is in notifications fragment // and selected 'Mark all as Read' if (id == R.id.action_mark_all_read) { Toast.makeText(getApplicationContext(), "All notifications marked as read!" , Toast.LENGTH_LONG).show(); } // user is in notifications fragment // and selected 'Clear All' if (id == R.id.action_clear_notifications) { Toast.makeText(getApplicationContext(), "Clear all notifications!" , Toast.LENGTH_LONG).show(); } return super .onOptionsItemSelected(item); } // show or hide the fab private void toggleFab() { if (navItemIndex == 0 ) fab.show(); else fab.hide(); } } |
HomeFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package com.mr.navigationdrawer.fragment; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.mr.navigationdrawer.R; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link HomeFragment.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link HomeFragment#newInstance} factory method to * create an instance of this fragment. */ public class HomeFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1" ; private static final String ARG_PARAM2 = "param2" ; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public HomeFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment HomeFragment. */ // TODO: Rename and change types and number of parameters public static HomeFragment newInstance( String param1, String param2) { HomeFragment fragment = new HomeFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); if (getArguments() != null ) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false ); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null ) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super .onAttach(context); // if (context instanceof OnFragmentInteractionListener) { // mListener = (OnFragmentInteractionListener) context; // } else { // throw new RuntimeException(context.toString() // + " must implement OnFragmentInteractionListener"); // } } @Override public void onDetach() { super .onDetach(); mListener = null ; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } |
PhotoFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package com.mr.navigationdrawer.fragment; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.mr.navigationdrawer.R; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link PhotosFragment.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link PhotosFragment#newInstance} factory method to * create an instance of this fragment. */ public class PhotosFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1" ; private static final String ARG_PARAM2 = "param2" ; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public PhotosFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment PhotosFragment. */ // TODO: Rename and change types and number of parameters public static PhotosFragment newInstance( String param1, String param2) { PhotosFragment fragment = new PhotosFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); if (getArguments() != null ) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_photos, container, false ); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null ) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super .onAttach(context); // if (context instanceof OnFragmentInteractionListener) { // mListener = (OnFragmentInteractionListener) context; // } else { // throw new RuntimeException(context.toString() // + " must implement OnFragmentInteractionListener"); // } } @Override public void onDetach() { super .onDetach(); mListener = null ; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } |
1 2 3 4 5 6 7 8 9 10 11 | dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.0.0-beta1' compile 'com.android.support:design:24.0.0-beta1' compile 'com.android.support:support-v4:24.0.0-beta1' testCompile 'junit:junit:4.12' } |
Jika gradle success dan tidak ada error, maka aplikasi akan berjalan lancar seperti yang kita harapkan. pada tutorial ini saya telah berhasih membuat menu sliding navigation drawer. jika saya berhasil kenapa anda tidak? OK demikian sedikit tutorial mengenai cara membuat menu sliding navigation drawer pada android studio.sampai jumpa...


Demikianlah Tutorial cara membuat menu sliding navigation drawer pada android studio.
Artikelnya bagus Bro..mudah-mudahan aq bisa buat aplikasinya. thanks
ReplyDeletegan boleh lihat coding yang dimens nya
ReplyDelete