I have a layout and need to place one of the views in the center. It's easy. The problem is that I also have some other views which need to be positioned to the top of the screen.
Problem: If there's enough space it looks ok, but when the screen is too small the top and center elements overlap.
Question: How to push the "centered" element down if the screen is to small?
The original layout is very complex, I prepared an example:
<?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" >
<TextView
android:id="@+id/theOneAtTheTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some long text which may overlap the lower view. Some long text which may overlap the lower view."
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/theOneInTheMiddlep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some long text which may overlap the upper view. Some long text which may overlap the upper view. "
android:layout_centerInParent="true"
/>
</RelativeLayout>
Here's a little simulation. The middle picture shows how it looks on a small screen, and the pictures on sides show the desired situation.
I really doubt you can do this directly in the layout file as a xml layout is not the place for such conditional logic. The only real situation where you could(I think) use the layout file would be if you knew the exact dimensions of the views involved plus the API level of the app would be 3.2
, creating a lot of layouts for different situations(which I really doubt).
If you center one of the views in a RelativeLayout
it will be centered no matter what, it doesn't have the notion of overlaping views at the xml layout file level. In code is another thing.
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