Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View in the center of layout if there's enough space

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.

like image 611
Michał Klimczak Avatar asked Sep 10 '12 12:09

Michał Klimczak


1 Answers

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.

like image 95
user Avatar answered Nov 15 '22 23:11

user