Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Align 3 buttons in a line, android?

I have linear vertical layout in parent. Then a horizontal linear layout in the bottom including 3 buttons

I want 1st button at the leftmost side of the activity in bottom 2nd button in the center and 3rd button in the rightmost side

Here is the xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

   <ImageView
    android:id="@+id/viewImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:contentDescription="@null"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_gravity="fill_vertical"
        android:text="Button" />
</LinearLayout>

like image 308
WISHY Avatar asked Dec 14 '13 08:12

WISHY


People also ask

How to position Linear Layout in android Studio?

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 to divide Linear Layout in android Studio?

If you want to divide layout into two parts of same size you have to set layout_width attributes to 0dp of both layouts and layout_weight to 1 inside the main layout. Then set weightSum to 2 of the parent layout. Same concept can be applied to divide layout to any ratio.


1 Answers

For those type of alignment(one button in extreme left of parent, another in center and one more in extreme right of parent layout) RelativeLayout will be more handy than the LinearLayout.Because, if you use RelativeLayout as your parent layout for your buttons you can make use of android:layout_alignParentLeft="true" for one button you want to align in extreme left,android:layout_centerInParent="true" to align in center and android:layout_alignParentRight="true" to align a button in right side corner.

Try this..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

   <ImageView
    android:id="@+id/viewImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:contentDescription="@null"
    android:src="@drawable/ic_launcher" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button" />
</RelativeLayout>

</LinearLayout>
like image 172
Hariharan Avatar answered Nov 01 '22 16:11

Hariharan