Android studio navigation box with snippet or action

I am trying to develop an application using the navigation box template in Android Studio. So, I created a new project using this template. But when I run the program and click on the menu item, the view does not change. I searched everywhere on the Internet, but I did not see how I can handle this.

This is the code provided by Android Studio:

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; } 

What I want to achieve is to replace the current view with the appropriate view for the selected menu item.

Is Fragment the best way to do this, or do I need to create a different action for each menu item?

+5
source share
4 answers

In your case, adding a snippet would be a better solution.

create a fragment of BlankFragment.java

 public class BlankFragment extends Fragment { public BlankFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_blank, container, false); } } 

and create fragment_black.xml file

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.above_inc.shyam.drawer.BlankFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout> 

now replace your method

 public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); Fragment fragment = null; if (id == R.id.nav_camera) { // Handle the camera action fragment = new BlankFragment(); } 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) { } if (fragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 

add below code to your content_main.xml

  <?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.above_inc.shyam.drawer.MainActivity" tools:showIn="@layout/app_bar_main"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

you can also add an additional fragment to other parameters, the same as the camera in the above code

+17
source

A snippet will be the best way if you want your navigation box to be displayed on the next screen when you click on a menu item.

If you use activity, the navigation box will not be displayed unless you create BaseActivity, which extends the navigation box and uses it everywhere. In this case, you should also change the transition animation acitivty, because after clicking on the menu, which may look strange, a new action will appear.

+2
source

Right click on your project -> Create -> Snippet.

Edit the switch housing accordingly.

0
source

@Passiondroid answer about using fragments only if you want the navigation box in all actions to be enabled.

About triggering an action when a menu item is clicked, in one of my applications, I follow this,

 public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); switch(id) { case R.id.nav_profile: selection = 1; intent = new Intent("android.intent.action.WEB"); intent.putExtra("selection",selection); startActivity(intent); break; case R.id.nav_books: intent = new Intent("android.intent.action.WEBD"); selection = 2; intent.putExtra("selection",selection); startActivity(intent); break; case R.id.fav_quotes: intent = new Intent ("android.intent.action.SHOWFAVTES"); selection = 3; intent.putExtra("selection",selection); startActivity(intent); break; default: break; } 

To change and adjust the menu item, you can find "activity_main_drawer.xml" in the res-> menu.

And when you click on each of the menu items, you can start your activity according to your desire. Before using it, you declare actions in your AndroidManifest.xml file.

0
source

Source: https://habr.com/ru/post/1245297/


All Articles