I've searched all over Google trying to find the answer but haven't been able to find an answer. I'm using the "Create Navigation Drawer Activity" template in Android Studio. How do I add my fragments to the Navigation Drawer?
Here's where I assume I add the fragments:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} 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;
}
Based on tutorials I've seen, I've tried adding the following in the if statements with no success:
fragment = new ItemFragment();
Related
This question already has answers here:
Android Navigation Drawer click Event Issue
(2 answers)
Closed 6 months ago.
How to handle menu item clicks on NavigationView in Main Activity?
NavigationView navigationView = findViewById(R.id.nav_view);
I am using "Navigation Drawer Activity" template from latest Android Studio (2021.2.1 Patch 2) as a start.
Thanks.
Here is what I did hope this helps:
DrawerLayout drawerLayout = view.findViewById(R.id.drawerLayout);
view.findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
And then it will call this function when drawer button is clicked to open the drawer
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.miItem1){
}
if (id == R.id.miItem2){
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.ProfileFragment, fragment);
transaction.commit();
//startActivity(new Intent(getActivity(), PopFragment.class));
}
if (id == R.id.miItem3){
profileOrCoverPhoto = "image";
showImagePicDialog();
}
if (id == R.id.miItem4){
showNamephoneupdate("name");
}
if (id == R.id.miItem5){
showPasswordChangeDailog();
}
if (id == R.id.miItem7){
firebaseAuth.signOut();
startActivity(new Intent(getActivity(), SplashScreen.class));
getActivity().finish();
}
if (id == R.id.miItem8){
showdeleteaccountdialog();
}
return true;
}
How to call an activity from the navigation drawer.
thia ia my navigation drawer:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I have an activity called ProfileActivity.java but i dont know how to call an activity through navigation drawer...help
Hello try this hope it may help you
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.RelativeLayout, homeFragment, homeFragment.getTag()).commit();
} else if (id == R.id.nav_Profile) {
//Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
startActivity(new Intent(YOUR_ACTIVITY,ProfileActivity.class));
} else if (id == R.id.nav_manage) {
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Hello i'm not an expert in android development. I'm trying to develop an application using the Navigation drawer template of Android Studio. So, i created a new project using this template. But when i run the program an click on a menu item, the view doesn't change. So i searched everywhere on internet i didn't see how i can handle this.
This is the code provided by 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 which menu's is cliked.
Create Gallery.Java and paste this code:
public class Gallery extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_gallery,null);
}
}
And in your navigation drawer file paste this code:
if (id == R.id.nav_gallery)
{
Fragment fragment = new Gallery();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.framelayout, fragment).commit();
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_downloads) {
Intent i = new Intent(this,DownloadPage.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_settings) {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) { // here i want to add fragment
Manage manage = new Manage();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.downfragment, manage, "Settings");
transaction.commit();
}
};
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
}
You don't need another View.OnClickListener for this, the onNavigationItemSelected() is already invoked when you click on the drawer item. So simply remove your listener code, like the following:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_downloads) {
Intent i = new Intent(this,DownloadPage.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_settings) {
Manage manage = new Manage();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.downfragment, manage, "Settings");
transaction.commit();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
}
I'm new to Android App developing. Today I tried to update my app to android new material design. So I used Android studio (1.4) navigation Drawer Activity. Problem is I cant understand how to use navigation bar to navigate between my actives. It different from online tutorials I seen. It dosen't use Fragments.
I can change names, icons .. etc the problem is I cant understand how to navigate between activities using navigation drawer ?
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;
}
I had the same problem but got fixed.
Follow the steps below:
1.open the "content_main.xml" file located in the "layout" folder.
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>
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;
In this case, this is better to use fragments, for each nav item, replace your current fragment in your main activity, it will be simple to do. See fragments section in android dev guide.
You can better use fragment not activity to add content on each navigation option.(after creating application using Navigation Drawer Activity option in Android Studio).
content_main.xml (assign id to this relative layout, 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 change fragments 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;
}