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 5 - it is incorrect, red textview under the button!
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!
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With