Up icon not showing in lower levels when Nav. Drawer is configured - android

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.

Related

Android Navigation Drawer little "bug"

I'm very, VERY new in android. So, I started with a simple application, created on min API 14, I personalized some strings and I started to modify the application drawer.
When I tried to debug my simple app and launch it on my Nexus 5 (updated to last stock lollipop), I have this little annoying bug, with this icons:
In all cases, when the navigation drawer is opened/closed it shows that ARROW, instead of changing between the "menu" icon and "arrow" icon.
What I'm doing wrong?
Searching for a solution, I found some lines in code, like this:
I don't know how to do... If you could explain me what's wrong, I'll be very happy.
You need to syncState() call of your ActionBarDrawerToggle object from onPostCreate():
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

Action Bar looks old in only one activity, but the rest look ok

I ran into a bug when I was making custom themes to use different action bar colors.
My action bar looks like this everywhere in my app except my list activity:
My list activity's action bar looks like this:
I tried looking at old commits I had saved and tried to copy their android_manifest themes and parent themes, but nothing changed. What could possibly have caused this bug?
I realized that my problem was because I had typed in setTheme(android.R.style.Theme_Black); in the activity that had the issue (the list activity). I needed to delete this, and then it worked. I forgot that you could override the theme programmatically, and that I should check my list activity for any "setTheme()" methods.
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("SongListActivity", "onCreate(Bundle savedInstanceState)");
setTheme(android.R.style.Theme_Black); //<----- BUG
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songlist_list);

Navigation drawer icon arrow instead of three lines

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();
}

Drawer Indicator on ActionBar compat v7 21

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.

ActionBarCompat change item icon

I am working on an application for API 7+ that implements ActionbarCompat.
Currently I have an activity with a "favourite" icon on the ActionBar and I am trying to find a way to toggle the graphic between favourite and not favourite.
All my efforts with playing around in onCreateOptionsMenu, onPrepareOptionsMenu and onOptionsItemSelected have failed, it appears the icon can only be changed at the time of creation and not while the activity is already running.
Has anybody come across a way to change one of these ActionBarCompat item icons at run time while the activity is visible using ActionBarCompat and still maintain compatability with the native ActionBars on APIs 11+?
You need to implement some basic logic in onCreateOptionsMenu() that checks whether the item is favourited or not and changes the icon based on that logic.
In your setFavourite() method, you'll need:
setFavourite() {
....
if (changedFavourite)
invalidateOptionsMenu()
See documentation here: http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()
That should force ABS to redraw.
Inspired by #mobinvent's comment I found that the following gives pre HoneyComb devices utlising ActionBarCompat the options menu lifecycle sequencing as per invalidateOptionsMenu() on HoneyComb and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) invalidateOptionsMenu();
else {
supportInvalidateOptionsMenu();
openOptionsMenu();
closeOptionsMenu();
}
Note the hacky requirement to open the options menu and close it. In my particular instance I call this from onActivityResult which at this stage does not appear to introduce any ugly menu flicker.
Good riddance to pre ActionBar OS / devices!

Categories

Resources