Joey Yi Zhao
Joey Yi Zhao

Reputation: 42520

How to set actionbar transparent background

I use below code to show an actionbar on the activity. But the background of the action bar is not transparent. I have searched some articles about how to set the actionbar background transparent but it doesn't seem to work for me. I have attached all the layout and theme code as below. Please help to check what I am missing here.

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scanner_activity);

    Toolbar toolBar = (Toolbar)findViewById(R.id.scanner_toolbar);
    toolBar.setTitle("Title");
    setSupportActionBar(toolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Below is the activity layout xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.v7.widget.Toolbar
    android:id="@+id/scanner_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@android:color/transparent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:theme="@style/AppScannerToolBarTheme"
    />

<com.journeyapps.barcodescanner.DecoratedBarcodeView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/scanner_toolbar"
    android:layout_alignParentBottom="true"
    android:id="@+id/zxing_barcode_scanner"
    app:zxing_use_texture_view="true"/>


</RelativeLayout>

Below is the AppScannerToolBarTheme for the toolbar:

<style name="AppScannerToolBarTheme"  parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:backgroundStacked">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
</style>

EDIT:

I have added below code but it still doesn't work:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
toolBar.setAlpha(0.5f);
toolBar.setBackground(new ColorDrawable(Color.TRANSPARENT));

Below image is what I want to achieve:

enter image description here

Upvotes: 0

Views: 2530

Answers (2)

erluxman
erluxman

Reputation: 19415

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000")));

OR

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Hope this will work for you thanks .

Upvotes: 2

NIPHIN
NIPHIN

Reputation: 1081

Toolbar is a view, so you could use alpha method from view class.

toolbar.setAlpha(0.5f);

Upvotes: 0

Related Questions