Created
August 26, 2013 16:17
-
-
Save emil10001/6343343 to your computer and use it in GitHub Desktop.
Navigation Drawer for Android. This was done for a blog post: http://www.recursiverobot.com/post/59404388046/implementing-the-new-navigation-drawer-in-android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <android.support.v4.widget.DrawerLayout | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:id="@+id/drawer_layout" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <!-- The main content view --> | |
| <FrameLayout | |
| android:id="@+id/content_frame" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"/> | |
| <!-- The navigation drawer --> | |
| <ListView | |
| android:id="@+id/left_drawer" | |
| android:layout_width="240dp" | |
| android:layout_height="match_parent" | |
| android:layout_gravity="start" | |
| android:choiceMode="singleChoice" | |
| android:divider="@android:color/transparent" | |
| android:dividerHeight="0dp" | |
| android:background="#111"/> | |
| </android.support.v4.widget.DrawerLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="fill_parent" | |
| android:layout_height="wrap_content" | |
| android:text="New Text" | |
| android:id="@+id/textView"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends ActionBarActivity { | |
| private String[] mPlanetTitles; | |
| private DrawerLayout mDrawerLayout; | |
| private ListView mDrawerList; | |
| private CharSequence mTitle; | |
| private ActionBarDrawerToggle mDrawerToggle; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| mTitle = "test"; | |
| mPlanetTitles = new String[]{"one", "two", "three"}; | |
| mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | |
| mDrawerList = (ListView) findViewById(R.id.left_drawer); | |
| // Set the adapter for the list view | |
| mDrawerList.setAdapter(new ArrayAdapter<String>(this, | |
| R.layout.drawer_list_item, mPlanetTitles)); | |
| // Set the list's click listener | |
| mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); | |
| mDrawerToggle = new ActionBarDrawerToggle( | |
| this, /* host Activity */ | |
| mDrawerLayout, /* DrawerLayout object */ | |
| R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ | |
| R.string.drawer_open, /* "open drawer" description */ | |
| R.string.drawer_close /* "close drawer" description */ | |
| ) { | |
| /** Called when a drawer has settled in a completely closed state. */ | |
| public void onDrawerClosed(View view) { | |
| getActionBar().setTitle(mTitle); | |
| } | |
| /** Called when a drawer has settled in a completely open state. */ | |
| public void onDrawerOpened(View drawerView) { | |
| getActionBar().setTitle(mTitle); | |
| } | |
| }; | |
| // Set the drawer toggle as the DrawerListener | |
| mDrawerLayout.setDrawerListener(mDrawerToggle); | |
| getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
| getSupportActionBar().setHomeButtonEnabled(true); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.main, menu); | |
| return true; | |
| } | |
| @Override | |
| protected void onPostCreate(Bundle savedInstanceState) { | |
| super.onPostCreate(savedInstanceState); | |
| // Sync the toggle state after onRestoreInstanceState has occurred. | |
| mDrawerToggle.syncState(); | |
| } | |
| @Override | |
| public void onConfigurationChanged(Configuration newConfig) { | |
| super.onConfigurationChanged(newConfig); | |
| mDrawerToggle.onConfigurationChanged(newConfig); | |
| } | |
| @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 (mDrawerToggle.onOptionsItemSelected(item)) { | |
| return true; | |
| } | |
| // Handle your other action bar items... | |
| return super.onOptionsItemSelected(item); | |
| } | |
| /** | |
| * Swaps fragments in the main content view | |
| */ | |
| private void selectItem(int position) { | |
| Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show(); | |
| // Highlight the selected item, update the title, and close the drawer | |
| mDrawerList.setItemChecked(position, true); | |
| setTitle(mPlanetTitles[position]); | |
| mDrawerLayout.closeDrawer(mDrawerList); | |
| } | |
| @Override | |
| public void setTitle(CharSequence title) { | |
| mTitle = title; | |
| getSupportActionBar().setTitle(mTitle); | |
| } | |
| private class DrawerItemClickListener implements ListView.OnItemClickListener { | |
| @Override | |
| public void onItemClick(AdapterView parent, View view, int position, long id) { | |
| selectItem(position); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <string name="drawer_open">Open</string> | |
| <string name="drawer_close">Close</string> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment