I am trying to show Icon_drawble in a ActionBar, but when R.drawable.ic_drawer is in the first position show return arrow in actionbar.
like this:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
I need to show something like this, but the code above It doesnt work.
If I change R.drawable.ic_drawer to another position,it give me the follow logcat error.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.drawer_open,
R.drawable.ic_drawer,//another position
R.string.drawer_close
)
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navegatiodrawer/com.example.navegatiodrawer.MainActivity}: android.content.res.Resources$NotFoundException: File Open navigation drawer from drawable resource ID
This appears to be a bug in recent versions of the AppCompat library.
Part of the confusion is likely due to the fact that Android Studio's new project wizard generates bad code when you create a new navigation drawer activity- it uses the v4 support library ActionBarDrawerToggle, which is deprecated. Instead, it should use the v7 support library ActionBarDrawerToggle.
You have two options:
The best option is to switch to the v7 ActionBarDrawerToggle. To do this, change your imports to use android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle. The only other change you should need to make is removing your ic_drawer parameter altogether- the newer version of the toggle generates the hamburger you are looking for automatically.
If you insist on using the v4 toggle or a custom icon, you can revert to an older version of the support library. Using the default generated project when creating a new navigation drawer activity, I was able to sidestep this bug by reverting to com.android.support:appcompat-v7:22.1.0 in my build.gradle.
There is already a bug in the issue tracker for updating the wizard-generated code. I wouldn't expect the v4 toggle to get fixed since it is deprecated.
Related
I have an activity in my app which is not launcher.
I want to add a navigation drawer with toggler. I tried following the instructions here http://developer.android.com/training/implementing-navigation/nav-drawer.html#ActionBarIcon
But my back navigation still shows up (It exits the app as I have added appropriate flags to the intent)
How can I show the drawer toggler icon ?
Also android.support.v4.app.ActionBarDrawerToggler is deprecated, what should I use instead ?
for android.support.v4.app.ActionBarDrawerToggler is deprecated to use new one get support library supporv7 appcompact check here https://developer.android.com/intl/es/reference/android/support/v4/app/ActionBarDrawerToggle.html
and creating Navigation drawer here you get all information
http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html
code sample
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.setDrawerListener(toggle);
toggle.syncState();
use the latest support library you will get done it easily and not use setHomeasUpEnabled(true)
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.
I have a problem with the new Animation from the drawer look here on the G+post from me: Link to the Picture
How can I replace the app icon with the new drawer animation on API19 and lower devices? I want it to look like the ones from PlayStore, Newstand, etc.
You're going to need the new support V7 library and will need to set the Action Bar Drawer Toggle as such - with this being your activity/context:
ActionBarDrawerToggle navigationToggle = new ActionBarDrawerToggle(this,navigationDrawerLayout,R.string.nav_drawer_open,R.string.nav_drawer_closed);
navigationDrawerLayout.setDrawerListener(navigationToggle);
Where ActionBarDrawerToggle is inherited from android.support.v7.app.ActionBarDrawerToggle
It is absolutely necessary that your new Activity extend ActionBarActivity provided by android.support.v7.app.ActionBarActivity otherwise the action bar will not show up. You'll likely have to adjust all your action bar references from getActionBar() to getSupportActionBar(). Let me know if there's anything I can help with as I just transitioned 2 apps into the new guidelines using the SupportV7 library.
What is stated by Logan is correct but it does not require that much changed.
If you already have a drawer then you will most likely have code similar to this.
import android.support.v4.app.ActionBarDrawerToggle;
//some other code
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout,
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open,
R.string.drawer_close)
If so then all you need to do is to remove the line that is comment above, and change to the v7 library, so that you get something like this.
import android.support.v7.app.ActionBarDrawerToggle;
//some other code
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close)
There is no need to change to inherit anything other than the standard Activity; and using a theme such as holo.light.darkactionbar or any other action bar related theme works just fine.
I want to change the color of ic_drawer to white. Yes the three lines icon that you can see below.
Could you point me to any solution via xml?
Currently, I have the white icons ready.
It is not possible by just setting a color code, you have to do it with custom images.For generating a navigation drawer indicator icon, use the Android Asset Studio.
Insert the images in your res/drawable-XXXX folder (replace XXXX with hdpi, mdpi, etc.), under a name like custom_ic_drawer.
If you followed the tutorial on the Android Developer portal for creating a navigation drawer, you created a drawer toggle with the following code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)...
Replace this line with the following:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.custom_ic_drawer, R.string.drawer_open, R.string.drawer_close)...
This way the toggle will have your custom icon.
If you want to change the icon for the Up navigation (the grey caret on your left picture, see this question.
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.open_drawer,
R.string.close_drawer
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
This is part of my code I'm using to set the navigation drawer and the app icon to toggle it.
I'm very confused in that why is it required to setDrawerListener when already while constructing the DrawerToggle object we have specified the DrawableLayout in the constructor.
Second, why is the setDisplayHomeUpEnabled required when we are actually not enabling it. And without it, why isn't the ic_drawer displayed?
Thanks.
From documentation link
ActionBarDrawerToggle can be used directly as a DrawerLayout.DrawerListener, or if you are already providing your own listener, call through to each of the listener methods from your own.
So setDrawerListener method is for setting drawer listener, it could be custom one or you could use your ActionBarDrawerToggle object because it already implements DrawerLayout.DrawerListener.
Setting setDisplayHomeUpEnabled is showing < sign in your action bar but when using navigation drawer it shows three lines.
This is how it is implemented.