Home » Android » Android: Make Actionbar overlap Activity layout

Android: Make Actionbar overlap Activity layout

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an activity where the Actionbar is hidden and when I click a button I want it to be visible, but I don’t want the layout activity to scroll down, the bar must overlap it.
How can I do such a thing?

To hide the bar i use

actionbar.hide();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

And to make it visible:

actionbar.show();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

My layout is just

<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"
    >

    <MyPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <MyPager>

</RelativeLayout>

Thanks in advance.

How to&Answers:

You are probably looking for the ActionBar overlay mode. Enable it for a single Activity by calling requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); before setContentView(). Or in a theme by setting android:windowActionBarOverlay to true.

Answer:

Create a RelativeLayout for the parent layout of the activity and put your action bar at the top of the layout which will overlay it on top of the main activity content as shown in the example below.

The RelativeLayout allows you to position elements but also allow you to determine the z-index order of view elements by there order from top to bottom i.e. which views are in front/behind of others.

<RelativeLayout
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/action_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="100dp">

    </LinearLayout>
    <LinearLayout
        android:id="@+id/main_activity_content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</RelativeLayout>

Now you can hide/show the action_bar_layout and it won’t effect the positon of the activity content in main_activity_content_layout (by using setVisibility() method of the layout)

Answer:

For Google Play Store like effect, try…

  • Try this new Toolbar widget introduced in Lollipop (API Level 21)
  • Which is also supported by this api in earlier versions of android.