androidandroid-linearlayoutcenterlayout-gravity

Adjust TextView to Vertically center in LinearLayout (horizental)


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>

Solution

  • 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" />