how disable DrawerArrowToggle from toolbar or move right - android

first question:
i want to create a right to left toolbar like this image:
how can customize the toolbar DrawerArrowToggle to move right?
And second question:
i create a custom toolbar and make a button for open drawer but toolbar DrawerArrowToggle is appear left of my custom toolbar.
how to remove it?
--update--
with thanks to sasikumar by adding this line the DrawerArrowToggle is removed.
mDrawerToggle.setDrawerIndicatorEnabled(false);
here my toolbar code:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.action_bar, null);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(view, new ActionBar.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
Toolbar parent = (Toolbar) view.getParent();
parent.setContentInsetsAbsolute(0, 0);
Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, Drawer,
parent, R.string.openDrawer,
R.string.closeDrawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
Drawer.closeDrawer(Gravity.RIGHT);
} else {
Drawer.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
my desire app is like this:
sorry for bad english.

you can hide the arrow by using onDrawerSlide method.
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
getToolbar(), R.string.open, R.string.close) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, 0); // this disables the animation
}
};
If you want to remove the arrow completely, you can add
super.onDrawerSlide(drawerView, 0); // this disables the arrow # completed state
at the end of the onDrawerOpened function

Check the below link, maybe helpful:
How can I open navigation drawer from right side to left
and this link too:
Android - Is Navigation Drawer from right hand side possible?

Seems you've added remaining code.Simply set your drawer component gravity to the right.
like...
android:layout_gravity="right"
now set the drawer position to the right using following code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};

Related

Wait until DrawerLayout animation is done

I have a drawerLayout (support.v4) with a navigationView and have many (like many up to n menu items) menu items. To have less items on the screen at the same time, some menu items lead to another menu with many items.
The thing is, my menu switch works fine, I'm able to pass from one menu to the other one but I have no animation... I want to close the drawer when an item linked to another menu is selected and once the menu is loaded, to open it again.
Here's my code:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem selectedItem) {
mDrawerLayout.closeDrawer(GravityCompat.START);
Log.d(MainActivity.class.getName(), selectedItem.toString());
MobileXMLMenu menu = UserMenu.getInstance().getMenu();
for(int i = 0; i < menu.sizeMenuItems(); i++){
if(menu.getMenuItem(i).getLabel(Utility.retreiveLanguage()).equals(selectedItem.toString())){
Log.d(MainActivity.class.getName(), menu.getMenuItem(i).sizeMenuItems() + "");
if(menu.getMenuItem(i).sizeMenuItems() > 0){
Menu nMenu = navigationView.getMenu();
nMenu.clear();
for(int j = 0; j < menu.getMenuItem(i).sizeMenuItems(); j++){
MobileXMLMenuItem item = menu.getMenuItem(i).getMenuItem(j);
nMenu.add(item.getLabel(Utility.retreiveLanguage())).setIcon(MemberOptionsManager.getInstance().getMenuItemIcon(item.getDecorator()));
}
mDrawerLayout.openDrawer(GravityCompat.START);
isInSubmenu = true;
}
}
}
return false;
}
});
I also have this for my ActionBarDrawerToggle (don't know if this could help):
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mFragment.getFragmentName());
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
public void onDrawerOpened(View drawerView){
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle("Open");
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
};
When I select an item not link to any sub menu, the drawer closes, but otherwise, I see no animation, just the menu content changing with no animation. How can I force my app to wait for the drawer to close before changing the menu content?
Thanks!
You can use callback methods of the drawer layout from official documentation of the android.
onDrawerClosed(View drawerView)
onDrawerOpened(View drawerView)
onDrawerSlide(View drawerView, float slideOffset)
onDrawerStateChanged(int newState)
You can find more at link:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html
You can setDrawerListener on DrawLayout like this
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
handleClickNavigationItem(mClickedNavigationItem);
}
public void onDrawerOpened(View drawerView) {
}
};
mDrawerLayout.setDrawerListener(toggle);
handleClickNavigationItem method contains the code which handle navigation event.
And just save clicked item in onNavigationItemSelected method
public boolean onNavigationItemSelected(MenuItem item) {
mClickedNavigationItem = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Toolbar Navigation Hamburger Icon missing

I'm looking for a way to display the hamburger icon whitout using the Drawer/DrawerToggle and use the default icon included in Android
By setting getSupportActionBar().setDisplayHomeAsUpEnabled(true); it display the back arrow but not the hambuerger. Other post on Stackoverflow (like this or this) use the DrawerLayout or a custom drawable. I cannot find the vector or png for the hamburger icon on the Android source.
Do you know how can I find the original hamburger icon in android/support library? (or how to displayed it)
Note: Vector and png can be found on google.com/design website : http://www.google.com/design/spec/resources/sticker-sheets-icons.html#
In my activity
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(LOG_TAG, "navigation clicked");
}
});
Layout file
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"/>
Styles.xml
<!-- Base application theme. -->
<style name="Theme.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryDef</item>
<item name="colorPrimaryDark">#color/primaryDarkDef</item>
<item name="colorAccent">#color/primaryDef</item>
<!-- Remove the actionbar shadow-->
<item name="android:windowContentOverlay">#null</item>
</style>
If you want to use the same drawer as lollipop then let me tell you that's not a static image. That image is drawn in real time by a class called DrawerArrowDrawableToggle. So there is no "hamburger" icon for that.
However if you want the hamburger icon with no animation you can find it here:
https://material.io/tools/icons/?icon=menu&style=baseline
To have an animated hamburger icon you should use DrawerLayout with ActionBarDrawerToggle and enable the icon for the ActionBar and for the ActionBarDrawerToggle.
Example:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle;
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.hello_world, R.string.hello_world)
{
public void onDrawerClosed(View view)
{
supportInvalidateOptionsMenu();
//drawerOpened = false;
}
public void onDrawerOpened(View drawerView)
{
supportInvalidateOptionsMenu();
//drawerOpened = true;
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
Also, you need to add these methods to your Activity:
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
For that you just need write to some lines
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
toggle.setDrawerIndicatorEnabled(true); if this is false make it true or remove this line
Here is the simplest solution that worked for me.
The ActionBarDrawerToggle has two types constructors. One of them take toolbar as a parameter. Use that (second one below) to get the animated hamburger.
ActionBarDrawerToggle(this, mDrawerLayout, R.string.content_desc_drawer_open,
R.string.content_desc_drawer_close);
ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.content_desc_drawer_open,
R.string.content_desc_drawer_close);` //use this constructor
You can try to make your own drawable for the hamburger icon like this.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#ffffff"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z" />
</vector>
Then in your fragment/activity,
getSupportActionBar().setHomeAsUpIndicator(R.drawable.as_above);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
For other drawables, this might help: https://github.com/google/material-design-icons/blob/master/navigation/drawable-anydpi-v21/
I had the same problem and I found the simplest solution here:
appcompatv7-v21-navigation-drawer-not-showing-hamburger-icon
All I had to do was to call:
mDrawerToggle.syncState();
I had the same problem.
Get the ToolBar and then set Navigation icon
final android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.blablabla);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle("title");
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_list);
ok to hide back arrow use
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
then find hamburger icon in web ->hamburger
and finally, set this drawable in your project with action bar method:
getSupportActionBar().setLogo(R.drawable.hamburger_icon);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
it's work with me
Maybe you can try this, but you will lose animation between arrow and hamburger icon
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
super.setContentView(R.layout.activity_menu_drawer_left);
_drawerToggle = new ActionBarDrawerToggle(this, _drawerLayout, R.string.drawer_opened, R.string.drawer_closed) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
changeDrawerIconOnDrawerClick(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
changeDrawerIconOnDrawerClick(R.drawable.ic_drawer);
}
};
//to change default icon to hamburger item initially
changeDrawerIconOnDrawerClick(R.drawable.ic_drawer); }
private void changeDrawerIconOnDrawerClick(int resourceId) {
//Drawable icon = ContextCompat.getDrawable(getApplicationContext(), resourceId);
Drawable icon = ResourcesCompat.getDrawable(getResources(), resourceId, null);
icon.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
_drawerToggle.setDrawerIndicatorEnabled(false);
_drawerToggle.setHomeAsUpIndicator(icon);
}
Replace the default Up-arrow with your own drawable
getSupportActionBar().setHomeAsUpIndicator(R.drawable.hamburger);
Just Add the following in your onCreate method,
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, mToolbar, R.string.home_navigation_drawer_open, R.string.home_navigation_drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
mDrawer.addDrawerListener(toggle);
toggle.syncState();
and In strings.xml,
<string name="home_navigation_drawer_open">Open navigation drawer</string>
<string name="home_navigation_drawer_close">Close navigation drawer</string>
Use this constructor in MyActionBarDrawerToggle :
public MyActionBarDrawerToggle(AppCompatActivity host, DrawerLayout drawerlayout, SupportToolbar toolbar, int openedResource, int closedResource)
: base(host, drawerlayout, toolbar, openedResource, closedResource)
{
mHostActivity = host;
mOpenedResource = openedResource;
mClosedResource = closedResource;
}
and Call this method in teh mainActivity (Using AppCompatActivity)
mDrawerToggle = new MyActionBarDrawerToggle(
this, //Host Activity
mDrawerLayout, //DrawerLayout
mToolbar, //Toolbar
Resource.String.openDrawer, //Opened Message
Resource.String.closeDrawer //Closed Message
);
mDrawerLayout.AddDrawerListener(mDrawerToggle);
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayShowTitleEnabled(true);
mDrawerToggle.DrawerIndicatorEnabled = true;
mDrawerToggle.SyncState();
in onCreate():
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Backstack.get(MainActivity.this).goBack();
}
});
//actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
//getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(true);
And when setting up UP navigation:
private void setupViewsForKey(Key key) {
if(key.shouldShowUp()) {
drawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
drawerToggle.setDrawerIndicatorEnabled(true);
}
drawerToggle.syncState();
in JetPack it work for me
NavigationUI.setupWithNavController(vb.toolbar, nav)
vb.toolbar.navigationIcon = ResourcesCompat.getDrawable(resources, R.drawable.icon_home, null)

How to replace the hamburger icon used for ActionBarToggle on Android Toolbar with a custom drawable?

I have implemented a basic ActionBarDrawerToggle using the new Toolbar in Android 5.0.
However, I am unable to figure out how to change the default hamburger icon that is supplied.
From the android documentation it says that "the given Activity will be linked to the specified DrawerLayout and the Toolbar's navigation icon will be set to a custom drawable... This drawable shows a Hamburger icon when drawer is closed and an arrow when drawer is open. It animates between these two states as the drawer opens."
I currently have this all working correctly with the following code, however I want to replace the default supplied hamburger with my own drawable.
Here is my current code:
MainActivity.java
#InjectView(R.id.main_activity_toolbar)
Toolbar mToolbar;
#InjectView(R.id.main_activity_drawer_layout)
DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main_activity);
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.navigation);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
This line:
mToolbar.setNavigationIcon(R.drawable.navigation);
doesn't seem to work.
Is this possible to do? Thanks!
ActionBarToggle Documentation - https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html
You can use the toolbar as Stand Alone mode, that means you should not use your toolbar as part of your ActionBarDrawerToggle constructor, you can achieve that using the below code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, null,
R.drawable.appbar, R.drawable.appbar)
(Note how the toolbar instance is not being sent to the ActionBarDrawerToggle constructor)
Also, you should inflate your menu manually
mToolbar = (Toolbar) findViewById(R.id.nav_toolbar);
mToolbar.inflateMenu(R.menu.base);
And remove the setSupportActionBar(mToolbar); line of code.
Of course, you will have to handle the navigation click by yourself:
mToolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() ...
Then, you can open your drawer like this:
drawerButton = (BadgeDrawerButton) findViewById(R.id.badge_drawer_button);
drawerButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
Hope this may help.
These two lines of code work for me:
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer); //set your own
And then call this:
mDrawerToggle.syncState();
The default menu drawable for the ActionBarDrawerToggle is DrawerArrowDrawable.
You can subclass this to add custom functionality, like badges, like so:
public class BadgedDrawerArrowDrawable extends DrawerArrowDrawable {
/**
* #param context used to get the configuration for the drawable from
*/
public BadgedDrawerArrowDrawable(Context context) {
super(context);
setColor(context.getResources().getColor(R.color.colorAccent));
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(60);
canvas.drawText("!", canvas.getWidth() - 60, 25, paint);
}
}
Usage:
actionBarDrawerToggle.setDrawerArrowDrawable(new BadgedDrawerArrowDrawable(activity));
My solution is by subclassing ActionBarDrawerToggle.
public class MyActionBarDrawerToggle extends android.support.v7.app.ActionBarDrawerToggle {
public MyActionBarDrawerToggle(Activity activity, final DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
setHomeAsUpIndicator(R.drawable.drawer_toggle);
setDrawerIndicatorEnabled(false);
setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
}
}
the thing that worked for me is that i just needed to call toolbar.setNavigationIcon(R.drawable.ic_camera_alt_24dp);
at the end of onCreate, or at least after mDrawerToggle = new ActionBarDrawerToggle...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_action_name);
as of Jan 2018, this is a working solution (at least for me):
//setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this, drawer_layout, null, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
toolbar.inflateMenu(R.menu.menu_main)
toolbar.setNavigationIcon(R.drawable.ic_menu)
toolbar.setNavigationOnClickListener {
drawer_layout.openDrawer(Gravity.START)
}
toolbar.setOnMenuItemClickListener {
true
}
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
things to care:
setSupportActionBar should be commented out
ActionBarDrawerToggle is not taking a reference to toolbar
We should inflate menu ourself, and handle onClicks. onCreateOptionsMenu and onOptionsItemSelected will not be functioning.
Don't forget to call toggle.syncState()
As for v7 support library - you can create your own representation of DrawerArrowDrawable.
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
supportInvalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
DrawerArrowDrawable drawerArrowDrawable = new DrawerArrowDrawable(this);
drawerArrowDrawable.setAlpha(1);
drawerArrowDrawable.setSpinEnabled(false);
drawerArrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_LEFT);
drawerArrowDrawable.setColor(Color.BLACK);
mDrawerToggle.setDrawerArrowDrawable(drawerArrowDrawable);
Here's how I was able to finally get mine to work.
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
It turned out to be the
mDrawerToggle.syncState();
That finally got everything to work.
I think it is recommended to put the call to syncState() in the onPostCreate(...) lifecycle method.
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

AppCompat v7 Toolbar Up/Back Arrow Not Working

I have two fragments in an activity. When fragment A is showing, I want the navigation drawer burger icon to show and the navigation drawer to work. When fragment B is showing, I want the back arrow to show and when it's clicked do an up navigation. However, I can't seem to get the new AppCompat v7 toolbar to show the up arrow at all inside my ActionBarActivity unless the nav drawer is open.
In my activity, for my onCreate() method I have...
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And then I call mDrawerToggle.syncState(); in my onPostCreate()
I've tried searching on how to programatically trigger the toolbar icon to the back arrow but nothing has worked. From what I've gathered, calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
from my fragment should change the icon but that's not the case. This may be a stupid question, but what am I doing wrong?
From what I have seen in the source code of v7 ActionBarDrawerToggle, you can animate the icon to different states without having the drawer being opened.
private enum ActionDrawableState{
BURGER, ARROW
}
private static void toggleActionBarIcon(ActionDrawableState state, final ActionBarDrawerToggle toggle, boolean animate){
if(animate) {
float start = state == ActionDrawableState.BURGER ? 0f : 1.0f;
float end = Math.abs(start - 1);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ValueAnimator offsetAnimator = ValueAnimator.ofFloat(start, end);
offsetAnimator.setDuration(300);
offsetAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
offsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float offset = (Float) animation.getAnimatedValue();
toggle.onDrawerSlide(null, offset);
}
});
offsetAnimator.start();
}else{
//do the same with nine-old-androids lib :)
}
}else{
if(state == ActionDrawableState.BURGER){
toggle.onDrawerClosed(null);
}else{
toggle.onDrawerOpened(null);
}
}
}
Morphing between Burger and Arrow depends on values between 0f and 1.0f, basically these are values that the drawer passes to the ActionBarDrawerToggle.
I used ValueAnimator to animate values in this range, i.e mimicking the drawer toggling.
null arguments are safe because ActionBarDrawerToggle does not care at all about drawer views.
Make sure you take a look at new interpolators to do the animation fully-by-the-book of material design guidelines:
fast_out_linear_in
fast_out_slow_in
Another approach is to access mSlider private field of the ActionBarDrawer through reflection and call setPosition(float position) method to toggle between Burger and Arrow.
mSlider is of type (extends) DrawerArrowDrawable.
Personally, I always try to avoid reflection, as long as there is no other way to do your dirty work.
As Support Library updated to 23.0.0, there is a better way to play drawer-arrow animation. So I'm going to improve #Nikola's answer. Here's code:
public static void playDrawerToggleAnim(final DrawerArrowDrawable d) {
float start = d.getProgress();
float end = Math.abs(start - 1);
ValueAnimator offsetAnimator = ValueAnimator.ofFloat(start, end);
offsetAnimator.setDuration(300);
offsetAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
offsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float offset = (Float) animation.getAnimatedValue();
d.setProgress(offset);
}
});
offsetAnimator.start();
}
And call it whenever you want by:
playDrawerToggleAnim((DrawerArrowDrawable) toolbar.getNavigationIcon());
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
int stackHeight = getSupportFragmentManager().getBackStackEntryCount();
if (stackHeight > 0) { // if we have something on the stack (doesn't include the current shown fragment)
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
}
});
After ...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getSupportFragmentManager().popBackStack();
return true;
....
}
In my case the icon is animating:
I have used ActionBarDrawerToggle v7.
MainActivity:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
setSupportActionBar(toolbar);
toolbar.setTitle("ToolBar Demo");
toolbar.setLogo(R.drawable.ic_launcher);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.open_navigation_drawer,
R.string.close_navigation_drawer) {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// TODO Auto-generated method stub
super.onDrawerSlide(drawerView, slideOffset);
}
/** Called when a drawer has settled in a completely closed state. */
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle("hello");
}
/** Called when a drawer has settled in a completely open state. */
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("hi");
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) { // <---- added
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) { // <---- added
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState(); // important statetment for drawer to
// identify
// its state
}
#Override
public void onConfigurationChanged(Configuration newConfig) { // <---- added
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(Gravity.START | Gravity.LEFT)) { // <----
// added
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}

Android lollipop toolbar switch between open/close drawer and back button

I had standart navigation drawer, but now i'm trying to modify it, using toolbar.
Earlier my code looked like:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MAIN", "CREATE");
initViews();
setListeners();
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
});
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
NavigationDrawerFragment.java
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
((MainActivity) getActivity()).getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
if (mDrawerListView != null) {
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
Now i modified my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
initViews();
setListeners();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
shouldDisplayHomeUp();
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
mBackCount++;
shouldDisplayHomeUp();
}
});
}
NavigationDrawerFragment.java
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
((MainActivity) getActivity()).getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
if (mDrawerListView != null) {
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
And shouldDisplayHomeUp function:
public void shouldDisplayHomeUp (){
boolean canBack = getFragmentManager().getBackStackEntryCount() > 0;
mNavigationDrawerFragment.getDrawerToggle().setDrawerIndicatorEnabled(!canBack);
}
But the back arrow is not shown
I tried call
getSupportActionBar().setDisplayHomeAsUpEnabled(canBack);
getSupportActionBar().setHomeButtonEnabled(canBack);
So the back arrow appers but clicking on it has no effect
From the docs:
To allow Up navigation with the app icon in the action bar, call
setDisplayHomeAsUpEnabled():
#Override public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true); }
This adds a left-facing caret alongside the app icon and enables it as an action
button such that when the user presses it, your activity receives a
call to onOptionsItemSelected(). The ID for the action is
android.R.id.home.
This means that you will have to implement your back routine on onOptionsItemSelected and check for R.id.home. To avoid calling the routine when you click on the hamburger menu check for canback too on onOptionsItemSelected.
http://developer.android.com/training/implementing-navigation/ancestral.html#up
EDIT
To archieve what you want you will have to implement your own navigation routine.
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(shouldBack()) {
//call onbackpressed or something
if(displayBackAgain)
return; //return after so you don't call syncState();
}else if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
mNavigationDrawerFragment.openDrawer();
mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();
}
});
}
To enable the backbutton icon just call getSupportActionBar().setDisplayHomeAsUpEnabled(true); to disable it just call mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();
I found a way to control the back button and the nav. It worked with me.
First , set up:
private void setupNav () {
this.toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(this.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.mActionBarDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, this.toolbar, 0, 0);
this.mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//catch back button here.
}
});
this.mDrawerLayout.setDrawerListener(this.mActionBarDrawerToggle);
this.mActionBarDrawerToggle.syncState();
}
Important thing, this is the way I hide the hamburger and show the back button. You have to put this code in the place you want to show back button. I also lock the Nav when showing back button.
if (!isShowBackButton) {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//enable back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Categories

Resources