-1

I'm trying to implement simple view overlay using RelativeLayout but it seems like something is wrong, I want the last view in the layout to be rendered on top of the rest but it is actually rendered behind. can you spot whats wrong in my code?

edit: figured out it only happens if I want to overlay buttons with some other view

enter image description here

here is the view xml:

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:layout_alignParentTop="true"
    android:id="@+id/btn1"
    android:text="Normal View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<FrameLayout
    android:background="#ff0000"
    android:layout_width="match_parent"
        android:layout_height="match_parent"/>

 </RelativeLayout>
fire in the hole
  • 1,171
  • 15
  • 34

1 Answers1

1

buttons and other elements in API 21 and greater have a high elevation, and therefore ignore the xml order of elements regardless of parent layout. thanks to this answer.

The solution to this bug is to set a higher elevation on the view which I want to be rendered over the button, the elevation value for the overlaying view should be higher than the button's.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            view.setElevation(1000);
}

or in xml

<FrameLayout 
    android:elevation="1000"
...
fire in the hole
  • 1,171
  • 15
  • 34