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.
Related
This is Main Activity Where I have created Fragments through mobile_navigation.xml
Now in Action Bar i have set 2 items one is search which is another activity which i have sent through intent.Now for cart item i want to send it to Cart fragment of the same Activity.How do i send it??
I am new to Development Please Help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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.nav_home, R.id.nav_my_archaka,R.id.nav_my_cart)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
parentframelayout = findViewById(R.id.parentframelayout);
}
#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_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.main_search) {
Intent searchintent = new Intent(this, SearchActivity.class);
startActivity(searchintent);
return true;
}
if (id == R.id.main_cart) {
////how to send to cart fragment of this same activity;
return true;
}
return super.onOptionsItemSelected(item);
}
}
Create an action in your navgraph and add this code on if() condition:
Navigation.findNavController(view).navigate(R.id.action_fragment1_to_fragment2);
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'm In trouble and I have to set my first shown fragment by a program ( I have to change the first fragment every time I try to open the drawer), I searched a Lot but I can't change my code. Help me guys.
my drawer code:
public class Maincontant extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maincontant);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_gallery,R.id.nav_home , R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_contact)
.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.maincontant, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
You can use setNavigationItemSelectedListener then can call specific fragment like this following:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Log.e("selected","selected---menu---"+menuItem.toString());
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
if(menuItem.toString().equalsIgnoreCase("Gallery"))
{
navController.navigate(R.id.nav_gallery2);
}
else if(menuItem.toString().equalsIgnoreCase("Home"))
{
navController.navigate(R.id.nav_home);
}
else
{
navController.navigate(R.id.nav_slideshow);
}
return true;
}
});
If you are using NavigationGraph then you will have to specify the fragment there. navController.navigate(R.id.nav_gallery2);, I have specified this fragment on NavigationGraph, But not on DrawerMenu.
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);