Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar does not show app icon after setDisplayHomeAsUpEnabled(true)

I want to enable the navigating up function in the ActionBar and have followed the document about this at:http://developer.android.com/guide/topics/ui/actionbar.html

But when I check my app (on android 4.0), the app icon does not show up, instead only a left caret is shown.

I have checked the android manifest file, the java file and layout file, but still have no idea of how to get it work....

Here is the AndroidManifest.xml:

...
<activity
        android:name="com.test.HelpActivity"
        android:label="@string/help"
        android:parentActivityName="com.test.HomeActivity"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.test.HomeActivity" />
</activity>
...

And here is the Java code:

...
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class HelpActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help);

        ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

}

Thank you for help!

=====UPDATE=====

I did a few more testing, and here is what I got:

on an Android Emulator (2.3), it works perfect.

on an Android Emulator (4.1.2), it works perfect.

on an Android Emulator (4.0), it seems fine, but when I click the home/up button, nothing happens and logcat displays: bad parentActivityName 'HomeActivity' in manifest

on my phone (4.0.3), only left caret is displayed, yet navigation works fine.

on an Android Emulator (4.3), it works fine, but the focusable area becomes the app icon + app title. (That is when I press anywhere within this long area, it is highlighted and navigation up event is triggered)

I am now totally lost... Guess it could be some bug in certain android versions.

like image 480
Qianqian Avatar asked Apr 14 '14 14:04

Qianqian


2 Answers

add this line below actionBar.setDisplayHomeAsUpEnabled(true);
it works for me

 actionBar.setDisplayShowHomeEnabled(true);
 actionBar.setIcon(R.drawable.ic_launcher);
like image 65
Sumit Avatar answered Oct 07 '22 07:10

Sumit


I think in android 5 and API 21 we can't use app icon for up action.
if your appcompat support library is v7 rev21 i think you can't use the app icon at least for up action. but u can use it instead of top left caret or you can use app icon right of that caret although if u click it nothing happen.

I have two app completely like that trainings in one of them i use the older appcompat v7 and minSdkVersion="11" and i never added getSupportActionBar().setDisplayHomeAsUpEnabled(true);
to it but app have app icon and no problem . but in another app i update my support library a few day ago to rev21 and of course this app's minSdkVersion is "8" but this app hasn't actionBar icon for Up action and if you add top code to it you do nothing. I search many of sites and just i understand is that in material design google removed app icon for up action because of increasing space on actionbar and etc. You can see this line
"When using the Material themes (default in API 21 or newer) the navigation button (formerly "Home") takes over the space previously occupied by the application icon. Apps wishing to express a stronger branding should use their brand colors heavily in the action bar and other application chrome or use a in place of their standard title text."
in this page http://developer.android.com/reference/android/support/v7/app/ActionBar.html
I think. thanks.

like image 23
hitman snipe Avatar answered Oct 07 '22 07:10

hitman snipe