Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textview on(above) Button, doesn't work in Android 5(API 21)

It looks like a bug in 5 Android(API 21). I need a textview on button, textview should placed above the button. It works correct on Android 4.1(API 16) and incorrect on 5 Android(API 21). There are Screenshots and code:

Android 4.1 - It is correct, red textview above the button

Android 4.1

Android 5 - it is incorrect, red textview under the button!

Android 5

Code:

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



<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="#00FF00">

    <Button
        android:id="@+id/bVio"
        android:layout_width="wrap_content"
        android:layout_height="50dip"
        android:text="VIO"></Button>

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00"
        android:textSize="12dip"
        android:textColor="#FFFFFF"
        android:background="@drawable/rounded_textbox"/>

    </FrameLayout>

</RelativeLayout>

rounded_textbox - it is just shape... if remove background, all looks same, textview under button in 5 android.

Please, advice!

like image 742
Evgeny Borzenkov Avatar asked Nov 25 '14 18:11

Evgeny Borzenkov


People also ask

Can TextView be clickable in android?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.

How do I add TextView to my toolbar?

If you want to add a view to your Toolbar then you have to add it inside Toolbar as a child, and not a child of the include. If you only want to show this TextView on one Activity then set TextView visibility to GONE by default and change it to VISIBLE inside your Activity where you want to show it.

How do you set up TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

How do I move one view to the top of another android?

The FrameLayout is the simplest ViewGroup and stacks the Views in the order they're defined in layout XML (or added programmatically); the first will be lower, and the last will be on top. Here is the actual layout XML with the two overlapping TextView boxes.


1 Answers

Yes. It is big chnage in Android L(API 21). There is new thing - Elevation, it is something like z-index in HTML. So to fix this bug you need use android:elevation="100dp" OR android:translationZ="100dip" for view that should be on top. So correct code is:

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



<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="#00FF00">

    <Button
        android:id="@+id/bVio"
        android:layout_width="wrap_content"
        android:layout_height="50dip"
        android:text="VIO"></Button>

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00"
        android:textSize="12dip"
        android:textColor="#FFFFFF"
        android:background="@drawable/rounded_textbox"
        android:elevation="100dp"/>

    </FrameLayout>

</RelativeLayout>
like image 117
Evgeny Borzenkov Avatar answered Sep 28 '22 18:09

Evgeny Borzenkov