I am trying to implement navigation drawer without any actionbar. I have a small layout at the top of main layout and it looks like below
I want that when i will click the button the navigation drawer will appear under the small(colored) layout. I have tried with some example but the drawer always appear like this
But i want that the navigation drawer will appear under the small layout instead of "from the top" . I want something like this:
How can i achieve that??
My tried example's xml file:
<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" >
<!-- The first child in the layout is for the main Activity UI -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#0f9d58" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/actionBar"
android:background="#0f9d58" >
<Button
android:id="@+id/actionBarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="Holy Operating Systems, Batdroid!"
android:textSize="24sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<!-- Side navigation drawer UI -->
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_below="@id/actionBar"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
And the main activity was :
public class MainActivity extends Activity {
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
Button actionBarButton;
Boolean buttonStateOpen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStateOpen=false;
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.navList);
addDrawerItems();
actionBarButton=(Button) findViewById(R.id.actionBarButton);
actionBarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(buttonStateOpen==false)
{
drawer.openDrawer(Gravity.LEFT);
buttonStateOpen=true;
}
else if(buttonStateOpen==true)
{
drawer.closeDrawer(Gravity.LEFT);
buttonStateOpen=false;
}
}
});
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
}
if You have Fixed height of your layout the you can set that much of margin from top to
ListView
use this method to calculate the height of your
RelativeLayout
then
then use this to set Runtime margin to
ListView
from topYes you can create a drawer layout without action bar, it is very possible, Just do these following steps:
From your activity call this method
showDrawer(Drawerlayout)
. And add this code in the xml for the view which shows the drawer layout:and find the fragment for navigation drawer in the activity using the below code:
Try to set the height and the width of the action bar =0dp from layout app_bar_.. so it will not effect on the code but will remove the action bar like this