Button always displays on top in FrameLayout Button always displays on top in FrameLayout android android

Button always displays on top in FrameLayout


This answer

Buttons in Lollipop and higher have a default elevation to them whichcauses them to always draw on top. You can change this by overridingthe default StateListAnimator.

Try putting this into your button XML:

android:stateListAnimator="@null"

The FrameLayout should now cover thebutton.


In the Android 5.0 (API 21) and above, you must add android:elevation into the view.

<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"        android:elevation="3dp"/>


Put your Button inside FrameLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <FrameLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="new button" />    </FrameLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="some text" /></RelativeLayout>