2

Almost every article on the topic of layering widgets leads to FrameLayouts, which are supposed to stack according to order of occurance in the XML. To test, I put a button and a TextView in the xml, hoping to see the TextView overlap the button. It doesn't matter if the button is placed before or after the textview, the button is always on top. Can anyone see what I'm doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jdot.testloadingfragments.MainActivity">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="        "
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="----------------------------------------------------------"/>


</FrameLayout>
John Ward
  • 910
  • 2
  • 10
  • 21

2 Answers2

5

Adding elevation to the textView will make it show above the Button

e.g

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jdot.testloadingfragments.MainActivity">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="        "/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:elevation="2dp"
        android:text="----------------------------------------------------------"/>
</FrameLayout>

More info here: Why is the framelayout not drawing z index correctly?

Community
  • 1
  • 1
Isaac
  • 1,442
  • 17
  • 26
  • Thanks. I'm avoiding elevation, but didn't know that Button is a special case. It doesn't make sense that AppCompat would have themes that force you to use a more current API level for a common design element. – John Ward Jan 11 '17 at 00:38
0

FrameLayout is ideally used to display a single item.

If you want to display multiple child views in Frame Layout try using android:layout_gravity attribute with each child to position them correctly without overlapping and use elevation if you want your views overlapping each other.

For more Info visit https://developer.android.com/reference/android/widget/FrameLayout.html

Mohammed Junaid
  • 1,362
  • 14
  • 18