Android – hiding ActionBar icon and title during launch

I was developing an app that had a tabbed interface (FragmentPagerAdapter) inside the main activity. Each tab was setting different ActionBar icon and title inside onTabSelected method. Something like this:


@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
   viewPager.setCurrentItem(tab.getPosition());
   if (tab.getPosition() == 0){
      actionBar.setTitle("title1");
      actionBar.setIcon(R.drawable.icon1);
   }
   if (tab.getPosition() == 1) {
      actionBar.setTitle("title2");
      actionBar.setIcon(R.drawable.icon2);
   }
   if (tab.getPosition() == 2) {
   actionBar.setTitle("title3");
   actionBar.setIcon(R.drawable.icon3);
}
}

This was working fine when the app was running and resuming from paused. But on the initial launch, global application icon and title was appearing inside ActionBar for half a second or so, before being overridden by the code in onTabSelected .

These items (title and icon) were coming from AndroidManifest.xml attributes android:label and android:icon. You can’t simply remove them as they also set app’s launcher label and icon. My first thought was to set android:label and android:icon as blank text and transparent icon inside <activity>Unfortunately main <activity> attributes were overriding whatever was set inside <application>, which made launcher icon and label disappear…

After few hours or reading bunch of stackoverflow posts and trying all sorts of things, I got this resolved. Icon and Title were dealt in different ways.

For the icon I set android:logo=”@android:color/transparent” inside AndroidManifest.xml for <application>


<application
android:allowBackup="true"
android:theme="@style/AppTheme"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:logo="@android:color/transparent">

android:logo attribute, if specified, sets ActionBar icon, but does not affect application launcher icon, which is still decided by android:icon. The only small issue with this was that actionBar.setIcon(…) code insideonTabSelected was not changing icon any more as ActionBar icon was now decided by logo, not icon. This was resolved by simply using actionBar.setLogo(…). Alternative option, which also works, is to put line actionBar.setDisplayShowTitleEnabled(false) inside main activity onCreate method. This forces ActionBar to use icon instead of logo again.

To get ActionBar title sorted I took different approach. First I setup a new theme with no ActionBar.


<style name="ThemeBlankActionBar" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBarBlank</item>
</style>
<style name="ActionBarBlank" parent="android:Theme.Holo.Light"></style>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>

The last <style name=”AppTheme”… element is the original app theme.

I then applied the new ThemeBlankActionBar theme to the main activity via AndroidManifest.xml


<activity
   android:theme="@style/ThemeBlankActionBar"
   android:name=".MainActivity"
   android:launchMode="singleTop">
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

And the last step was to restore the original AppTheme on application launch by inserting line setTheme(R.style.AppTheme); inside the main activity onCreate method, before super.onCreate(savedInstanceState);
This way when application launches it has no ActionBar, but as soon as setTheme(R.style.AppTheme); command is executed, ActionBar comes back (with AppTheme) and then icon and text are immediately set by code in onTabSelected method.

Android Studio Beta 0.8.14
Min SDK: 14
Target SDK: 21


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *