How can I align views justify in LinearLayout?

I want to align two buttons left and right and I'm using layout_weight property width .25 for both of buttons. But when I did it, buttons width are became %50.

This is what I want: image

This is what I have: image

And this is my XML layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25"
        android:textColor="#212121"
        android:background="@drawable/button_default"
        android:layout_margin="5dp"
        android:src="@drawable/like" />

    <Button
        android:text="NEXT"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:textColor="#FFFFFF"
        android:background="@drawable/button_primary"
        android:layout_margin="5dp"/>
</LinearLayout>

How can I do this?

Upvotes: 0

Views: 497

Answers (2)

Francesc
Francesc

Reputation: 29260

Do it like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:background="@drawable/button_default"
        android:src="@drawable/like"
        android:textColor="#212121" />

    <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/button_primary"
        android:text="NEXT"
        android:textColor="#FFFFFF" />
</LinearLayout>

or like this:

<?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="wrap_content">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="5dp"
        android:background="@drawable/button_default"
        android:src="@drawable/like"
        android:textColor="#212121" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:background="@drawable/button_primary"
        android:text="NEXT"
        android:textColor="#FFFFFF" />
</RelativeLayout>

Upvotes: 0

Jay Rathod
Jay Rathod

Reputation: 11245

Try this code.

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:weightSum="2">

        <ImageButton
            android:id="@+id/like"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@mipmap/ic_launcher"
            android:src="@mipmap/ic_launcher"
            android:textColor="#212121" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@mipmap/ic_launcher"
            android:text="NEXT"
            android:textColor="#FFFFFF" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Related Questions