Navigation elements on click - android

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
Fragment fragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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_home, R.id.nav_gallery,
R.id.nav_tools, R.id.nav_share)
.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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
I want to implement on click methods to some of my elements in nav view but if I use the following code it doesn't change the title in action bar and it is not consistent can anyone suggest a better way of setting on click listeners for the drawer. I am using the navigation drawer version 2.2.0
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.nav_blog) {
Intent intent = new Intent(MainActivity.this,Blog.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_logout){
FirebaseAuth.getInstance().signOut();
Intent i = new Intent(MainActivity.this,StartActivity.class);
startActivity(i);
finish();
}
if (item.getItemId() == R.id.nav_settings){
Intent i = new Intent(MainActivity.this,Settings.class);
startActivity(i);
}
if (item.getItemId() ==R.id.nav_home){
fragment = new HomeFragment();
}
if (item.getItemId() ==R.id.nav_gallery){
fragment = new GalleryFragment();
}
if (item.getItemId() ==R.id.nav_tools){
fragment = new ToolsFragment();
}
if (item.getItemId() ==R.id.nav_cal){
fragment = new CalenderFragment();
}
FragmentManager fragmentManager =
getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false;
}
});

My recommendation would be to use Android Annotations. To do so, simply add the following lines to your build.gradle
repositories {
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
def AASnapshotVersion = '4.7.0-SNAPSHOT'
dependencies {
annotationProcessor "org.androidannotations:androidannotations:$AASnapshotVersion"
implementation "org.androidannotations:androidannotations-api:$AASnapshotVersion"
}
You can now annotate a function with #Click(R.id.nav_home) for exemple to specify that it is an onClickListener.
For more information on AA, you can check their website http://androidannotations.org/ which will direct you to their git page

Related

How to set a cart fragment when action bar item is clicked (on option item selected) in the same activity when i am in any other fragment

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);

android change Hamburger icon of Navigation Drawer [duplicate]

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

How I can change my first fragment in drawerMenu

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.

How to setup default navigation drawer and select different fragments?

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);

Nothing happens when item of navigation bar clicked

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.

Categories

Resources