In this tutorial I present my Android NavigationDrawer Example. I have been asked for this many times and I hope to clear everything up.
I’ll focus in on showing exactly how to define the items in the Navigation Drawer and then how to switch to different fragments when those items are clicked on. I cut out anything that you don’t need to understand so you can use it today.
If you like videos like this, it helps to tell Google Plus with a click here [googleplusone]
If you need to learn more about Fragments I have my Android Fragment tutorial here.
Code Snippets From the Video
I abbreviated the code so it will be very easy to make changes in your own code here.
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 |
---------- fragment_about_me.xml ---------- Do the same for the other fragment layouts, but change fragment_contact_info.xml : android:text="Contact Information Fragment" fragment_my_company.xml : android:text="My Company Fragment" <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="About Me Fragment" android:padding="5dp" android:textSize="16sp"/> </LinearLayout> ---------------------------------- ---------- AboutMeFragment.java ---------- Do the same for the other Fragments except ContactInfoFragment.java : ((MainActivity) activity).onSectionAttached(2); MyCompanyFragment.java : ((MainActivity) activity).onSectionAttached(3); and change all occurances of AboutMeFragment to ContactInfoFragment or MyCompanyFragment import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class AboutMeFragment extends Fragment { /** * Returns a new instance of this fragment for the given section number. */ public static AboutMeFragment newInstance() { AboutMeFragment fragment = new AboutMeFragment(); return fragment; } public AboutMeFragment () { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_about_me, container, false); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).onSectionAttached(1); } } ---------------------------------- ---------- strings.xml ---------- <string name="title_section1">About Me</string> <string name="title_section2">Contact Information</string> <string name="title_section3">My Company</string> ---------------------------------- ---------- MainActivity.java ---------- // Changes the fragment when you click on a Navigation Drawer item @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); // NEW STUFF if(position == 0){ fragmentManager.beginTransaction() .replace(R.id.container, AboutMeFragment.newInstance()) .commit(); } else if (position == 1){ fragmentManager.beginTransaction() .replace(R.id.container, ContactInfoFragment.newInstance()) .commit(); } else if (position == 2){ fragmentManager.beginTransaction() .replace(R.id.container, MyCompanyFragment.newInstance()) .commit(); } } // Gets the title of the current fragment public void onSectionAttached(int number) { switch (number) { case 1: mTitle = getString(R.string.title_section1); break; case 2: mTitle = getString(R.string.title_section2); break; case 3: mTitle = getString(R.string.title_section3); break; } } @SuppressWarnings("deprecation") // Changes the title of the page to the current fragments title public void restoreActionBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } -------------------------------------------------- ---------- NavigationDrawerFragment.java ---------- mDrawerListView.setAdapter(new ArrayAdapter<String>( getActionBar().getThemedContext(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, new String[]{ getString(R.string.title_section1), getString(R.string.title_section2), getString(R.string.title_section3), })); |
Leave a Reply