2

Here is the full XML android layout file code:

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

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="New Button"
    android:textAllCaps="false"
    android:textSize="15pt"
    android:id="@+id/element_button"
    android:layout_gravity="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/element_number"
    android:layout_gravity="top|left" />
</FrameLayout>

It produces this in the android Studio preview window as well as on the device:

enter image description here

Am I losing it? What gives? As you can see, the "New Text" text view is BEHIND the button. When in every code sample I found on stackoverflow, and in simple programming logic, the button gets drawn FIRST, THEN the TextView, therefore, textview is on top of button. But its not. This happens with the RelativeLayout as well.

Seth
  • 1,769
  • 4
  • 26
  • 39
  • 1
    I had nearly the same problem... Try to set 'elevation' to TextView and Button. Use the same value for both of them.. Probably the style/theme that is applied to button sets some default value of elevation... – convexHull Dec 05 '15 at 15:50

2 Answers2

3

You're not crazy it's the theme in your Styles that is doing it:

Will make the crazy ordering:

  <style name="AppTheme" parent="android:Theme.AppCompat">

Will restore your sanity:

  <style name="AppTheme" parent="android:Theme.Holo">

WHY it's doing it ... maybe a bug, maybe a change, sorry I'm not sure

You can play around with the themes here to prove it:

enter image description here

vs

enter image description here

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 2
    AppCompat & Material design based themes (api 20+) have elevation on buttons by default. This causes the button to show over the text. Halo is dated is probably not the best theme for new apps. – cyroxis Dec 05 '15 at 16:05
  • Thank you @Blundell. This was the issue. Why did they even add elevation to the layouts? It worked perfectly fine before where you would draw the first element. Now how the heck do I make this compatible for pre 21 android devices? I never had any luck with resources and layouts and older versions of android, so I always just just set things to older themes and such. – Seth Dec 05 '15 at 17:05
  • For compatibility I added a directory layout-v21 for the layouts which require elevation to be set. I don't like this elevation thing lol. Thank you guys for the help, was driving me crazy. – Seth Dec 05 '15 at 17:15
  • @cyroxis: how can we learn other widgets that have this elevation problem? Elevation clearly works, but since AppCompat is intended to provided backward compatibility, why would it's themes require a much more current API, especially for common design needs? This doesn't make sense to me. – John Ward Jan 11 '17 at 00:34
0

Try use elevation in your xml layout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/element_number"
    android:layout_gravity="top|left"
    android:elevation="2dp" />

or set by code

ViewCompat.setElevation(this, 2);
c-romeo
  • 388
  • 4
  • 13