Aligning LinearLayouts in Relative Layout

SiflouSiflou USMember ✭✭

Hey Guys,

I want to align a linear layout containing buttons at the bottom of the screen. That part is easy, i just put my layout inside a relative layout and tell the relative layout height to match parent. So I now have my linear layout aligned at the bottom. Now what I want to do is to align my content linear layout in the center not of the relative layout but, in the center of the space between the bot aligned layout and the remaining of the space in the relative layout. If you dont get it, I attached a drawing with the example. If some one can help me with this, it would be so appreciated. Thanks a lot !

Claude

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    Not entirely sure what you mean with either your text or your drawing. However this is what I think you want:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:minWidth="200dp"
            android:minHeight="200dp"
            android:background="#DC143C"
            android:layout_centerInParent="true">
            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="I'm in your center... centering... However I (TextView) can't center vertically because I my parrent has height set to wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/buttonWrapper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Zomg" />
            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="BBQ" />
        </LinearLayout>
    </RelativeLayout>
    

    This gives the following result, where the bottoms are aligned at the bottom, and the content view is vertically centered:

  • SiflouSiflou USMember ✭✭

    Yay, I got it working, I dont even need a relative layout anymore, I just used the weight parameter set to 1 in my content layout, and when I do match parent in the middle content layout, it doesnt go all the way down in my parent layout, so I can center my element like i just said and my footer buttons stays at the bottom. Before, when I did match match parent in my linear layout, it like pushed my footer layout all the way down so I couldnt even see it, with the weight set to 1 it did work ! Thanks a lot guys, you rock ! Sorry again if my drawing and explanation wasnt the most clear of all, I thought it was, but for me its sure thats its easy to explain, but anyway you guys got it because now it works :) Thanks a lot again !

  • TagoTago NGMember

    thanks so much your code saved me a lot of trouble can't thank you enough. will get to you if i have any issue.

  • gyanvermagyanverma INMember

    Thanks for the code it worked perfect :)

Sign In or Register to comment.