0

I want to design a layout whose expected output should be like this:

enter image description here

but I get this

enter image description here

this is my sample code. any idea how can i fixed it

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello"
                android:gravity="left"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="How"
                android:gravity="end"/>
        </LinearLayout>

ADM
  • 20,406
  • 11
  • 52
  • 83
Ahmed
  • 51
  • 1
  • 7
  • 1
    Its `layout_gravity` not `gravity` . Use `ConstrainsLayout` or `RelativeLayout` instead. Even a `FrameLayout` will also work. Also [Check this](https://stackoverflow.com/questions/33076051/layout-gravity-not-working-in-horizontal-linear-layout). – ADM Aug 16 '21 at 07:08

4 Answers4

0

Try the below code with two different Android Layouts

With RelativeLayout

    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
     
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentStart="true"
        android:text="hello"
       />

    <Button
        android:layout_width="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_height="wrap_content"
        android:text="How"
        android:gravity="center"/>
</RelativeLayout>

With ConstraintLayout

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:gravity="center"
        android:text="hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:gravity="center"
        android:text="How"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sniffer
  • 1,495
  • 2
  • 14
  • 30
0

Change your parent layout to Relative layout

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:layout_alignParentStart="true"
        android:layout_gravity="center"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>
Patrick
  • 107
  • 7
0

For the correct implementation of ConstraintLayout, you may follow below approach:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        app:layout_constraintEnd_toStartOf="@id/how"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/how"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/hello"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
0

if you want to insist on using the LinearLayout, just a place a view above of the how button like this :

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="hello"
            />

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="How"
            android:layout_gravity="center"/>
Lobe Loic
  • 21
  • 5