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;
}
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;
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am creating Navigation drawer activity .I have added one fragment Camera.
When I click in Profile fragment, I got error
" Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference" in this line. MainActivity.onNavigationItemSelected (MainActivity.java:104).
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass= null;
if (id == R.id.nav_camera) {
fragmentClass = Camera.class;
// 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) {
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activitymain.xml
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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" />
Your Fragment is null, and you are trying to replace that. try
In your Fragment class add this
public static MyFragment newInstance() {
MyFragment myFragment = new MyFragment();
return myFragment;
}
Then do this in your main activity:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout,
MyFragment.newInstance()).commit();
// 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;
}
In my android application, I had one main activity(i.e.Home) and two full-screen fragments. I can access theses three using Navigation Drawer. These two fragments act as the separate screen but actually its top on main activity.
In my case, I clicked the first fragment then I clicked the second fragment. During this time, I closed the first fragment and displayed the second fragment. But, main activity screen was now displayed one second during fragment transition.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out);
if(!item.isChecked())
getSupportFragmentManager().popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
item.setChecked(true);
if (previousMenuItem != item.getItemId()) {
if (item.getItemId() == R.id.fragment_two) {
initialiseFragmentTwo();
} else if (item.getItemId() == R.id.nav_main_activity) {
getSupportFragmentManager(). popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE);
getSupportActionBar().setTitle(R.string.app_name);
}
else if(item.getItemId() == R.id.nav_fragment_one){
initialiseFragmentOne();
}
}
previousMenuItem = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
Instead of using POP_BACK_STACK_INCLUSIVE try to use replace
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_gallery) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_slideshow) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_manage) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_share) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_send) {
fragmentClass = FragmentTwo.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
See here below links
http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity
https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/
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();
}
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();