6

I want to make my ImageView on top of my Button, but the button will always overlay the ImageView , no matter which layout I choose or the way I arrange my layout... this is an example of a FrameLayout that I tried:

EDIT: just for simplifying my question : Why doesn't the FrameLayout work as it should? (you can copy this code to your Android Studio and see yourself)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:padding="0.1dp">

    <Button
        android:clickable="false"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="135"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:id="@+id/buttonText"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/triangle"
        android:id="@+id/triangle"
        android:paddingTop="5dp"
        android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>
james
  • 221
  • 1
  • 5
  • 14

4 Answers4

6

I experienced the same issue (of note, if the button is disabled it would not overlay all the other views), and found a fix. Simply wrap the button and image in separate sub-frame layouts.

<FrameLayout>...
    <FrameLayout>...
        <Button ... />
    </FrameLayout>
    <FrameLayout>...
        <ImageView ... />
    </FrameLayout>
</FrameLayout>

I don't know if this has any negative repercussions, aside from perhaps a bit of inefficiency, but it seems to work fine.

InfalibleCoinage
  • 588
  • 13
  • 21
  • 1
    Your solution worked for me; however, it's not necessary to wrap both views in FrameLayouts or put them both in a FrameLayout at all, just wrapping the one I want to be in the background did it for me. EDIT: clarification – Nikaoto Aug 12 '17 at 21:30
5

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:padding="0.1dp">

    <Button
        android:clickable="false"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="135"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:id="@+id/buttonText"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/triangle"
        android:id="@+id/triangle"
        android:paddingTop="5dp"
        android:layout_gravity="center_horizontal|bottom"
        android:elevation="3dp" />

</FrameLayout>
Khoa Vo
  • 1,005
  • 10
  • 7
0

You can call bringToFront() on the view you want to get in the front

Example:

yourView.bringToFront();
Aawaz Gyawali
  • 3,244
  • 5
  • 28
  • 48
-1

I solved the problem myself by changing the Button to TextView . The FrameLayout default behavior isn't working here because Android system always set Buttons to overlay other views (so the user will always be able to click the button).

james
  • 221
  • 1
  • 5
  • 14