Bring back the Navigation Drawer Indicator with support library v7 - android

I've been working on upgrading my app for Android API 21, and have gotten it mostly working with support library v7. However, because of these changes, the Navigation Drawer Indicator (hamburger menu button) no longer gets displayed.
It used to be that the indicator icon was set in the ActionBarDrawerToggle() method, but this is no longer the case in support lib v7.
There is another version of ActionBarDrawerToggle() that takes a ToolBar object. Do I have to have a Toolbar in addition to my Actionbar to bring back the Drawer Indicator?
What else would do the trick?
Here's my actionbar code.
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
// R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);

Just a shot in the dark here, but it might be a theme-related problem.
Does your app theme have the following items?
<style name="YourAppStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Other items ommited -->
<item name="drawerArrowStyle">#style/YourAppStyleName.DrawerArrowToggle</item>
</style>
<style name="YourAppStyleName.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
That also provides a good-looking animated arrow.

Yes you have to use toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

Related

Navigation drawer icon not showing using appcompat v7

style.xml
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="android:homeAsUpIndicator">#drawable/ic_back</item>
<item name="homeAsUpIndicator">#drawable/ic_back</item>
</style>
MainActivity.java
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
View actionBarView = inflator.inflate(R.layout.custom_title, null);
actionBar.setCustomView(actionBarView);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_action_nav_menu, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
tvTitle.setText(mTitle);
//getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
tvTitle.setText(mDrawerTitle);
//getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
you can see in above image, where in black circle i want to show 3 line image rather than back symbol image.
Use android.support.v7 instead of android.support.v4 as following:
import android.support.v7.app.ActionBarDrawerToggle;
And change your mDrawerToggle to the following:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
)...
Add following codes:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
change package import android.support.v4.app.ActionBarDrawerToggle; to
import android.support.v7.app.ActionBarDrawerToggle;
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_ );
mDrawerlayout is object DrawerLayout

action bar icon keep showing back icon

i'm literally new in developing android apps. My project using navigation drawer between fragments. i tried to custom the drawer with tutorial http://www.tutecentral.com/android-custom-navigation-drawer/ and got an error. in my application, in action bar, it keeps showing back icon instead the default icon.
this is url for before-after image of actionbar : http://oi58.tinypic.com/2ahsbys.jpg
here is the code i'm confused of (pretty same with the tutorial, tell me if there's something i missed)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
if i delete setDisplayHomeAsUpEnabled, the back icon is gone but i can't click somewhere to display the drawer, i have to slide it. please refer me if someone has solved it. many thanks.
*i have changed the R.drawable.ic_drawer to other drawable icons but it had no effects.
Try setting a theme for your activity in manifest file and inside styles.xml set your drawable for homeAsUpIndicator for that theme
<style name="CustomTheme" parent="#style/Widget.AppCompat.ActionBar">
<item name="homeAsUpIndicator">#drawable/ic_drawer</item>
</style>

Change Navigation Drawer homeAsUpIndicator with ActionBarDrawerToggle and appcompat v7

I'm trying to change my action bar's home as up indicator. Right now it shows an arrow (when opened) and 3 stripes (when closed):
I want to replace this arrow with a custom drawable. The code below doesn't works:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setLogo(getResources().getDrawable(R.drawable.actionbar_logo));
getSupportActionBar().setDisplayUseLogoEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.app_name, /* "open drawer" description for accessibility */
R.string.app_name /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
//getActionBar().setTitle(mTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle(mDrawerTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.actionbar_logo));
mDrawerLayout.setDrawerListener(mDrawerToggle);
If I use <item name="displayOptions">useLogo|showHome|showTitle</item> at the style, it shows my custom drawable besides the original homeAsUpIndicator, while what I want is to replace it
How can I achieve this? Thanks!

Hide the icon in the ActionBar in Android

I am trying to hide the application logo from the ActionBar. In order to do that I am using the setDisplayHomeAsUpEnabled(boolean) method:
this.getActionBar().setDisplayHomeAsUpEnabled(true);
This works almost as intented, except for two things:
The icon is still visible when the application starts (and then disappear)
The navigation drawer icon is not visible on some devices (for example on the Samsung Galaxy Tab 3)
How can I fix this?
The icon is still visible when the application starts (and then disappear)
To avoid this you need to undisplay your logo from your style.xml as follows:
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/mActionBar</item>
<item name="actionBarStyle">#style/mActionBar</item>
</style>
<style name="mActionBar" parent="#style/Widget.Holo.Light.ActionBar">
<item name="android:icon">#android:color/transparent</item>
<item name="icon">#android:color/transparent</item>
</style>
Because your app starts with loading your Theme and then after displays your UI by your Activity. Also, this will be a resource way, because your activities will not reload the UI like the home icon, etc. since they are first initialized thanks to the theme.
The navigation drawer icon is not visible on some devices
Make sure you have this following snippnet code:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
//...
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
And check your icon, or (re)download it.
Hope this helps.
Try: getActionBar().setDisplayUseLogoEnabled(false);

Toggle DrawerLayout With Title Only (NO App Icon)?

I have an activity with a DrawerLayout. I can open the drawer two different ways...by swiping from the left area of the screen towards the right and by clicking the app title. I DO NOT have an app icon displayed, only a title. I've implemented this exactly as recommended by Google here: Creating a Navigation Drawer: Open and Close with the App Icon
Everything is functional as far as opening and closing the drawer itself. However, it does not display the standard DrawerLayout icon that is suppose to be used. Instead I get the regular up caret (looks like a less than sign).
As soon as I add the app icon back to the ActionBar it begins to work as expected. The Drawer layout icon displays and animates when the drawer is opened or closed. I've tried removing the app icon both in my styles XML file and programmatically.
Is there a way to get the DrawerLayout icon working WITHOUT the app icon???
UPDATE: I've found a work around, but it's a hack more so than a solution. I simply created a 1x1 pixel transparent PNG (blank.png) and set it as my app icon in my styles.xml file. Below is all relative code:
styles.xml
<style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyCustomActionBar</item>
<item name="android:icon">#drawable/blank</item>
</style>
<style name="MyCustomActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>
MainActivity -> onCreate()
this.navDrawerToggle = new ActionBarDrawerToggle
(
this,
this.navDrawerLayout,
R.drawable.icon_nav_drawer,
R.string.nav_drawer_open,
R.string.nav_drawer_closed
)
{
public void onDrawerClosed(View view) {}
public void onDrawerOpened(View drawerView) {}
};
MainActivity -> onPostCreate()
super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();
MainActivity -> onResume()
this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);
MainActivity -> onPause()
this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);
MainActivity -> onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);
MainActivity -> onOptionsItemSelected(MenuItem item)
if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;}
else
{
// A bunch of item click handling happens here...
return true;
}
I was curious about this so I tried it with the sample for the DrawerLayout and it worked fine. Also, it seems like you have to use your own drawer icon drawable, it's recommended anyways. You can download the default assets from this site Creating a Navigation Drawer and put them in their respective drawable resource folder.
Here's what worked for me:
ActionBar actionBar = getActionBar();
// Let's get rid of the app icon here
actionBar.setIcon(null);
actionBar.setTitle("App Name");
// Setting these 3 options allows us to show the title as well as a "Home" elements
// "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable
// and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
| ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP);
// Get a reference of the DrawerLayout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(drawerToggle);
// Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer
// (this will also give you the nice/smooth animation) as well as allow you to do some
// other things depending on the events: onDrawerClosed & onDrawerOpened.
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_closed /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
actionBar.setTitle("Closed...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
actionBar.setTitle("Open...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set a listener to be notified of drawer events.
drawerLayout.setDrawerListener(drawerToggle);
UPDATE: It seems like the android:displayOptions on the ActionBar style are not being accounted for. Use actionBar.setDisplayOptions(int options) instead.
getActionBar().setIcon(android.R.color.transparent);
This worked for me..... ;)
After setting the drawer toggle you need to call the following method:
mDrawerToggle.syncState();
so your code would look like this:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
actionBar.setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
actionBar.setTitle("Preview Mode");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);

Categories

Resources