1

I want to align button on the right side of the view in linear layout. This is causing both the button on left size and i tried relative layout in that i cause one button left and other on right. I want both the button side by side with gap of 10 in between.

Please help.

                <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal">


                <ImageButton
                    android:background="@null"
                    android:id="@+id/reply_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:src="@drawable/reply"
                    android:layout_gravity="left"/>

                <Button
                    android:id="@+id/readmore_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@null"
                    android:text="Read more.."
                    android:textAllCaps="false"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />
            </LinearLayout>

Here is the output for the reply

ios developer
  • 3,363
  • 3
  • 51
  • 111
  • 1
    Could you put the entire code of the LinearLayout? – Nekak Kinich May 14 '19 at 20:51
  • In a `LinearLayout`, `layout_gravity` specified by the children must always be in the orthogonal axis to the orientation. This is, for an horizontal `LinearLayout`, it can be `top`, `center` (or `center_vertical`) or `bottom`. See https://stackoverflow.com/questions/4783896/layout-gravity-in-linearlayout – Xavier Rubio Jansana May 14 '19 at 21:26

1 Answers1

1

You can set android:layout_weight="2" to your container and for every child set android:layout_weight="1" and android:layout_width="0dp" 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="match_parent"
  android:orientation="horizontal"
  android:layout_weight="2">

<ImageButton
    android:background="@null"
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_weight="1"
    android:layout_gravity="left"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="@null"
    android:text="Read more.."
    android:textAllCaps="false"
    android:layout_weight="1"
    android:textColor="@android:color/white"
    android:textSize="14sp" />
</LinearLayout>

But if you already developed some ios apps before as your name is implying, you may find it more easy to work with ConstarintLayout - one layout to fit all screen sizes (its very similar to the drag and drop editor in ios I just cant remember its name)

Here is an example using ConstraintLayout:

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button2"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

Edit according to what you asked:

<?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="match_parent"
  android:orientation="horizontal"
  android:layout_weight="3">

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="left"
    android:layout_gravity="start"/>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:text="Read more.." 
    android:layout_weight="1"
    android:textSize="14sp" />

<Button
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="right"
    android:layout_weight="1"/>
</LinearLayout>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Thank you for the reply. I used 1st option but it dont allign both the button. It only allow to allign only 1 button.can you check it once again please – ios developer May 14 '19 at 21:08
  • 1
    "It only allow to align only 1 button" - this is not so clear, help me better to understand you - could you maybe add a screenshot of how it looks now and how you want it to look? – Tamir Abutbul May 14 '19 at 21:10
  • I had edited my question and added output image in question – ios developer May 14 '19 at 21:21
  • yes both are buttons. I wanted it to allign 3 button side by side towards left – ios developer May 14 '19 at 21:23
  • 1
    So you want 3 buttons in 1 row? and every button will take the same space? – Tamir Abutbul May 14 '19 at 21:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193359/discussion-between-ios-developer-and-tamir-abutbul). – ios developer May 14 '19 at 21:25
  • `layout_weight` doesn't go on the parent `LinearLayout`. You might be thinking of `weightSum`, but that's not necessary in this case, as evidenced by the fact that it's working without it. – Mike M. May 15 '19 at 00:19
  • 1
    @MikeM. thank you for adding this information, normally I work with constraintLayout so I had no idea. And ye, I meant `weightSum`, my mistake – Tamir Abutbul May 15 '19 at 07:03