My application uses ActionBarCompat library as well as the NavigationDrawer support library.
I use ActionBarDrawerToggle appcompat v7 to get the drawer. There are a custom search view on ActionBar. Like this:
But the drawer indicator shows wrongly, doesn't show the Back Arrow when action search view is expanded;
I want it to show like PlayStore application:
How can I do it? Thanks in advance.
There is simple and quick solution to this.
First, you should know now the android.support.v4.app.ActionBarDrawerToggle is deprecated.
You must replace that with android.support.v7.app.ActionBarDrawerToggle.
Here is an Example showing the same.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
After that use support Action Bar as shown here in this documentation and then your drawer indicator will show correctly also showing the Back Arrow when action search view is expanded.
Remember to use com.android.support:appcompat-v7:21.0.+" (Api level 21)
and android.support.v7.app.ActionBar
You can set up the support library using this guide.
AND after that your drawer indicator will perfectly look like this..!!!
You got that issue because you set (android:)homeAsUpIndicator in your theme. Remove that attribute will solve your problem.
This is not the arrow from ActionBarDrawerToggle. I think Google uses Toolbar as they do in google io app. On OnClick event they just change toolbar navigation icon with toolbar.setNavigationIcon(R.id.ic_up). And R.id.ic_up - is a custom arrow drawable.
For me none of solutions posted here in SO worked. I had to look under the hood of support library to find out why and when is the home icon set and I noticed few things. The main observation is that the icon is set in this function:
android.support.v7.widget.Toolbar#setNavigationIcon(android.graphics.drawable.Drawable)
on the line
mNavButtonView.setImageDrawable(icon);
If you are facing the same problem as I was and none of suggested solutions work (setting theme, trying to call setNavigationIcon on toolbar, setHomeAsUpIndicator on Actionbar or even something else), I suggest to locate function mentioned above and put breakpoint there. You can then see when the function is called and identify the last function call (from the Frame window in android studio) that sets up the icon. For me it was this activity life-cycle method syncing navigation drawer:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
mToolbar.setNavigationIcon(R.drawable.ic_hamburger);
}
I simply added last line and it worked.
Related
I've just begun work on a small little app and would like to change the Toolbar title to the name of the fragment the user has selected from the nav drawer, however upon setting the Toolbar as the action bar to be able to use :
getSupportActionBar.setTitle("[insert category here]")
The toolbar's hamburger button stops opening the nav drawer when clicked. (You can still open it via dragging in from left).
Does anyone know how to fix this and/or does anyone know of another method to change the toolbar's title? (I found this method via googling).
Try this in your Activity:
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
And this is for your Fragment(you can use it on OnCreate or onResume method):
#Override
public void onResume(){
super.onResume();
// Set the title
((MainFragmentActivity) getActivity()).setActionBarTitle("Your title");
}
Also, have a look at:
Change ActionBar title using Fragments
Setting Custom ActionBar Title from Fragment
this worked well with me hope it helps anyone facing the same problem (this fix the problem when you press on the hamburger icon to open the drawerLayout)
setSupportActionBar(binding.appBarMain.toolbar)
//to open the navBar by pressing on the hamburger Icon
ActionBarDrawerToggle(this, binding.drawerLayout,binding.appBarMain.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
I have created a new project in eclipse with the navigation drawer and instead of the three lines icon on the top left of the screen I have the back arrow icon. I have found nothing on stack over flow which seems to help.
I have tried to change the setDisplayHomeAsUpEnabled and setHomeButtonEnabled but it does not help
here is a part of the code (by the way its the default code from eclipse)
any one have and idea?
I believe you can try setting custom activity back button to Hamburger button.
Here is the code.
So you can change it programmatically easily by using homeAsUpIndicator() function that added in android API level 18 and upper.
ActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
If you use support library
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
You can change your styles.xml file to replace the navigation drawer icon adding this code:
<item name="homeAsUpIndicator">#drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">#drawable/ic_drawer</item>
The ic_drawer icon (which you'll put in drawable folder) can be downloaded on the web.
To sync the state of the drawer indicator/affordance with the linked DrawerLayout just add code below to your activity
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
With the new NavigationView is it still recommended to use ActionBarDrawerToggle or is this not "Material Design"? For instance previously we were supposed to hide action bar items when the drawer was opened but now the guidelines say that they should stay.
With the new NavigationView is it still recommended to use ActionBarDrawerToggle
No, it's not required.
If you look at the "official" demo code for the new Design Library, ActionBarDrawerToggle is no longer used, since the new NavigationView and AppCompatActivity don't really need it.
With the new v22 support library, you can strip out all of your ActionBarDrawerToggle code and just use the following to handle the interaction between NavigationDrawer and the ActionBar/ToolBar hamburger icon:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
....
}
return super.onOptionsItemSelected(item);
}
You will need to provide your own "hamburger" drawable (R.drawable.ic_menu in my example). Besides that, the above code is all that's needed to handle opening of the drawer. The android.R.id.home case in onOptionsItemSelected() represents your hamburger drawer button. It points to a built-in resource id (not something you add to you menu xml), and it's handled automatically.
Besides that, you have to implement closing of the drawer by simply adding closeDrawers() to your click listener, like this:
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Handle menu item clicks here.
drawerLayout.closeDrawers();
return true;
}
});
closeDrawers() is a method of DrawerLayout, and takes care of everything. That's it. That's all the code you really need to properly handle navigation drawers now. No more messy code for flipping hamburgers and such!
Of course, if you really want to, you can still use NavigationView with ActionBarDrawerToggle the old way. But you certainly don't have to.
If you want drawer callbacks
Even though ActionBarDrawerToggle is not required to open/close the drawer, it may still be useful for handling additional callbacks (especially if you're using ActionBar already). Otherwise, you can implement your own by using DrawerLayout.DrawerListener or by using DrawerLayout.SimpleDrawerListener(), to handle other open/close related events.
With the new NavigationView is it still recommended to use ActionBarDrawerToggle
Yes. The two tackle two completely different aspects of the navigation drawer.
In total, there are generally three components to a navigation drawer:
A DrawerLayout
Your navigation drawer content
A method of showing and hiding the drawer
The DrawerLayout is the layout that holds the navigation drawer content and your app's content. It is what allows you to pull the drawer in from the side and display the drawer over your app's content (the first child of the DrawerLayout).
Your navigation drawer content (the second child of your DrawerLayout) is typically a list of items that the user can click on. Previously, most implementations that I have seen used a ListView or a RecyclerView and maybe a header of some sort. NavigationView is a replacement for this, and is used to provide Material-compliant drawer contents.
ActionBarDrawerToggle is used to provide the hamburger icon in your app bar. It is what allows your users to tap on the icon to open or close your drawer.
Completing the other answers, the Navigation View should be fit into the whole screen in terms of height so it will hide the hamburger icon when opened. Because of this, having the animation from burger to arrow or even just showing the arrow is not necessary.
But when clicking on the current screen it goes to another fragment, imagine a gallery of photos and clicking on a photo will show it bigger, there should be an animation from burger to arrow and the arrow should stay and when pressed there should be a reverse animation to the burger so the navigation view can be opened again.
You can achieve this with ActionBarDrawerToggle still, even with navigation view because it uses the same DrawerLayout as before. So it still has uses, but of course not necessary.
OK, that was a clumsy title :(
Here's the description of the problem. There are two Activities: MainActivity & PreferenceActivity.
The MainActivity has drawer set up (per tutorial) and working fine (behavior, three-line icon, everything).
The problem is that the PreferenceActivity also shows the R.drawable.ic_drawer, this is not as intended.
Intended behavior is to have < icon in the PreferenceActivity action bar.
The only changes to PreferenceActivity are:
onCreate() I have added getSupportActionBar().setDisplayHomeAsUpEnabled(true); and
I have overriden onOptionsItemSelected() (to handle android.R.id.home click to track some analytics).
I have tried suggestions from this question. Based on that, I have added setDrawerIndicatorEnabled(true); in the MainActivity.onResume() and have added setDrawerIndicatorEnabled(false); in MainActivity.onPause(), but w/o any effect.
Ideas hove to solve this?
Could it be that the setDrawerIndicatorEnabled(false) doesn't correctly revert the indicator because ActionBarSherlock defines the attribute for it (in its theme) as "homeAsUpIndicator" and not as "android.R.attr.homeAsUpIndicator" (as described here)?
The latest edit of the question (regarding ABS) got me thinking.
Since I had problems with drower icon not moving, I have added this to my theme (extending ABS theme):
<item name="android:homeAsUpIndicator">#drawable/ic_navigation_drawer</item>
Since actually this fixed the mentioned problem (of drawer icon not moving):
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
the attrib override in the theme was a leftover and was a root cause for < icon not showing on lower levels as it should, so removing the "android:homeAsUpIndicator" override fixed my problem.
Can we launch navigation drawer without using ActionBarDrawerToggle? In my implementation, I do not want to use action bar.
So, I don't want ActionBarDrawerToggle to open and close the drawer. I want to open the navigation drawer when hardware menu button is clicked.
I am targeting Android 2.2 and using support library v4.
As per my understanding, ActionBarDrawerToggle class will have implementation internally to handle the opening and closing of navigation drawer. Am I correct?
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageView ivNavigationIcon = //some image or button or menu what ever you want
ivNavigationIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
I want to open the navigation drawer when hardware menu button is clicked
Not every Android device has a MENU button, so you better have some other solution beyond just that.
Also, using MENU to open a navigation drawer, instead of a menu -- may be viewed as odd behavior by some of your users.
I am targeting Android 2.2 and using support library v4.
If you are using DrawerLayout, it has openDrawer() and closeDrawer() methods that you can use that will open the drawer and close the drawer, respectively.