Introduction
A sliding drawer is the same as a custom ListView or a simple ListView. The only difference is that a Sliding Drawer remains hidden in the start. It only appears when we slide it out. After clicking on an item, it hides again. Today I will show a demo of a sliding drawer.
Now we move to the code part of this article.
In this project, I used two activities. Our first activity is activity_main activity_main.xml. The XML code of this activity is,
- <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">
- <FrameLayout
- android:id="@+id/content_frame"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <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="#fff"/>
- </android.support.v4.widget.DrawerLayout>

- <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">
- </android.support.v4.widget.DrawerLayout>

The second Activity is menu_detail_fragment.xml. The following is the XML code of this activity,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:gravity="center"
- android:background="#5ba4e5"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="40px"
- android:textColor="#ffffff"
- android:layout_gravity="center"
- android:id="@+id/detail"/>
- </LinearLayout>
Graphical Layout

- public class MainActivity extends Activity {
- String[] menu;
- DrawerLayout dLayout;
- ListView dList;
- ArrayAdapter < String > adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- menu = new String[] {
- "Home", "Android", "Windows", "Linux", "Raspberry Pi", "WordPress", "Videos", "Contact Us"
- };
- dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- dList = (ListView) findViewById(R.id.left_drawer);
- adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, menu);
- dList.setAdapter(adapter);
- dList.setSelector(android.R.color.holo_blue_dark);
- dList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
- dLayout.closeDrawers();
- Bundle args = new Bundle();
- args.putString("Menu", menu[position]);
- Fragment detail = new DetailFragment();
- detail.setArguments(args);
- FragmentManager fragmentManager = getFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
- }
- });
- }
- }
- String[] menu;
- DrawerLayout dLayout;
- ListView dList;
- ArrayAdapter<String> adapter;
- menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
- dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- dList = (ListView) findViewById(R.id.left_drawer);
- adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
- dList.setAdapter(adapter);
- dList.setSelector(android.R.color.holo_blue_dark);
- dList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
- dLayout.closeDrawers();
- Bundle args = new Bundle();
- args.putString("Menu", menu[position]);
- Fragment detail = new DetailFragment();
- detail.setArguments(args);
- FragmentManager fragmentManager = getFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
- }
- });
- public class DetailFragment extends Fragment {
- TextView text;
- @Override
- public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
- View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
- String menu = getArguments().getString("Menu");
- text= (TextView) view.findViewById(R.id.detail);
- text.setText(menu);
- return view;
- }
- }
In the preceding code we use an inflate method of the LayoutInflater class. The LayoutInflater class dynamically adds or removes items for a Layout. The Inflate method reads an XML file and converts the contents of the XML file into a View type object. Using the getString method we retrieve the data passed from the MainActivity class. We assign this value to the TextView and finally return the View.






Yashwanth MuthineniPosted Aug 27, 2015, 1:55 AM EDT
Nice Share
Santhakumar MunuswamyPosted Jul 30, 2015, 6:56 AM EDT
Good article. Thanks for sharing
Neeraj KumarPosted Jul 29, 2015, 12:00 PM EDT
Good Article
Pankaj Kumar ChoudharyTOP 100Posted Jul 29, 2015, 8:04 AM EDT
Thanks Gopi Sir..............
Gopi ChandPosted Jul 29, 2015, 4:59 AM EDT
Nice effort
Pankaj Kumar ChoudharyTOP 100Posted Jul 29, 2015, 3:08 AM EDT
Thanks Nilesh Sir.............
Nilesh JadavPosted Jul 29, 2015, 2:47 AM EDT
Nice article sir
Pankaj Kumar ChoudharyTOP 100Posted Jul 29, 2015, 2:13 AM EDT
Thanks Sibeesh Sir.....
Sibeesh VenuTOP 100Posted Jul 29, 2015, 2:08 AM EDT
Nice Share.
Rajeesh MenothPosted Jul 29, 2015, 2:03 AM EDT
Good One...