Reputation: 3139
I need to implement such a layout :
So to put the ImageView with the source right behind the listView when Navigation Drawer is opens.
My xml :
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:background="@drawable/logo"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout android:layout_width="320dp" android:layout_height="fill_parent" android:orientation="horizontal"
android:id="@+id/drawer_list_layout" android:gravity="start">
<ListView android:id="@+id/left_drawer"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="start|center_vertical"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffffff"/>
<ImageView android:layout_width="wrap_content" android:id ="@+id/drawer_logo"
android:layout_height="wrap_content"
android:src="@drawable/logo"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Implementation in the activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawer_list_layout = (LinearLayout)findViewById(R.id.drawer_list_layout);
....
But app crashes in the method selectItem :
private void selectItem(int position) {
// Update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MapFragment();
break;
case 1:
fragment = new RegisterFragment();
break;
case 2:
fragment = new AboutProjectFragment();
break;
default:
break;
}
// Insert the fragment by replacing any existing fragment
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mScreenTitles[position]);
mDrawerLayout.closeDrawer(drawer_list_layout); // - the line of crash.
} else {
// Error
}
}
My onPrepareOptionsMenu
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(drawer_list_layout);
menu.findItem(R.id.action_search).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
So ,refers to this, this answers, which are similar to mine and implemented all the advices, I still get the trouble.
My stack :
Caused by: java.lang.IllegalArgumentException: View android.widget.LinearLayout{428d39a8 V.E..... ......I. 0,0-0,0 #7f05008d app:id/drawer_list_layout} is not a sliding drawer
at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1170)
at com.redonbas.MainActivity.selectItem(MainActivity.java:174)
at com.redonbas.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:5231)
Besides , I rebuilt the project - so it is not the case.
Experienced people just give a suggestion how can I implement such a feature in the app and what the reason of the crash.
Upvotes: 0
Views: 2060
Reputation: 56
I once encountered the problem, and changing it from android:gravity="start"
to android:layout_gravity="start"
solved it.
Upvotes: 3
Reputation: 2549
I got the same problem, and I've implemented both
android:gravity="left"
and
mDrawerLayout.closeDrawer(Gravity.LEFT);
Last, I changed the LayoutParams in code, It works:
DrawerLayout.LayoutParams layoutParams = new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, Gravity.LEFT);
mDrawerLayout.updateViewLayout(linearLayout, layoutParams);
Upvotes: 2
Reputation: 28823
Caused by: java.lang.IllegalArgumentException: View android.widget.LinearLayout{428d39a8 V.E..... ......I. 0,0-0,0 #7f05008d app:id/drawer_list_layout} is not a sliding drawer
Replace
boolean drawerOpen = mDrawerLayout.isDrawerOpen(drawer_list_layout);
with
boolean drawerOpen = mDrawerLayout.isDrawerOpen(Gravity.LEFT);
OR
change LinearLayout's gravity to LEFT instead of START.
<LinearLayout android:layout_width="320dp"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/drawer_list_layout"
android:gravity="left">
Hope it helps.
Upvotes: 0