Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align elements horizontally within a LinearLayout?

enter image description here

I am not able to control the height of the LinearLayout. These won't align properly and won't fill up the width. I want the divider to be in the center and both the buttons on either side. Here is my code:

<LinearLayout
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

    <Button
        android:id="@+id/signUpButton"
        style="@style/AuthButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_button_label" />

    <View
        android:id="@+id/buttonDivider"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider" />

    <Button
        android:id="@+id/cancelButton"
        style="@style/AuthButton"
        android:text="@string/cancel_button_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient 
    android:startColor="@color/white"
    android:centerColor="@color/blue"
    android:endColor="@color/white" />
</shape>

Update 1:

View still sticks out after @Onik's suggestion

enter image description here

Update 2:

I removed the View element and added this code in LinearLayout instead and it worked!

android:divider="@drawable/divider"
android:showDividers="middle"

Note: android:divider property is available only in Android 3.0 (API level 11) or higher.

Actually this link helped me and shows the proper way to put a divider: How to add (vertical) divider to a horizontal LinearLayout?

like image 464
Prince Avatar asked May 25 '14 02:05

Prince


People also ask

How do you do a horizontal LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How do you align text in LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.

What is the default orientation of a LinearLayout?

By default, orientation is horizontal. Horizontal orientation will only have one single row of elements on the screen and in the case of vertical orientation, it will only have one child per row. One more important attribute in a linear layout is the android:layout_weight attribute which assigns weights to elements.


1 Answers

You need to use layout_weight attribute for Buttons. To prevent divider to fill the height of the layout (like entire height of the screen in the picture) use layout_height="match_parent" instead of layout_height="wrap_content". This will fill the screen's (or parent's) width and set the button's width to be equal, with the divider between them of same height:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

<Button
    android:id="@+id/signUpButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sign_up_button_label" />

<View
    android:id="@+id/buttonDivider"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@drawable/divider" />

<Button
    android:id="@+id/cancelButton"
    android:text="cancel_button_label"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

like image 173
Onik Avatar answered Sep 22 '22 10:09

Onik