Android Studio 1.4 Navigation Drawer

I am new to Android app development. Today I tried to upgrade my application to develop a new Android design. So I used Android Drawer Activity (1.4) for Android. The problem is that I cannot figure out how to use the navigation bar to navigate between my assets. This is different from the online tutorials I've seen. He does not use Fragments. I can change names, icons, etc. The problem is that I can’t understand how to navigate between actions using the navigation box?

Thank you

public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camara) { } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 
+7
android android-studio navigation-drawer
source share
3 answers

I had the same problem, but fixed.

Follow these steps:

1. Unzip the file "content_main.xml" located in the folder "layout".

2. Use the code below:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main" tools:context=".MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mainFrame"> </FrameLayout> </RelativeLayout> 
  1. go to the onNavigationItemSelected method:
  public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); Fragment fragment = new YourFragment(); if (id == R.id.nav_camara) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.mainFrame, fragment); ft.commit(); } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } //Close Drawer After Action DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; 
+7
source share

In this case, it is better to use fragments, for each nav element, replace your current fragment in its main activity, this will simply be done. See the Snippets section in the Android Developer's Guide.

0
source share

You can better use a non-active fragment to add content for each navigation option (after creating the application using the Activity Navigation Drawer function in Android Studio).

content_main.xml (assign id to this relative location, Example: android: id = "@ + id / relative_layout_for_fragment")

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.root.netutility.MainActivity" tools:showIn="@layout/app_bar_main" android:id="@+id/relative_layout_for_fragment"> <!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />--> </RelativeLayout> 

MainActivity.Java (You are changing snippets like this)

  @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); Fragment fragment = null; if (id == R.id.ping_tab) { fragment = new FirstFragment(); toolbar.setTitle("First"); } else if (id == R.id.traceroute_tab) { fragment = new SecondFragment(); toolbar.setTitle("Second"); // if you wan to change title }else if (id == R.id.nav_share) { fragment = new ThirdFragment(); } else if (id == R.id.nav_send) { fragment = new FourthFragment(); } if(fragment != null) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.relative_layout_for_fragment, fragment).commit(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 
0
source share

All Articles