I have created a navigation drawer activity with android studio template (Navigation Drawer Activity). Now in androidx & android studio 3.5.3 they have changed the implementation of navigation drawer activity. in the java class the implementation looks like
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);
And onCreateOptionsMenu take ContextMenu, View and ContextMenu.ContextMenuInfo instead of just Menu
#Override
public void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_drawer_main, menu);
}
But there is no onOptionsItemSelected or onContextItemSelected method. So my question is if I want to customize the drawer item click event, how can I do that? suppose I want to pass intentExtra to the fragment or I have to check variable value on specific item click.
Thanks in advance.
AndroidX Drawer Navigation implementation is quiet easy as before
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
setupNavigation();
}
private void setupNavigation() {
// link Toggle button with Drawer
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
{
public void onDrawerClosed(View view)
{
supportInvalidateOptionsMenu();
//drawerOpened = false;
}
public void onDrawerOpened(View drawerView)
{
supportInvalidateOptionsMenu();
//drawerOpened = true;
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
}
// Create Menu for NavigationView
Menu menu = navigationView.getMenu();
menu.add(category.getName());
menu.getItem(0).setIcon(category.getIcon());
menu.getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
drawerLayout.closeDrawers();
// TODO; Here's your sexy looking codes ;P must return false if you don't know what `true` do
return false;
}
});
}
I have done the job with addOnDestinationChangedListener. In this method you can switch for the ids, you have given in AppBarConfiguration.Builder method. In my implementation I'm hiding or showing a layout based on the click event.
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller,
#NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_home) {
//item with id nav_home found do your work
main_activity_layout.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Nav Home Pressed!", Toast.LENGTH_SHORT).show();
} else {
main_activity_layout.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "onDestChangeElse!", Toast.LENGTH_SHORT).show();
}
}
});
Related
This question already has an answer here:
how to change toolbar icon (hamburger icon) when using navigation drawer with jetpack navigation component
(1 answer)
Closed 2 years ago.
I've seen almost all the links related to it but they were not implemented according to my requirement. I want to replace this default icon with my custom drawer icon. So that's why I've to ask question here. Help me to change hamburger icon. Thanks!
Here is my code for Navigation Drawer.
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
ImageView backButton;
DrawerLayout drawer;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
backButton = header.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.START);
}
});
// 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)
.setOpenableLayout(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.main, 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 void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);//your icon here
try this
This question already has an answer here:
How is Android Studio Navigation Drawer navigating different Fragments?
(1 answer)
Closed 2 years ago.
I open a new Activity using Android template for Navigation Drawer Activity.
When I click on the default Navigation drawer it shows the different fragment.
I have changed the title of the options in Navigation Drawer.
But if I click on then, it doesn't take me to the Fragment.
I cannot find any itemSelected or onClick methods in the Main Activity of the template where I can change the code to show new Fragments.
Please help how can I adjust the code to get my own fragments.
'''
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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_order, R.id.nav_status)
.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.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
'''
//First set Navigation Item Selected listener
navigationView.setNavigationItemSelectedListener(this);
//then override the onNavigationItemSelected method like this..
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.home:
//do something...
break;
case R.id.about:
//do something...
break;
}
I am novice developer and I'm integrating default (ready-made) Navigation drawer in my app with androidx. I'm having problem with navigating fragments as it doesn't work when I click on the drawer menu items. There are two fragments which are linked with their id's and set onClick listener, but it doesn't work. It just shows the main fragment but doesn't navigate to other fragments.
I'm not familiar with default functions like onCreateOptionsMenu(Menu menu) and onSupportNavigateUp() which are created by itself when I created the default drawer activity, so I just comment these functions and by following a tutorial I implement the NavigationView.OnNavigationItemSelectedListener which gives me onNavigationItemSelected(#NonNull MenuItem item) function where I wrote the code for navigate fragments. But it doesn't worked and when I debug, it shows that the onNavigationItemSelected(#NonNull MenuItem item) is not called.
public class FindBarber extends AppCompatActivity implements.
NavigationView.OnNavigationItemSelectedListener {
private AppBarConfiguration mAppBarConfiguration;
ActionBarDrawerToggle toggle;
Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
int OPEN, CLOSE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_barber);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, OPEN, CLOSE);
drawer.addDrawerListener(toggle);
toggle.syncState();
// 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_profile, R.id.nav_kutit_wallet,
R.id.nav_terms_of_use, R.id.nav_rate_the_app, R.id.nav_contact_us)
.setDrawerLayout(drawer)
.build();*/
/* NavController navController = Navigation.findNavController(this, R.id.content_frame);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);*/
FragmentManager fmanager = getSupportFragmentManager();
fmanager.beginTransaction().replace(R.id.content_frame, new GMap()).commit();
}
/*#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.find_barber, menu);
return true;
}
*/
// #Override
/* public boolean onSupportNavigateUp() {
*//*NavController navController = Navigation.findNavController(this, R.id.content_frame);*//*
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}*/
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new GMap()).commit();
break;
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Profile()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
There is no error message nor any app crash occurs. When I click navigation drawer item for navigating any fragment, it doesn't show anything except the main default fragment which I show in screen.
Call loadFragment(new Fragment(),"My Fragment") onNavigationItemSelected
public void loadFragment(Fragment fragment, String title_name){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.customer_container,fragment,title_name);
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
fragmentTransaction.commit();
}
Please try this below line :
navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
I have used google's sample navigation activity. In that I have modified the navigation bar items. I want to start a new activity when an item is clicked.
I have add onNavigationItemClicked event as well, still when I click on the item nothing happens.
public class MainActivity extends AppCompatActivity{
private AppBarConfiguration mAppBarConfiguration;
private TextView name, email;
private DrawerLayout drawer;
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_my_printers:
Intent intent = new Intent(context, MyPrintersActivity.class);
startActivity(intent);
break;
case R.id.nav_settings:
Toast.makeText(context, "Not yet Implemented", Toast.LENGTH_SHORT).show();
}
return false;
}
});
View headerView = navigationView.getHeaderView(0);
name = headerView.findViewById(R.id.name);
email = headerView.findViewById(R.id.email);
name.setText(SessionClass.getUserInSession().getName());
email.setText(SessionClass.getUserInSession().getEmail());
// 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_my_printers, R.id.nav_settings)
.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.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
The line
NavigationUI.setupWithNavController(navigationView, navController);
Calls setNavigationItemSelectedListener internally to connect destinations to menu items. This overrides the OnNavigationItemSelectedListener view you've set.
Navigation supports <activity> destinations, allowing you to use the default setupWithNavController functionality out of the box. You'd add an activity destination to your graph that uses the same id as your menu item:
<activity
android:id="#+id/nav_my_printers"
android:name=".MyPrintersActivity"/>
Then the default setupWithNavController will call startActivity for you.
How to open Navigation Drawer when click on navigation drawer icon.
I want open Navigation Drawer like this
![Navigation Drawer][1]
I added a menu but still not showing
toggle = new ActionBarDrawerToggle(
DashBordActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
if (navigationView != null) {
setupDrawerContent(navigationView);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
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
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
//#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setupToolbar(String title) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(title);
toolbar.setTitleTextColor(Color.BLACK);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void setupDrawerContent(NavigationView navigationView) {
//revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawer.closeDrawers();
return true;
}
});
}
navigation drawer is not open when i click on the drawer how to solve this problem
is there any code is missing in my code.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(START);
}
});
Try this,
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="#string/import" />
</group>
Try this:
This tutorial is very help full like:-
Add the navigation drawer layout
set icon and text in drawer layout
I refer this link
https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
it is very helpful try this