I am using NavigationDrawer in my Android app, I have made my own custom layouts XML file and their adapters also but When I run my program then my application
Your ListView must specify the android:layout_gravity attribute in the xml so it can be identified as sliding drawer by the DrawerLayout.
Use right, left, start, end as values.
While using
DrawerLayoutmake sure it is the rootLayout. don't make it a child ofLinearLayoutorRelativeLayout.
as closeDrawer come up with two override function first View and second Gravity
As normally it is close by Gravity. So in order to close drawer you must set
android:layout_gravity =""
and all android:layout_gravity must be unique in DrawerLayout
drawer.closeDrawer(GravityCompat.START)//this code close the drawer child with layout_gravity ="start".
and if you have some other view like linearLayout above listView give layout_gravity to that layout and remove it from listView. drawer close item on the basis of gravity
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
///drawer.closeDrawer(anyViewToClose);
drawer.closeDrawer(GravityCompat.START);//but it is better to do this using gravity.
See closeDrawer code
public void closeDrawer(@EdgeGravity int gravity, boolean animate) {
final View drawerView = findDrawerWithGravity(gravity);
if (drawerView == null) {
throw new IllegalArgumentException("No drawer view found with gravity "
+ gravityToString(gravity));
}
closeDrawer(drawerView, animate);
}
also see isDrawerView
boolean isDrawerView(View child) {
final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
ViewCompat.getLayoutDirection(child));
if ((absGravity & Gravity.LEFT) != 0) {
// This child is a left-edge drawer
return true;
}
if ((absGravity & Gravity.RIGHT) != 0) {
// This child is a right-edge drawer
return true;
}
return false;
}
I had same problem with tutorial of Android Hive. But after I found one magical solution.
Put android:layout_gravity="start" or android:layout_gravity="left" in your ListView
<ListView
android:id="@+id/listSlideMenu"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:background="@color/color_primary"/>
I got the same issue learn from androidhive perfectly working with gravity="start" but on gravity change to end it raise error