Home > Software design >  How to allign a textView according to its center in Android
How to allign a textView according to its center in Android

Time:11-28

Very simply put, I want to allign a textView according to its center instead of its border, so lets say, when using android:layout_marginRight="50dp", instead of the right border being 50dp away, the center of the textView being 50dp away from the parent's right border. Here is my code for reference:

<RelativeLayout
            android:id="@ id/breakfastStats"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@ id/breakfastAdd"
            android:layout_marginTop="1dp"
            android:background="@color/lightblack">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Carbohydrate"
                android:textColor="@color/white"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:layout_marginTop="5dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Fat"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                />

enter image description here

CodePudding user response:

Fixed by using a GridLayout instead of a RelativeLayout.

CodePudding user response:

You can use margin and padding value together for the TextView.

marginRight = "25dp"
paddingRight = "25dp"
gravity = "center"
  • Related