0

I have created a actionbar with a custom xml layout that should have a transparent background. It works as it should on ICS but on KitKat it has a gray background instead of the transparency. Can anyone help me solve this issue and make the actionbar transparent for all Android versions 4 and up?

Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_actionbar_layout"
    android:background="@android:color/transparent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <ImageButton
        android:id="@+id/action_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/menu" />

    <ImageButton
        android:id="@+id/action_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/settings" />

    <ImageButton
        android:id="@+id/action_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/action_settings"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/action_settings"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/share" />

    <com.quanticapps.athan.views.RobotoTextView
        android:id="@+id/action_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/app_name"
        android:textColor="@android:color/white" />

</RelativeLayout>

And here is the code that I use:

final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
                .inflate(R.layout.main_actionbar, null);
        // Set up your ActionBar
        final ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);
        // Typeface mTypeface = Typeface.createFromAsset(getAssets(),
        // "rockwell.ttf");
        title = (TextView) findViewById(R.id.action_title);
        share = (ImageButton) findViewById(R.id.action_share);
        settings = (ImageButton) findViewById(R.id.action_settings);
        menu = (ImageButton) findViewById(R.id.action_menu);

        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

        title.setTextSize(15);
        title.setGravity(Gravity.CENTER);
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

2 Answers2

0

try these way..

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));

check this answer

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

Its always better to set Actionbar property in style. Define a style where set the actionbar background to transparent and apply that style to your activity. This way your code will be free from handling UI and logic.