Beh S
Beh S

Reputation: 25

How can I position the end edge of a TextView to the center of the layout?

I have a simple TextView inside of a RelativeLayout.

I want to position end edge of TextView to center of the RelativeLayout as in image below

Desired result

I found something like android:layout_toStartOf=...

Example:

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">  
    <TextView 
          android:text="TextView" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toStartOf=...
        />
</RelativeLayout>

But this only aligns the end edge to start of something. (Can't be used for align end edge to CENTER of layout).

Upvotes: 1

Views: 489

Answers (3)

atabouraya
atabouraya

Reputation: 3332

You can do that using PercentRelativeLayout

<android.support.percent.PercentRelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@android:color/white"
    android:text="TextView"
    android:textColor="@android:color/black"
    app:layout_marginRightPercent="50%" />

Upvotes: 1

Samuel Rabinowitz
Samuel Rabinowitz

Reputation: 135

NOTE: There is already a similar question and answer here.

You need to have a widget in the RelativeLayout that you can use as a reference to position the TextView. An empty Space widget (0dp x 0dp) centered horizontally works well for this.

<RelativeLayout>

    <!-- Empty space widget (0x0 dp) centered horizontally -->
    <Space
        android:id="@+id/spacer" 
        android:layout_width="0dp" 
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" 
        android:visibility="invisible"/>

    <!-- TextView to left of the spacer-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/spacer"
        android:text="awesome text!"/>

</RelativeLayout>

Upvotes: 1

Floern
Floern

Reputation: 33904

You could create a dummy View centered within your RelativeLayout and then align the TextView left to it:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/center_anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/center_anchor"
        android:text="hello world" />

</RelativeLayout>

You may also have a look at the PercentRelativeLayout which supports percentage based dimensions and margins. That means you can specify a right margin of 50% for example.

Upvotes: 1

Related Questions