ActionBar with support library and Fragments overlay content

16,346

Solution 1

Edit: This is now officially fixed and released in the Support Library v19.

As JJD commented below, you can use normally android.R.id.content with appcompat-v7 r.19.0.0 or newer. The home button works too.

With other words: The workaround below is no more needed if you use version 19.0.0 or newer.


I got the answer at code.google.com. i've made a summary from frederic's answer:

For pre ICS devices you must use:

R.id.action_bar_activity_content

instead of

android.R.id.content

R.id.action_bar_activity_content is a new id used in layout for displaying app content, it would appear that it replace android.R.id.content when you use support v7 appcompat ActionBarActivity.

You can use this code to retrieve the correct id of the activity content :

public static int getContentViewCompat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
               android.R.id.content : R.id.action_bar_activity_content;
}

Thanks to frederic

Solution 2

Another alternative if you do not want to modify the source code of android-support-library-v7-appcompat is to add an empty layout in the layout xml file such as:

<LinearLayout
    android:id="@+id/content_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and make the fragment call to add to that layout instead:

ft.add(R.id.content_view, mFragment, mTag);

Solution 3

Seems a bit late to contribute but I had the same problem and haven't seen the answer here....

Check your styles.xml file, there might be an xml attribute for overlaying the actionbar set to true ie

true

The whole entry looks something like this

    <item name ="actionBarTabTextStyle" > @style/TabTextStyle</item>
    <item name = "windowActionBarOverlay">true</item>

If that is the case then just change the value of "windowActionBarOverlay" to false.

Share:
16,346
Primoz990
Author by

Primoz990

Updated on June 09, 2022

Comments

  • Primoz990
    Primoz990 almost 2 years

    I added the android-support-library-v7-appcompat to my project to support ActionBar from API level 7 above.

    It works like a charm on android 4.0+ and also on android 2.3 with a normal Activity that has setContentView in onCreate, but when the activity is loading an Fragment in onCreate the ActionBar gets overlapped with the content of my layout. At all other scenarios the ActionBar works well.

    Here is some code:

    class AssetsActivity extends ActionBarActivity{
        @Override
        protected void onCreate(Bundle arg0)
        {
            super.onCreate(arg0);
    
            OpenLocalFragment assets = OpenLocalFragment.newInstance(index);
                assets.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, assets).commit();
        }
     }
    

    The theme of this activity is set in the manifest to:

    @style/Theme.AppCompat

    An this is the result on android 2.3 (on 4.0+ the ActionBar shows well) actionBar overlaping

    You can se that the first lisview item is overlaping the ActionBar(White round icon and title "My activity")

    It is possible that i found a bug in the support library, it is released only for 2 days now? Thanks to all.

  • Primoz990
    Primoz990 almost 11 years
    In this case is this a simple workaround but i'm using this principe in many activities with different layouts. I will post an edit with my current workaround. Thanks anyway, could be someones solution.
  • Primoz990
    Primoz990 over 10 years
    NOTE: this is now officially a bug in Android. They fixed it and it will be released in the future. code.google.com/p/android/issues/detail?id=58108
  • JJD
    JJD over 10 years
    Please note that using appcompat-v7 r.19.0.0 introduces an new bug affecting devices running Android 2.3.x as discussed in issue #59077.
  • JJD
    JJD over 10 years
    In addition to Primoz990 answer: You can use android.R.id.content if you use appcompat-v7 r.19.0.0 or newer. Chris Banes fixed issue 59077 so the home button works on Android 2.3 and newer. You do no longer need the workaround referencing R.id.action_bar_activity_content.
  • Santacrab
    Santacrab about 10 years
    I'm using support library version 19.0.1 but the fragment is still overlayed. Tried to see if the workaround would work, but if I use the replace() on R.id.action_bar_activity_content for Android 2.3, I got a "No view found for id 0x7f050015 (mypackage:id/action_bar_activity_content)". What can I try? Edit:I just used eightyhertz solution, adding an empty LinearLayout and adding the view there
  • Gunnar Bernstein
    Gunnar Bernstein about 10 years
    You may try to add .addToBackStack(null) on your fragment transactions. I had some issues if you replace a fragment A with fragment B (addtobackstack), than replace B with C (no backstack) and then hit the back button. C is not paused, A is shown overlapping with C.