I am using the android navigation drawer navigation menu from the Android Studio template. How do I navigate to the different activity based on the id.
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.admin_panel_navigation, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}
I tried the below set of codes, but it doesn't perform the click operation.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_maths: {
//do somthing
break;
}
}
//close navigation drawer
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
I know I am missing something, but I am not able to find the solution. Can anyone please help me.
As per the Tie destinations to menu items section, Navigation uses the IDs you add to your menu xml, matching them to destinations in your navigation graph xml file.
So if you had a menu item such as
<item
android:id="#+id/nav_maths"
android:icon="#drawable/maths"
android:title="#string/maths" />
and wanted it to start a different activity, you could add an <activity> destination to your navigation graph:
<activity
android:id="#+id/nav_maths"
android:name="com.your.package.MathsActivity" />
And because they have the same ID, your activity would be started when you click that item in your menu.
Note that Navigation focuses on having just a single activity, so an activity destination should be considered an exit point from your graph - your second activity would have its own navigation graph, etc. that is completely separate from the first one.
Related
i have one big problem i am a beginner and i don't know how to implement horizontal and vertical slide between fragments, i have around 100 main and 100 additional fragments for each of that fragment.
Can someone please explain to me the simplest way.
https://i.stack.imgur.com/5WHhD.png
My code :
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav01, R.id.nav02, R.id.nav03, R.id.nav04, R.id.nav05, R.id.nav06, R.id.nav07, R.id.nav08, R.id.nav09
, R.id.nav10, R.id.nav11, R.id.nav12, R.id.nav13, R.id.nav14, R.id.nav15, R.id.nav16, R.id.nav17, R.id.nav18, R.id.nav19, R.id.nav20, R.id.nav21
, R.id.nav22, R.id.nav23, R.id.nav24, R.id.nav25, R.id.nav26, R.id.nav27, R.id.nav28, R.id.nav29, R.id.nav30, R.id.nav31, R.id.nav32, R.id.nav33,
R.id.nav34, R.id.nav35, R.id.nav36, R.id.nav37, R.id.nav38, R.id.nav39, R.id.nav40, R.id.nav41, R.id.nav42, R.id.nav43, R.id.nav44, R.id.nav45,
R.id.nav46, R.id.nav47, R.id.nav48, R.id.nav49, R.id.nav50, R.id.nav51, R.id.nav52, R.id.nav53, R.id.nav54, R.id.nav55, R.id.nav56, R.id.nav57,
R.id.nav58, R.id.nav59, R.id.nav60, R.id.nav61, R.id.nav62, R.id.nav63, R.id.nav64, R.id.nav65, R.id.nav66, R.id.nav67, R.id.nav68, R.id.nav69,
R.id.nav70, R.id.nav71, R.id.nav72, R.id.nav73, R.id.nav74, R.id.nav75, R.id.nav76, R.id.nav77,R.id.nav78,R.id.nav79,R.id.nav80,R.id.nav81,R.id.nav82,
R.id.nav83,R.id.nav84, R.id.nav85, R.id.nav86,R.id.nav87,R.id.nav88,R.id.nav89,R.id.nav90,R.id.nav91,R.id.nav92,R.id.nav93,R.id.nav94,R.id.nav95,R.id.nav96,
R.id.nav97,R.id.nav98,R.id.nav99
)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I updated my andoid studio version. In updated android studio version how to give onclick listener to settings option. when i used onclick listener to the same the drawer menu is not responding.
Screenshot of layout
This is my code snippet of activity class
public class HomeActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = 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 = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
But it should not effects to the other menu options in drawer navigation
As per the Tie destinations to menu items documentation, you can launch a particular screen when your menu item by overriding onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}
Then make sure that the android:id of your menu item (for example, the action_settings in your screenshot) matches the android:id of a destination in your navigation graph XML pointing to the <fragment> or <activity> you're using for your settings.
This same concept of ensuring that your menu item android:id and navigation graph android:id match applies to all of the menus including those used in a navigation drawer (that's how the default template's NavigationView works out of the box).
You can try following source code in Activity
#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) {
switch (item.getItemId()) {
//item id
case R.id.logout:
//Creating a shared preference
SharedPreferences sp = getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sp.edit();
//Adding values to editor
editor.putString(Constant.ID_SHARED_PREF, "");
//Saving values to editor
editor.apply();
Intent intent2=new Intent(HomeActivity.this, UserLoginActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent2);
return true;
case R.id.settings:
Intent intent=new Intent(HomeActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onContextItemSelected(item);
}
}
I am working on an app with a side Navigation drawer. The drawer opens fine, however the text that supposedly can be "clickable" does not seem to respond. The animation shows that there is feedback to when the drawer is tapped (you can hear the sound) however nothing results of it. I have tried to place toast messages to see if the button registers an action, but when pressed, no toast appears.
The code goes as follows (I have implemented NavigationView.OnNavigationItemSelectedListener):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_history, R.id.nav_settings,
R.id.nav_help, R.id.nav_signout)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
And then I implemented the method:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_history:
Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
break;
case R.id.nav_help:
break;
case R.id.nav_settings:
break;
case R.id.nav_signout:
signOut();
break;
}
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Thank you
The line
NavigationUI.setupWithNavController(navigationView, navController);
Calls setNavigationItemSelectedListener internally to connect destinations to menu items (i.e., when you click on the R.id.nav_settings MenuItem, it'll replace the Fragment in your NavHostFragment with the one with android:id="#+id/nav_settings" set). This listener overrides the OnNavigationItemSelectedListener view you've set, which is why your custom logic doesn't run.
If you want to combine both sets of functionality together, you need to call navigationView.setNavigationItemSelectedListener(this); after setupWithNavController and trigger the default behavior with NavigationUI.onNavDestinationSelected():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_history, R.id.nav_settings,
R.id.nav_help, R.id.nav_signout)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
// This line needs to be after setupWithNavController()
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
switch (menuItem.getItemId()){
case R.id.nav_history:
Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
break;
case R.id.nav_signout:
signOut();
break;
default:
// Trigger the default action of replacing the current
// screen with the one matching the MenuItem's ID
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Just a small sample based on the accepted answer but inside the fragment, without overriding the annotation and using Kotlin.
bottomNav.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.share -> {
shareViaWhatsApp()
}
else -> {
NavigationUI.onNavDestinationSelected(it, navController!!)
}
}
true
}
I've a bottom navigation with 4 menu items.
If the user is in the home navigation fragment and he clicks on the home navigation item again, the fragment is getting recreated.
How do I disable the click for the current navigation menu item?
Here's my code for the navigation:
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_feed, R.id.navigation_profile, R.id.trips_feed).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
You need to manage this with switch case
So get the fragment that's now open
Then say if the fragment is open, never open again
example code :
Fragment fragment = null;
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for (Fragment currentFragment : fragments) {
switch (item.getItemId()) {
case R.id.navigation_home:
if (!(currentFragment instanceof HomeFragmentGeneral)) {
fragment = HomeFragmentGeneral.newInstance();
}
break;
case R.id.navigation_search:
if (!(currentFragment instanceof NearlyFrag)) {
fragment = NearlyFrag.newInstance();
}
break;
case R.id.navigation_profile:
if (!(currentFragment instanceof ProfileFragment)) {
fragment = ProfileFragment.newInstance();
}
break;
}
}
if (fragment != null) {
attachFragmentToActivity(fragment, R.id.frame);
}
I am trying to use the new Navigation components from Android jetpack to create a navigation drawer. For some reason, the corresponding burger button does show up on the screen but doesn't react to clicks at all.
I've tried several tutorials online but to no avail. The last thing I tried was the offcial approach from https://developer.android.com/guide/navigation/navigation-ui.
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupNavigation();
}
private void setupNavigation() {
drawer = findViewById(R.id.drawer_layout);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationView navView = findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navView, navController);
}
I don't understand why it isn't reacting to clicks at all.
As per the ActionBar section of that same page:
Next, override onSupportNavigateUp() to handle Up navigation:
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp()
}
Note that the AppBarConfiguration you create will need to be a variable at the class level as well so it can be used in both setupNavigation() and onSupportNavigateUp().