I have problem with drawer indicator in toolbar.
The icon (hamburger) is visible only while moving (swiping) drawer view, and becomes invisible on stop.
Here is screenshot
http://imgur.com/EBGDq4z
And while moving it becomes visible
http://imgur.com/tEsAMLx
If stop move drawer view it becomes invisible again.
Here is my setup code
mToolbar.setVisibility(View.VISIBLE);
mToolbar.setTitle(getToolbarTitle());
setSupportActionBar(mToolbar);
if (hasDrawerToggle()) {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerMainLayout,
R.string.drawer_open, R.string.drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerMainLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
if (hasDrawerToggle()) {
mDrawerToggle.syncState();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (hasDrawerToggle()) {
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Why are you using hasDrawerToggle() condition?
The problem may due to your condition, so try by removing condition from all places like onPostCreate,onCreate etc.
Hop it will work.
Related
I have refined the Navigation Drawer Activity project template of Android Studio, which uses Toolbar, v7.app.ActionBarDrawerToggle and NavigationView instead of the NavigationDrawerFragment (and layout/fragment_navigation_drawer.xml).
According to Google's guidance and reference, I set up ActionBarDrawerToggle. I made it 1) instantiate in onCreate, 2) call syncState in onPostCreate and 3) call through to onConfigurationChanged and onOptionsItemSelected.
It is almost perfectly working except for one thing: the hamburger icon is showed as an arrow.
A similar issue can be found on the StackOverFlow, especially for this question. But the question is about the way using the old R.drawable.ic_drawer as hamburger which is not the material design (before 5.0 Lollipop) version. Moreover the question has no answer and the questioner commented he had solved without stating any solution.
After a while, I have found a solution accidentally. It is somewhat dirty. It is to call syncState in onCreate. Because it seems that, for some reason, onPostCreate is not called in my app. Actually, this dirty solution is used in an answer to the other problem.
But the official reference says to call syncState in onPostCreate. Why doesn't it work? Why doesn't my app call onPostCreate? This should be main cause of not being showed hamburger icon (instead being showed arrow).
Below is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle.syncState(); // calling this here is somewhat a dirty solution
}
#Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
}
Here onPostCreate:
#Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
It should be this:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
There are two types of onPostCreate:
Activity's onPostCreate with two arguments.
AppCompatActivity's onPostCreate with a single argument.
You should have made a mistake to choose the former one when you override a method on Android Studio.
I have a problem, I have different activity with different background color (white or blue), also the menu icon need to be white or blue (opposite to activity background), I can't find the correct way for change it after change activity, how can I do this?
U must do like this:
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer, //<-- This is the icon provided by Google itself
R.string.drawer_open,
R.string.drawer_close
)
This in setUp() method.
And dont forget to add these code:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
I can't have the Drawer Indicator display. Currently I have either nothing or the "<" at the top left corner of the screen depending of the actionBar settings. But I want the Drawer Indicator of the Nagivation Drawer instead.
I use :
v4.widget.DrawerLayout
v7.app.ActionBarDrawerToggle
but android.app.ActionBar (not the support 7 one).
Here is snippet of the code :
#Override
protected void onCreate(Bundle savedInstanceState)
{
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
//I tried all combinations unsuccessfully
....
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLV = (ListView) findViewById(R.id.left_drawer);
drawer_Linearlayout = (LinearLayout) findViewById(R.id.drawer_Linearlayout);
drawerLV.setAdapter(new ArrayAdapter<String>(
this,
R.layout.layout_main_drawer_list_item,
mDrawerItems));
drawerLV.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
drawerToggle = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open,R.string.drawer_close) {
#Override
public void onDrawerClosed(View view) {
actionBar.setTitle(mTitle);
}
#Override
public void onDrawerOpened(View drawerView) {
actionBar.setTitle(mDrawerTitle);
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerListener(drawerToggle);
}
I eventually fixed my problem.
I forgot to add the following callback in my Acticity :
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
By the way, Lollipop upgrade of v7.app.ActionBarDrawerToggle adds a nice effect when the navigation drawer is opening or closing. I recommend it.
I think you also must specify android.R.id.home it in the onOptionsItemSelected to make the back button visible :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//the onClick for your back button
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And you just need to use actionBar.setDisplayHomeAsUpEnabled(true);
UPDATE :
Just did a quick googling, take a look at this :
Display back button on action bar
Try my answer. I think it will solve your problem.
Sorry for my previous answer, I misread it. Think this is what you need:
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer, /* The image drawable you're missing */
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
The image is custom, also known as the hamburger.
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) { ... }
Okay, I double checked R.drawable.ic_drawer a few times. It is an icon with 3 bars, but my android display a left arrow. Anyone know what's wrong and how to fix it? Thanks in advance.
try to remove getActionBar().setDisplayHomeAsUpEnabled(true);
Call syncState() from your Activity's onPostCreate to synchronize the indicator with the state of the linked DrawerLayout.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
Additionally onConfigurationChanged should be called on the ActionBarDrawerToggle, include this in your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
In your NavigationDrawerfragment class go to setUp method and do something like this with actionbar to set actionBar.setHomeAsUpIndicator() to ic_drawer just like below. It will remove back button and replace with ic_drawer button
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
}
I am trying to implement a navigation drawer,Everything working perfectly except the toggle button is not visible.
Here is my Code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.list, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility)
also added these functions
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
How can i make it visible?
we have to enable following method
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);