Adjust TextView to Vertically center in LinearLayout (horizental)

109 views Asked by At

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>
4

There are 4 answers

0
Kuruchy On BEST ANSWER

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" />
0
etomun On

Add this prop to your @+id/tv_amenities LinearLayout

android:gravity="center_vertical"

0
lincollincol On

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

2
TrackRunner On

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?