Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically turn off android:windowActionBarOverlay style from action bar

Currently, I am using ActionBarSherlock. I want to launch SecondActivity from MainActivity.

MainActivity is using action bar with windowActionBarOverlay style turned on. SecondActivity is using action bar with windowActionBarOverlay style turned off. Hence, here is how my XML looks like.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"  
    android:debuggable="false" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/ThemeWithActionBarOverlay"
        android:screenOrientation="nosensor" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".SecondActivity" 
        android:theme="@style/ThemeWithoutOverlay">
    </activity>
</application>

<resources>
    <style name="ThemeWithActionBarOverlay" parent="@style/Theme.Sherlock">
        <item name="android:windowActionBarOverlay">true</item>
        <item name="abIcon">@drawable/ic_home</item>
        <item name="abTitleTextStyle">@style/ActionBarCompatTitle</item>
    </style>

    <style name="ThemeWithoutOverlay" parent="@style/Theme.Sherlock">
        <item name="abIcon">@drawable/ic_home</item>
        <item name="abTitleTextStyle">@style/ActionBarCompatTitle</item>
    </style>
</resources>

However, by doing so, in SecondActivity, I realize I can never have a up/back button on the top left of action bar. Although there is icon being shown, it is not pressable. Only by using back same theme (ThemeWithActionBarOverlay) as MainActivity, only up/back button will shown. However, if I let SecondActivity to use same theme as MainActivity, I find out no way to turn off windowActionBarOverlay behaviour.

// SecondActivity
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.history_list_activity);

    ActionBar actionBar = this.getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    // How to turn android:windowActionBarOverlay attribute to false during runtime?
    // actionBar.??? 
}

My questions are

  1. Why the child activity has to use action bar with same theme as parent's, in order to have proper up/back button shown? Is there any way I can use different themes, yet have up/back button appears on child activity?
  2. Is it possible to turn of windowActionBarOverlay style during runtime?
like image 385
Cheok Yan Cheng Avatar asked Mar 19 '12 03:03

Cheok Yan Cheng


1 Answers

To answer your first question, you don't need to have the parent and child activity using the same theme for the 'Up' button to work. In fact, I'm working on a similar parent/child activity application, and its working just fine using two different themes( a theme without overlay for the parent, and the fullscreen theme (with overlay) for the child).

There must be another reason why it is not working...

  • Make sure you have defined MainActivity to be the parent of second activity. You can do that either by code, or the prefered way, in the AndroidManifest.xml:

    <activity
        android:name=".SecondActivity" 
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
    
  • Make sure that in your child activity, you have activated the 'up' navigation:

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

For your second question, try overriding the 'windowActionBarOverlay' in the 'ThemeWithoutOverlay' theme to false:

<style name="ThemeWithoutOverlay" parent="@style/Theme.Sherlock">
    <item name="android:windowActionBarOverlay">false</item>
    <item name="windowActionBarOverlay">false</item><
    <item name="abIcon">@drawable/ic_home</item>
    <item name="abTitleTextStyle">@style/ActionBarCompatTitle</item>
</style>
like image 79
tinesoft Avatar answered Sep 29 '22 11:09

tinesoft