3

something weird is happening to me. I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray_dark" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:drawableTop="@drawable/some_drawable"
    android:text="Some text" />
</RelativeLayout>

And I would expect that TextView is visible above button. But for some reason the Button goes to front automaticaly, covering up the textview. When I change Button to common View, the order is right.

Is there something I am missing about Buttons in Android? Thanks

Edit:

Preview:

enter image description here

bakua
  • 13,704
  • 7
  • 43
  • 62

1 Answers1

3

I just tried the same thing and the weird thing is, it also happend to me. I fixed it like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:text="lalala"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#FFFF00"
        android:text="lalala"/>

</RelativeLayout>
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • I've tried that as well and still doesn't work. Looks like Button takes prioprity because it is clickable or something. – bakua May 22 '15 at 13:39
  • @bakua that should not happen. you can assign a click on any view, but this doesn't mean it should have a higher z order. – Bojan Kseneman May 22 '15 at 13:40
  • Exactly. Maybe it is a bug in IDE? When I build the app it behaves the same on device. I am using Android Studio 1.2.1.1 – bakua May 22 '15 at 13:44
  • @bakua It's quite weird, it happens to me too. I also posted a fix. Why this happens remains a mistery. – Bojan Kseneman May 22 '15 at 13:51
  • :D HA. Pretty funny workaround. Now I am just curious whether this is feature or a bug. – bakua May 22 '15 at 14:05
  • Yeah, it's a hack to trick the Android a bit. However, it does the job :D – Bojan Kseneman May 22 '15 at 14:34
  • cool, this hack really work, i alredy try everything, bringToFront, remove and add in another index, relative/frame layout and nothing work like this. – rcorbellini Oct 27 '16 at 13:00