I'm making an application in Android Studio, in which I use a NavigationDrawer. I have the problem when clicking on the button that would open the navigation menu does not, but it does allow me to drag the navigation from left to right so that it can be seen. How could it be solved and why does it not call the listener? The activity has two fragments. The layout is as follows:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:showDividers="middle" >
<FrameLayout
android:id="@+id/listar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/detalles"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/cabecera"
app:menu="@layout/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
The relevant code of the activity is the following:
public DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar appbar = (Toolbar)findViewById(R.id.appbar);
setSupportActionBar(appbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_action);
ab.setDisplayHomeAsUpEnabled(true);
if (findViewById(R.id.detalles) != null) {
mTwoPanes = true;
if (savedInstanceState == null) {
Details fragment = Details.newInstance(null, null);
getSupportFragmentManager().beginTransaction().add(R.id.detalles, fragment).commit();
}
}
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
prepararDrawer(navigationView);
if (savedInstanceState != null) {
List fragmentoGenerico = new List();
getSupportFragmentManager().beginTransaction().replace(R.id.lista, fragmentoGenerico).commit();
}else {
seleccionarItem(navigationView.getMenu().getItem(0).getSubMenu().getItem(0));
}
}
}
private void prepararDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
seleccionarItem(menuItem);
drawerLayout.closeDrawers();
return true;
}
});
}
The following method does not get called, so it does not open the drawer:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
How could it be solved?