3
I created a new project in android studio and chose to use the navigationDrawer
, created my screens now I would like to call them. How do I implement this method and switch windows in menu of an application using the:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.servic) {
fragment = new Cronograma();
} else if (id == R.id.nav_camera) {
fragment = new Localizacao();
} else if (id == R.id.nav_gallery) {
fragment = new Sobre_aplicativo();
} else if (id == R.id.nav_share) {
finish();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrame, fragment);
transaction.addToBackStack(null);
transaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
ACTIVITY MAIN
<?xml version="1.0" encoding="utf-8"?>
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/nav_header_main"
app:menu="@menu/activity_main_drawer" />
CONTENT MAIN:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
APP BAR MAIN:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
ACTIVITY MAIN DRAWER
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item
android:id="@+id/servic"
android:icon="@drawable/servico"
android:title="Serviços" />
<item
android:id="@+id/nav_camera"
android:icon="@drawable/pref"
android:title="A prefeitura" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/local"
android:title="Localização" />
</group>
<item android:title="Informações">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/dev"
android:title="Sobre o aplicativo" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/exit"
android:title="Sair" />
</menu>
</item>
Joao was doing so:
int id = item.getItemId();
 Fragment fragment = null;
 if (id == R.id.nav_camera) {
 fragment = new Cronograma();
 } else if (id == R.id.nav_gallery) {
 fragment = new Localizacao();
 } else if (id == R.id.nav_manage) {
 fragment = new Sobre_aplicativo();
 } else if (id == R.id.nav_share) {
 finish();
 }
So the screen is appearing above the menu.– Carlos Diego
Sends as your xml of this layout.
– João Gouveia