2

I would like to have a transparent actionbar with only two button on left and right.

for that I wrote in my Activity :

protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);

    if(getResources().getBoolean(R.bool.portrait_only)){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    actionBar = getSupportActionBar();
    LayoutInflater mInflater = LayoutInflater.from(this);
    actionBarView = mInflater.inflate(R.layout.actionbar_menu_button, null);
    actionBar.setCustomView(actionBarView);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);

    actionBarLeftButton = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_left_button);
    actionBarTitle = (TextView) actionBar.getCustomView().findViewById(R.id.actionbar_title);
    actionBarRightButton = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_right_button);
}

in style.xml I have :

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="android:textColor">@color/oeo_grey_5</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="Theme.AppCompat">
    <item name="android:background">@color/oeo_transparent</item>
    <item name="android:backgroundStacked">@android:color/transparent</item>
</style>

and my action bar layout is :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:paddingRight="20dp"
>
<ImageView
    android:id="@+id/action_bar_left_button"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:src="@drawable/actionbar_menu_button"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    />

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TITLE"
    android:gravity="center"
    android:textSize="18sp"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColor="@color/oeo_grey_2"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />

<ImageView
    android:id="@+id/action_bar_right_button"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:src="@drawable/actionbar_account_button"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    />
</RelativeLayout>

But I always have a kind of black / dark grey background even if the overlay setting works correctly. What did I miss?

UPDATE After looking to this link

I made the following modifications :

@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);

    if(getResources().getBoolean(R.bool.portrait_only)){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.oeo_transparent)));
    actionBar.setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.oeo_transparent)));
    LayoutInflater mInflater = LayoutInflater.from(this);
    actionBarView = mInflater.inflate(R.layout.actionbar_menu_button, null);
    actionBar.setCustomView(actionBarView);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);

    actionBarLeftButton = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_left_button);
    actionBarTitle = (TextView) actionBar.getCustomView().findViewById(R.id.actionbar_title);
    actionBarRightButton = (ImageView) actionBar.getCustomView().findViewById(R.id.action_bar_right_button);

and it worked. (I was also able to remove the style MyActionBar from styles.xml)

Community
  • 1
  • 1
Labe
  • 1,262
  • 2
  • 20
  • 30
  • 1
    Have a look at this question : http://stackoverflow.com/questions/22353011/transparent-actionbar-is-not-working – Josef Feb 15 '15 at 18:27

1 Answers1

0

MyActionBar style should be :

<style name="MyActionBar"
       parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@android:color/transparent</item>

</style>

or you make a transparent drawable for actionbar then:

    <item name="android:background">@drawable/actionbar_transparent</item>

Above parent theme is for DarkActionBar, otherwise your parent theme should be:

parent="@style/Widget.AppCompat.Light.ActionBar

insdead of:

parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse
Xcihnegn
  • 11,579
  • 10
  • 33
  • 33