Button always displays on top in FrameLayout
Asked Answered
E

8

53

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

Esculent answered 31/8, 2015 at 8:47 Comment(2)
you can use Linear-layoutFloater
Possible duplicate of Incorrect overlay behavior in FrameLayoutRozamond
R
101

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.

Rozamond answered 8/10, 2015 at 14:14 Comment(2)
this answer saved me. but i would prefer to add the system button elevation android uses to the other views in the framelayout. how can i get the button elevation dp valu android is setting ?Cordiacordial
Yes, this removes animation which uses elevation. Your button now become a simple ImageView. The proper solution is to adjust other items elevation to be higher than your button which is accepted answer.Lingua
S
22

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"/>
Smolensk answered 2/3, 2016 at 8:42 Comment(0)
H
3

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>
Herculaneum answered 25/12, 2017 at 14:47 Comment(0)
Y
2

As the official android documantation points out:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

It's better if you put your Button and Textview in a RelativeLayout inside the FrameLayout like:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text"/>
        <Button
            android:id="@+id/button1"
            android:layout_below="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="changeColor"
            android:text="new button"/>
        <RelativeLayout>
    </FrameLayout>
Yelp answered 31/8, 2015 at 8:51 Comment(0)
H
2

Apperently android:stateListAnimator="@null" works only for API = 21 or higher, So for those who target API<21 use this, it worked for me :D

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <FrameLayout
    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"/>
    </FrameLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"/>
</FrameLayout>
Housewares answered 27/11, 2018 at 19:52 Comment(0)
F
1

FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

You should use LinearLayout or RelativeLayout in FrameLayout . Like this way

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

   <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_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"/>
   </RelativeLayout>

</FrameLayout>
Floater answered 31/8, 2015 at 8:53 Comment(1)
Why it's difficult? You just could put in the order they get in xml? And that's how documentation explains it. I just can get how it works. Why it makes difference with buttons and imageviews. It shouldn't even know about that.Esculent
S
1

For API < 21 you cant use android:stateListAnimator="@null" or change the elevation. In my case I used 2 frame layouts embedded in a constraint layout. As the frame layouts can be stacked upon each other there is no need to change the elevation of the textview.

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <Button
        android:id="@+id/my_daybutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/cal_button_background"
        android:textColor="@color/colorPrimaryDark"
        android:gravity="start|top"
        android:paddingTop="2dp"
        android:paddingStart="2dp"
        android:paddingEnd="2dp"
        />
</FrameLayout>

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bla"
        android:textSize="9sp"
        android:textColor="@android:color/holo_red_dark"
        android:layout_gravity="bottom|end"
        />
</FrameLayout>

Subjection answered 15/6, 2018 at 12:47 Comment(0)
U
0

Just put elevation FrameLayout or any parent there you are going to load you fragment like

android:elevation="@dimen/dimen_20"

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_0"
    android:id="@+id/ready_to_scan"
    app:layout_constraintTop_toTopOf="parent"
    android:elevation="@dimen/dimen_20"
    app:layout_constraintBottom_toBottomOf="parent"/>
Uria answered 9/8, 2021 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.