How to hide one item from drawer android - android

I am a new application developer.I use in my app drawer it has many options for the user.But sometimes I want to set a condition and hide an element from it.Sorry to ask the question again, but I tried to work on our previous posts and didn't work with me.
This is my code:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
navigationView.setNavigationItemSelectedListener(this);
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else if (id == R.id.nav_Profile) {
Intent i = new Intent(this, UserProfile.class);
startActivity(i);
} else if (id == R.id.nav_Politics) {
Intent i = new Intent(this, Ploysity.class);
startActivity(i);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
So for example I want to hide nav_Politics how I can hide it?

Check out this snippet of code:
mDrawerLayout.closeDrawer(Gravity.LEFT);
mDrawerLayout.closeDrawer(Gravity.RIGHT, false);
Use Gravity.LEFT or GravityCompat.START to move the left drawer. Use Gravity.RIGHT or GravityCompat.END for the right.
false disable drawer closing animation.

Related

NavigationView handle menu item click [duplicate]

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

Navigation drawer items click is not working on other activities

I'm trying to click on navigation drawer items but not working.
I made a NavigationDrawerActivity (by default: creating new project) and an AriesActivity (new activity). When I go with NavigationDrawerActivity, item click works. But when i go with AriesActivity, item clicks are not working.
Here I included drawer icon on AriesActivity, clicking on icon navigation drawer opens but item clicks are not working.
So the main question is How to handle clicks over navigation items on other activities by including drawer on other activities?
Here's my AriesActivity code
public class AriesActivity extends AppCompatActivity {
Toolbar mtoolbar;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aries);
//setting TOOLBAR on this activity(page)
mtoolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mtoolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle(null);
//setting DRAWER on this activity(page)
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, mtoolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
}
You have to override this method (onNavigationItemSelected) Example :
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gallery) {
startActivity(new Intent(Activity1.this, Activity2.class));
finish();
} else if (id == R.id.nav_slideshow) {
startActivity(new Intent(Activity2.this, Activity1.class));
finish();
} 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;
}

Avoid click on same items of Navigation drawer concurrently one after other for fragments and for activities

I am using common Navigation drawer for all activities and fragments but when I am clicking on same item of Navigation drawer also it inflating the same screen. What I want is the same item should not be loaded for second time.
This is my code:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
You should not use common navigation drawer for all activities and fragments. Common design pattern is showing different fragments on the main activity for the navigation drawer menu items. And if the user navigates inside any fragment you should open an activity without any navigation drawer but with a back arrow in action bar.
Now if you dont want to show the same fragment then in
onNavigationItemSelected you can write the logic like below -
Fragment fragment = getSupportFragmentManager().findFragmentByTag("TAG");
if (fragment == null)
{
Create and attach new fragment
}else{
//Do not do anything.
}
Simple thing, store clicked view'd id to a variable and compare if its the same,
int id; // use this variable to store id of clicked view.
#Override public boolean onNavigationItemSelected(MenuItem item) {
if(id != item.getItemId) {
id = item.getItemId(); // store clicked id in variable
// Do your clicking stuffs here...
if (id == R.id.nav_profile) {
Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
}
}
}
For fragments:
if (id == R.id.nav_things_to_do) {
if (id == R.id.nav_things_to_do && viewPager.getCurrentItem() != 0) {
Intent main1 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main1.putExtra("position", 0);
main1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main1);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
if (id == R.id.nav_all_tasks) {
if (id == R.id.nav_all_tasks && viewPager.getCurrentItem() != 1) {
Intent main2 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
main2.putExtra("position", 1);
main2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(main2);
overridePendingTransition(0, 0);
drawer.closeDrawer(GravityCompat.START);
}
else {
drawer.closeDrawer(GravityCompat.START);
}
}
For Activities:
if (id == R.id.nav_images) {
try {
Intent i = new Intent(MainFragmentsActivity.this,
SelectFilesActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
drawer.closeDrawer(GravityCompat.START);
} catch (Exception e) {
e.printStackTrace();
}
}

Navigation item's title disappeared when clicked

I have created drawer with navigation view. I have Navigation item's on which I am calling other activities.
The issue is when I click on navigation item, the other activity launches,and if I come back to main activity and open a drawer the clicked navigation item's title is disappeared only I can see the icon of the item.
code:
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);
toolbar.setNavigationIcon(R.drawable.menu_icon);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer , toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer .addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_list) {
startActivity(new Intent(MainActivity.this, LaunchVenueServiceActivity.class));
// Handle the camera action
}
else if (id == R.id.nav_dashboard) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
else if (id == R.id.nav_config)
{
startActivity(new Intent(MainActivity.this,LaunchYourServiceStep2.class));
}
else if (id == R.id.nav_chat) {
} else if (id == R.id.nav_notes) {
startActivity(new Intent(MainActivity.this,NotesActivity.class));
} else if (id == R.id.nav_user_guide) {
}
else if(id == R.id.nav_log_out)
{
SharedPreferences.Editor editor = getSharedPreferences("username",MODE_PRIVATE).edit();
editor.remove("UserUsername");
editor.commit();
Intent intent1 = new Intent(MainActivity.this,LoginActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(intent1);
}
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
}
What can be the reason for this?? Can anyone help please? Thank you..
Got the solution.. Just tried to change the color of navigation item's text and it worked. Don't know why and how..
navigationView.setItemTextColor(ColorStateList.valueOf(Color.BLACK));
That may appear if you changed style of your layout in manifest. Check if you have defined all items from default style such as color especially.

NavigationDrawerItem'sClickevent

I created NavigationView and set 3 items.
As described in below picture :
Now what I want is, when I click paticular item, it should start an activity.
How to implement that?
Thanks.!!
What I can understand from your question is that you have used NavigationView for navigation drawer and you must have used menu file for your three menu option as displayed in the image you have shared. You need to handle those three cases in switch-case as given below. Change the menu item id according to your code and you are good to go.
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
//To close the menu drawer once it is clicked
drawerLayout.closeDrawers();
switch (menuItem.getItemId()){
case R.id.menu_item_appointments: //R.id.menu_item_appointment - menu item id which you mentioned in the menu file
// Code to start activity
return true;
case R.id.menu_item_upload_status:
//code to start activity
return true;
default:
Toast.makeText(getApplicationContext(),"Oops! Something went wrong.",Toast.LENGTH_SHORT).show();
return true;
}
}
});
I assumes that you are using listview in your Navigation drawer, which is a better way, now Check below code:
//to listen click events:
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
Here, position is the item no. of the navigation drawer item clicked. You can use a switch case to identify the item clicked and then call to startActivity using
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
Your selectItem should be like this:
private void selectItem(int position) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(currentActivity, NewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
currentActivity.startActivity(intent);
break;
case 1: // and so on.
Try this
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_item_1:
menuItem.setChecked(true);
Intent intent1 = new Intent(this, Activity1.class)
startActivity(intent1);
return true;
case R.id.navigation_item_2:
menuItem.setChecked(true);
Intent intent2 = new Intent(this, Activity2.class)
startActivity(intent2);
return true;
case R.id.navigation_item_3:
menuItem.setChecked(true);
Intent intent3 = new Intent(this, Activity3.class)
startActivity(intent3);
return true;
case R.id.nav_sub_menu_item01:
menuItem.setChecked(true);
Intent intent = new Intent(this, Activity1.class)
startActivity(intent);
return true;
}
}
Create an OnNavigationItemSelected like the below example .
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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);
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_project) {
Intent i = new Intent(HomeActivity.this, Project.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
Intent i = new Intent(HomeActivity.this, Gallery.class);
startActivity(i);
} else if (id == R.id.nav_contact) {
Intent i = new Intent(HomeActivity.this, Contact.class);
startActivity(i);
} else if (id == R.id.nav_login) {
Intent i = new Intent(HomeActivity.this, Login.class);
startActivity(i);
} else if (id == R.id.nav_favourites) {
Intent i = new Intent(HomeActivity.this, Favourite.class);
startActivity(i);
} else if (id == R.id.nav_share) {
}
}
add these two items in your strings.xml
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

Categories

Resources