androidandroid-framelayout

Button always displays on top in FrameLayout


I have FrameLayout like this:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeColor"
        android:text="new button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"/>

</FrameLayout>

The problem is that the button is displayed on top while FrameLayout class overview tells us this: "Child views are drawn in a stack, with the most recently added child on top".


Solution

  • Update:

    in android 21+ after introduction of elevation one can play with elevation attribute of various widgets to put them on top of one another. here is a material design guide for elevation values.

    for api < 21 :

    This answer

    Buttons in Lollipop and higher have a default elevation to them which causes them to always draw on top. You can change this by overriding the default StateListAnimator.

    Try putting this into your button XML:

    android:stateListAnimator="@null"

    The FrameLayout should now cover the button.