Chinmay Dabke Chinmay Dabke - 4 months ago 47
Android Question

Navigation Drawer without Actionbar

I searched many sites (stackoverflow as well) but couldn't understand how to implement the navigation drawer without the action bar. I know that this question has already been asked here but it doesn't have a proper explanation and code. I am a beginner in android development so can anyone please explain me with code How to make a navigation drawer without an action bar

Thanks in advance!

Answer

Hi,There is no relation between navigation drawer and action bar try this code it will help you.

<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" >

<FrameLayout
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView
    android:id="@+id/drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    android:choiceMode="singleChoice"/>

java code is here

public class MainActivity extends Activity {
final String[] data ={"one","two","three"};

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

     final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
     final ListView navList = (ListView) findViewById(R.id.drawer);
     navList.setAdapter(adapter);
     navList.setOnItemClickListener(new OnItemClickListener(){
             @Override
             public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                     drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                             @Override
                             public void onDrawerClosed(View drawerView){
                                     super.onDrawerClosed(drawerView);

                             }
                     });
                     drawer.closeDrawer(navList);
             }
     });
    }
}