I have an activity with many fragments that uses action bar and navigation drawer. It has "home as up" enabled. I have implemented proper logic that only top level fragments show action bar drawer toggle icon, other fragments show up arrow. I achieved this by:
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerList);
Now old v4 support library ActionBarDrawerToggle became deprecated. I've switched to v7 version together with new Toolbar to get Material Design look. After that when drawer is open "up" arrow is correctly displayed, but when the above-mentioned code is executed it disappears completely.
Is it a bug in support library or I have to do something different to show "up" arrow instead of drawer indicator?
Answer/comments of Nikola Despotoski and Andrey Novikov are perfectly correct but I want to mention that after toolbar was replaced with following code:
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(getV7DrawerToggleDelegate().getThemeUpIndicator());
setSupportActionBar(toolbar);
your activity will receive every onOptionsItemsSelected events even if you enable your drawer toogle again drawerToggle.setDrawerIndicatorEnabled(true);
So you need to handle this, I've ended with
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (drawerToggle.isDrawerIndicatorEnabled()) {
return drawerToggle.onOptionsItemSelected(item);
} else {
onBackPressed();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Have you tried to get themed up indicator using getV7DrawerToggleDelegate().getThemeUpIndicator () and set it after you disable the indicator?
Because when the indicator is disabled ActionBarDrawerToggle tries to set the previous indicator.
From ActionBarDrawerToggle source:
public void setDrawerIndicatorEnabled(boolean enable) {
if (enable != mDrawerIndicatorEnabled) {
if (enable) {
setActionBarUpIndicator((Drawable) mSlider,
mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
} else {
setActionBarUpIndicator(mHomeAsUpIndicator, 0);
}
mDrawerIndicatorEnabled = enable;
}
}
Edit:
As of deprecation of ActionBarActivity, you should use getDrawerToggleDelegate().getThemeUpIndicator ()
If you use AppCompatActivity, you can get the right drawer icon and back icon by
if(homeUp)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
else
{
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.syncState();
}
Without need for getV7DrawerToggleDelegate :D
Related
I am trying to use this code to show and hide menu icon in toolbar with Fragment in Android Studio:
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem IconBTON = menu.findItem(R.id.myIcon);
if (statusBlueTooth == true){
IconBTON.setVisible(true);
}
if (statusBlueTooth == false){
IconBTON.setVisible(false);
}
}
but the icon is not shown even though the value of the flag statusBlueTooth = true, but just when I touch the toolbar the icon is shown, what do I need to do so that this is shown according to the value of flag statusBlueTooth
Try adding this after in your onCreate of your Fragment:
setHasOptionsMenu(true);
and check IconBTON != null before setting the visibility.
I have created an ActionDrawer for my Wear OS App and want to change the icon and text on runtime. This code is working and my app continues to run without errors, but I am not able to slide up the ActionDrawer anymore. Also, when the ActionDrawerMenu is already opened and I press the button there, it disappears and my UI of the app seems frozen.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Top navigation drawer
WearableNavigationDrawerView wearableNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer);
wearableNavigationDrawer.setAdapter(new NavigationDrawerAdapter());
wearableNavigationDrawer.getController().peekDrawer();
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = (WearableActionDrawerView) findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
final int itemId = menuItem.getItemId();
switch(itemId) {
case R.id.menuItem_pause:
if(intent == null) {
menuItem.setIcon(getDrawable(R.drawable.ic_pause));
menuItem.setTitle(R.string.actionDrawerMenuPause);
} else {
menuItem.setIcon(getDrawable(R.drawable.ic_play));
menuItem.setTitle(R.string.actionDrawerMenuStart);
}
invalidateOptionsMenu();
return true;
}
return false;
}
I have tried some solutions with invalidateOptionsMenu(), onPrepareOptionsMenu() and onCreateOptionsMenu() but nothing worked for me. I think this just works with standard Android mobile apps but not with Wear OS. So how can I change the text and icon of my WearableActionDrawer-MenuItems when pressing a MenuItem?
I found the solution by accident. It's just this one line of code that programmatically implements the menu.
//Bottom action drawer
WearableActionDrawerView wearableActionDrawer = findViewById(R.id.bottom_action_drawer);
wearableActionDrawer.getController().peekDrawer();
wearableActionDrawer.setOnMenuItemClickListener(this);
getMenuInflater().inflate(R.menu.action_drawer_menu, wearableActionDrawer.getMenu());
I'm using the navdrawer layout from android to show a filter on the right side of my screen in a sliding menu.
Now I wanted to remove the left icon in the toolbar. I did this as follows:
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
The only problem I'm now facing is that the icon is an arrow and it doesn't do anything and I want it still to have the up behaviour.
I tried something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_filter:
Timber.d("Open or close filter");
mNavigationDrawerFragment.openDrawer();
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
But this doens't work because onOptionsItemSelected is not called for that arrow?
Anyone an idea?
try :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
I am creating drawer layout with both right and left menu as it worked fine but i need to stop toggling drawer icon when i open right menu . i am puzzled in it Please help.
Thanks in advance.
switch(item.getItemId()){
case R.id.add_member:
if(chk==0){
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT);
chk=1;
if(drawer.isDrawerOpen(Gravity.LEFT))
{
//ActivityCompat.invalidateOptionsMenu(Main.this);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT);
drawer.closeDrawer(Gravity.LEFT);
drawer.openDrawer(Gravity.RIGHT);
sh=1;
}
else{
drawer.openDrawer(Gravity.RIGHT);
sh=1;
}
}
else if(chk==1){
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
drawer.closeDrawer(Gravity.RIGHT);
chk=0;
}
break;
Please check answer in this link Disable animation of drawer icon in double navigation drawer for right drawer
You can extend ActionBarDrawerToggle and override onDrawerSlide() method to set the slideOffset to 0 in the rightDrawerListView
I have a NavigationDrawer and hide/show the OptionsMenu (which is located on the bottom due to a split ActionBar) whenever the drawer gets opened/closed:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!isDrawerOpened) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return false;
}
I would like to hide the Menu with an animation, (for reference: like the animation used by the system when you call ActionBar.hide() ), however I found no way to access the Layout used for the Menu (so I could get a View object to apply the animation to). It seems there's no public API available to perform this, I wouldn't mind some reflection approach if I knew what to look for. Any suggestions?
UPDATE
looking at this source I finally figured out how to access the View of a split ActionBar using a non-public identifier:
private View getSplitActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
return v.findViewById(getResources().getIdentifier("split_action_bar",
"id", "android"));
}
Now I can create an Animation, set an AnimationListener and call invalidateOptionsMenu() as soon as the Animation ends. There's one problem left: doing this will let an ugly white rectangle at the place where the bottom part of the ActionBar was, this white rectangle will stay there until the Animation is complete. To get rid of it I would need to use <item name="android:windowActionBarOverlay">true</item> in my style XML (or doing the same by code) to display the ActionBar in overlay mode. However this is not what I need.
The question is now: is there any way to apply the overlay mode only to the split ActionBar (= the bottom part of the ActionBar)?
You need to call invalidateOptionsMenu() when the drawer is open/close, like this
public void onDrawerOpened(View view) {
invalidateOptionsMenu();
}
Now, onPrepareOptionsMenu() will be called,where you can hide/show the option menu like this..
#Override
public boolean onPrepareOptionsMenu(Menu menu){
// check if drawer is open
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for(int index = 0 ; index < menu.size() ; index ++){
MenuItem menuItem = menu.getItem(index);
if(menuItem != null) {
// hide the menu items if the drawer is open
menuItem.setVisible(!drawerOpen);
}
}
return super.onPrepareOptionsMenu(menu);
}
You can hide all the menu or any of the required menu item.