Skip to content Skip to sidebar Skip to footer

Aligning Left/center/right Linearlayout - Android

This should be pretty easy, but since I am new in android development I am asking. I basicaly want to create a static menu bar at the bottom of my avtivity with simple buttons (pre

Solution 1:

You have to use a RelativeLayout.

Try something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" >

<Button
    android:id="@+id/previousButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="New Button" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:layout_alignParentBottom="true"
    android:id="@+id/nextButton"
    android:layout_toRightOf="@id/previousButton"
    />

</RelativeLayout>

EDIT:

try this then:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/bottomBar">
    />

<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerVertical="true"android:orientation="horizontal" ><Buttonandroid:id="@+id/previousButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/nextButton"android:layout_toRightOf="@id/previousButton"/></LinearLayout></RelativeLayout>

EDIT 2

Ok, so if I understood correctly: You want a Layout composed by a Scroll View and a View in the bottom of the screen with two buttons, one aligned to the left of the screen and the other aligned to the right. I think the following layout suggestion can work out for you. Nevertheless, without seeing your current layout I cannot clearly understand how you want your layout to be.

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/bottomBar"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">
    />

<ScrollViewandroid:id="@+id/scrollView1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="420dp"android:orientation="vertical" ></LinearLayout></ScrollView><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/previousButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:text="New Button"android:layout_alignParentLeft="true"/><Buttonandroid:id="@+id/nextButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:layout_alignParentRight="true"/></RelativeLayout></LinearLayout>

EDIT 3

In order to have a 3rd button just add the XML below after the last Button view:

<Button
        android:id="@+id/center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Center" 
        android:layout_toRightOf="@id/previousButton" 
        android:layout_toLeftOf="@id/nextButton"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"/>

I've added the marginLeft and marginRightso that the buttons have a distance between them, as I think this is what you want.

Solution 2:

you can't use the xml attribute alignParentBottom in a LinearLayout. It works for a RelativeLayout.

Secondly, the attribute gravity only position the content of the tag ( it will put the buttons in the center of the page verticalty)

Try this

<RelativeLayoutandroid:id="@+id/bottomBar"android:layout_height="wrap_content"android:layout_width="fill_parent"android:layout_alignParentBottom="true"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/previousButton"android:layout_alignBottom="@+id/bottomBar"android:layout_alignParentLeft="true"


        /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/nextButton"android:layout_alignBottom="@+id/bottomBar"android:layout_alignParentRight="true"

        /></RelativeLayout>

Solution 3:

If you only need to make each button take the same width and occupy the total width of the parent layout:

In buttons, change to android:layout_width="0dp" and add android:layout_weight="1"

This will distribute all the buttons with homogeneous width.

Post a Comment for "Aligning Left/center/right Linearlayout - Android"