2

I have four TextView in LinearLayout with orientation="horizontal". Below in image, I want my fourth TextView to vertically center with respect to other views, In simple words, I want to move my fourth TextView a little bit up so it looks like in the center. I tried to add layout:gravity and margins but its not working.

enter image description here

Here is the code:

  <LinearLayout
        android:id="@+id/tv_amenities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_main_feature_LL"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_food_and_restaurant"
            android:padding="3dp"
            android:text="Resturant"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_tv_black_24dp"
            android:padding="3dp"
            android:text="LCD Tv"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white"

            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_local_parking_black_24dp"
            android:padding="3dp"
            android:text="Parking"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white"

            />

        <TextView
            android:id="@+id/tv_plus_amenities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/round_background"
            android:text="+15"

            android:textColor="@android:color/white" />


    </LinearLayout>
Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32

4 Answers4

1

Add this prop to your @+id/tv_amenities LinearLayout

android:gravity="center_vertical"

etomun
  • 134
  • 6
1

Use android:layout_gravity="center_vertical" attribute in your TextView's

lincollincol
  • 762
  • 2
  • 12
  • 23
1

You could easily do that by adding android:layout_gravity="center_vertical" and removing your margin bottom.

        <TextView
        android:id="@+id/tv_plus_amenities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/round_background"
        android:text="+15"
        android:textColor="@android:color/white" />
Kuruchy
  • 1,330
  • 1
  • 14
  • 26
1

I checked your code inside a constraint layout and it's fine. All I changed is in your last TextView:

android:layout_gravity="center"

Or

android:layout_gravity="center_vertical"

The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=center only centers horizontally.

This link can help you to fix your issue in LinearLayout or RelativeLayout.

You might find this link useful:What is the difference between gravity and layout_gravity in Android?

TrackRunner
  • 484
  • 5
  • 6
  • I am using this under RelativeLayout but you tried it in ConstrainLayout. Moreover, already tried `android:layout_gravity="center"` but it's not working fine. – Shoaib Kakal May 01 '20 at 14:32
  • It shouldn't be a big difference (in code) between Relative and Constraint Layout. I created a little code snippet on [GitHub.](https://gist.github.com/LaszloLajosT/43b816ed420da4355264a1ad1835d86b) I hope it helps. – TrackRunner May 01 '20 at 19:35