Search code examples
androidandroid-fragmentsandroid-4.4-kitkat

Android 4 Fragment Action Bar Not Showing


I use Linear Layout inside a Relative Layout. Because of this, the action bar is not showing up. It works fine both on Android 5 and 6. This problem only occurs in Android 4. Could you please help me?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.blabla">
<LinearLayout
    android:paddingTop="60dp"
    android:paddingLeft="@dimen/act_left"
    android:paddingRight="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/background_gray_soft">
....
</LinearLayout>
</RelativeLayout>

Solution

  • For those who uses FrameLayouts with Fragments in ICS, do not change your background with

    android:backgound="..."
    

    It makes the Toolbar color same with your background.(I have no idea why. Still trying to figure it out.)

    You need to check your API version in your Java code and set the background color there.

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fragment_container);
                frameLayout.setBackgroundColor(getResources().getColor(R.color.background_gray_soft));
            }
    

    There is only one problem left and that could not be solved with this method.

    If you want to have a special background color in ICS(I only encountered this problem with Fragments), how will you set that?