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);
Related
I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back.
Thanks.
You can do this.
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home
Call method onBackPressed();
here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
I currently have two navigation drawers/menus:
However I need a little bit of help setting up an OnClick listener for the right Action Bar menu.
Currently - I have an onclick listener for the left Action Bar Menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
However I'm not sure how I might be able to handle an onClick event for the right Action Bar menu.
Any suggestions are greatly appreciated.
Full XML and Java Source:
http://pastebin.com/ygyyjtLZ
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case R.id.action_more: // change the action_more with your own id in main.xml menu
// do your stuff here (your right click method)
return true;
// rest of your menu actions here...
}
}
return super.onOptionsItemSelected(item);
}
Can i make my Action Bar app icon clickable without displaying the back icon?
This is my code, it works, I have only layout problem:
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my activity layout, what I want is remove back icon, is it possible?
Try to use setHomeButtonEnabled(boolean enabled) instead of setDisplayHomeAsUpEnabled(boolean enabled). The latter do exactly the same thing as former which is enabling home button but additionally put up affordance sign which you want to get rid off.
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
I'm playing around with the sherlockactionbar library, there is a problem which is the up button won't work on API 10. The up icon can be shown on the actionbar, but it won't respond to my pressing. On higher API such as 16, it functions as expected without problems.
I tried several solutions I came across, but none of them work. Below is one of them.
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, test.class));
return true;
Make sure you have these set:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And that your switch is in this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
/* Do Something //*/
return true;
default:
return false;
}
}