What i want here when i move to another Fragment by clicking in NavigationDrawer menu button then button should be disabled.
Because addToBackStack(); method add multiple times in their stack when click again and again. So wanted to disable it when i move to another fragment.
To disable toggle button in navigation drawer use
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
to enable use LOCK_MODE_LOCKED_OPEN replacing LOCK_MODE_LOCKED_CLOSED
To disable drawer item click
Refer Hide Some Navigation Drawer Menu Item - Android there you can hide it
If you do not wish to hide in onNavigationItemSelected where you check (id == R.id.whatevertheitemid) also use a Boolean to allow access as you wish
eg
if (id == R.id.whatevertheitemid && isAccessGiven) { // do your task
}
Its helps me to solve this issue:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_item1){ //use can write your menu item here
return false;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Use this to disable menu item:
NavigationView navigationView;
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Now in menu item click:
Menu menuView= navigationView.getMenu();
menuView.getItem(ID).setEnabled(false);
in kotlin:
navigation.menu.findItem(R.id.your_target_item_id).setEnabled(false)
this is work for me and disabled item in drawerToggle menu
in Java:
NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
navigation.getMenu().getMenu().findItem(R.id.your_target_item_id).setEnabled(false);
Related
I have a navigaiton drawer. When i click on a certain item from the nav drawer, the nav drawer is closed and is redirected to main page. I want navigation drawer to refresh after clicking an item in it.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.refresh_drawer) {
----CODE----
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
What can i do to make nav drawer refresh and not redirect to main page? somebody please help me
It closes because you used:
drawer.closeDrawer(GravityCompat.START);
In onNavigationItemSelected() method so, removing this will help.
Also:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Define this inside onCreate() method and make it private by adding it in above of onCreate() like this to have access whole the Activity:
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}
This is the recommended way to define such widgets inside an Activity when Activity creates.
I'm working with Navigation Drawer and I want to click the button on activity and Navigation Drawer should show, but I don't know how to do it. Normally, when we swipe from left edge to the right the navigation will be shown. as such, I want to be able to open the navigation drawer with the click of a button.
Use the following methods to open and close your navigation drawer:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//To Open:
drawerLayout.openDrawer(Gravity.START);
//To Close:
drawerLayout.closeDrawer(Gravity.END);
Source1 for more information:
Source2
Button hamMenu = findViewById(R.id.ham_menu);
hamMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
// If the navigation drawer is not open then open it, if its already open then close it.
if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
else navDrawer.closeDrawer(Gravity.END);
}
});
You can also use a method and Use the exact same steps given by #Zeeshan.
public void hamMenu(View view){
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (!drawer.isDrawerOpen(GravityCompat.START)) {
drawer.openDrawer(GravityCompat.START);
} else {
drawer.closeDrawer(GravityCompat.END);
}
}
Use this method in the button which you need android:Onclick="hamMenu" in .xml.
My users can delete items in my Navigation Drawer, they select which one they wish to delete and it should be removed from the menu.
Here is how I try to do it so far:
// Should remove item at position 'which'
private void removeTab(int which) {
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navView.getMenu();
menu.removeItem(which + 1);
}
But it does not remove the item.
I'm not sure this is how removeItem should work, is there a way to remove an item in a Navigation Drawer using its position in the menu?
EDIT:
I get the idea of hiding the item, but that is problematic as I will have to keep track of the number of items hidden in order to hide the next one. (If Item1 and Item2 are hidden then Item3 is still at position 3 and not 1 when displayed).
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideItem();
}
private void hideItem()
{
navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_settings).setVisible(false);
}
You can hold the data in an arraylist. When you swipe over an item, you can remove the item from the array list arraylist.remove(index) and call adapter.notifyDataSetChanged().
Important:
If you implement this solution, you have to pass the arraylist to the adapter and work on the same list object.
you need to provide the id of MenuItem to be removed inside removeItem. not position.
like this:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
navigationView.getMenu().removeItem(item.getItemId());
}
});
I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and hamburgerbar:
Hamburger bar
I can open this bar by sliding it but I also want to make it open by clicking on drawable icon (right top corner icon). How can i do that?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
return super.onOptionsItemSelected(item);
}
I don't think so that I need to do some changes in layout files. What do I have to add in MainActivity file to make it possible?
I am newbie in Android code. Any help will be appreciable.
Using the Toolbar component should be fairly easy to achieve this by using a similar code to this:
Toolbar toolbar = (Toolbar) findViewById(R.id.home_toolbar);
toolbar.inflateMenu(R.menu.menu_home);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
mDrawerLayout.openDrawer(Gravity.RIGHT); // where mDrawerLayout is your android.support.v4.widget.DrawerLayout in your activity's xml layout.
}
return false;
}
});
EDIT:
The key component here is the menu_home.xml file which goes to your res/menu folder. You can add your desired menu item there, customize it's icon and even more, add as many items as you'd like to have on the right side of the toolbar(Obviously handle the openDrawer() method on whichever menu item you need - the recommended one is the rightmost though).
Use the openDrawer() method.
private DrawerLayout mDrawerLayout;
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
mDrawerLayout.openDrawer(Gravity.END); // or whatever gravity the of the drawer you want to open
Use Activity's onOptionsItemSelected(MenuItem menuItem) method:
First of all, keep the reference to your DrawerLayout in a class field:
DrawerLayout drawerLayout;
Somewhere in onCreate put this:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
And implement the method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
// if you want the default back/home hamburger menu item just put android.R.id.home instead
if (menuItem.getItemId() == R.drawable.icon_navigation) {
drawerLayout.openDrawer(GravityCompat.END);
}
return super.onOptionsItemSelected(menuItem);
}
How to disable the ActionBar button that opens navigation drawer?
I use the code below to disable opening the navigation drawer using swipe, but its action bar button (at Top/Left) still active and can be used to open the navigation drawer.
DrawerLayout navigationDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Try This i think it Will help you
actionBar.setDisplayHomeAsUpEnabled(false);
or you can use this
getActionBar().setDisplayHomeAsUpEnabled(false);
Use this:
getActionBar().setHomeButtonEnabled(false);
You should use setNavigationOnClickListener() on toolbar to avoid Nav Drawer from opening.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your code.
}
});
Nav drawer will open just by swiping, and not by clicking on the menu item button.