Pranav Mahajan
Pranav Mahajan

Reputation: 2108

Centering a TextView while keeping it to the left of a button?

Requirement

I've got a button and a textview, and I want the textview to be centered in the parent view, while the button is on the right side in the parent view.

Approach

So I took a RelativeLayout, make the textview layout_centerHorizontal="true", and the button layout_alignParentRight="true".

Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/title_header_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="TextView" />
       <!--  Doesnt work -->
       <!--  android:layout_toLeftOf="@+id/right_button"  -->

    <Button
        android:id="@+id/right_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"        
        android:text="Button" />

</RelativeLayout>

Problem

So when the text is too long, it gets written on top of the button.

I tried doing layout_toLeftOf="@+id/right_button" on the textview, but now TextView is no longer centered.

Upvotes: 0

Views: 78

Answers (6)

Pooja
Pooja

Reputation: 2467

Try this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">


    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"/>

    <TextView
        android:id="@+id/title_header_TV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />


</TableRow>
</RelativeLayout>

Upvotes: 1

rogerwar
rogerwar

Reputation: 338

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   <!--  Doesnt work -->
   <!--  android:layout_toLeftOf="@+id/right_button"  -->

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="17dp"
    android:gravity="center"
    android:text="Add your demo text here for small and large"
    android:layout_toLeftOf="@+id/button1"/>
 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginLeft="19dp"
    android:text="Button" 
    android:layout_alignParentRight="true"/>
</RelativeLayout>

Upvotes: 0

Krish
Krish

Reputation: 1

Please change to `

<Button
    android:id="@+id/right_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Button" />

<TextView
    android:id="@+id/title_header_TV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/right_button"
    android:layout_alignTop="@id/right_button"
    android:layout_centerHorizontal="true"
    android:layout_toLeftOf="@id/right_button"
    android:gravity="center"
    android:text="TextView" />

`

Upvotes: 0

Amrut Bidri
Amrut Bidri

Reputation: 6360

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/title_header_TV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_toLeftOf="@+id/right_button"
    android:gravity="center"
    android:text="TextView " />

<Button
    android:id="@+id/right_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Button" />

</RelativeLayout>

check this out. a very simple solution for you

Upvotes: 0

mujeeb.omr
mujeeb.omr

Reputation: 499

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/title_header_TV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/right_button"
    android:gravity="center"
    android:text="This is a test message, This is a test message This is a test message " />
<!-- Doesnt work -->
<!-- android:layout_toLeftOf="@+id/right_button" -->

<Button
    android:id="@+id/right_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Button" />

</RelativeLayout>

Upvotes: 0

rogerwar
rogerwar

Reputation: 338

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   <!--  Doesnt work -->
   <!--  android:layout_toLeftOf="@+id/right_button"  -->
   <Button
    android:id="@+id/right_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"        
    android:text="Button" />
  <TextView
    android:id="@+id/title_header_TV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/right_button"
    android:layout_alignBottom="@+id/right_button"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="80dp"
    android:text="TextView" />

try this

Upvotes: 0

Related Questions