user2268507
user2268507

Reputation:

Center text in TextView and Button

I am trying to center the text in my TextView and Button. I currently have the following code:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"        
        android:orientation="vertical"
        android:background="#000000" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="@string/CouponFinder"
            android:textSize="30sp"
            android:textColor="#FF0000"
            android:typeface="monospace" />

        <ImageView
            android:id="@+id/couponImage"
            android:layout_width="match_parent"
            android:layout_weight="0.7"
            android:layout_height="0dp"
            android:src="@drawable/coupon" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:text="@string/continueButton"
            android:textSize="30sp" 
            android:gravity="center"/>

    </LinearLayout>

</RelativeLayout>

However, the text in the Button and TextView are to the right and hence some of the text is off the screen... I am also getting the following warning for my LinearLayout:

This LinearLayout layout or its RelativeLayout parent is possibly useless

Any help would be greatly appreciated!!

Upvotes: 0

Views: 24773

Answers (7)

Devon Kerai
Devon Kerai

Reputation: 1

What you've done is correct, but to see the changes, you just need to restart your IDE. I found the answer here.

Upvotes: 0

Manishika
Manishika

Reputation: 5564

The warning is because you are using only one LinearLayout as the Child View for RelativeLayout.So remove your RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"        
    android:orientation="vertical"
    android:background="#000000" >

     <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_weight="0.15"
    android:layout_height="0dp"
    android:gravity="center"
    android:text="@string/CouponFinder"
    android:textSize="30sp"
    android:textColor="#FF0000"
    android:typeface="monospace" />

    <ImageView
        android:id="@+id/couponImage"
        android:layout_width="match_parent"
        android:layout_weight="0.7"
        android:layout_height="0dp"
        android:text="@string/CouponFinder" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_weight="0.15"
        android:layout_height="0dp"
         android:text="@string/continueButton"
        android:textSize="30sp" 
        android:gravity="center"/>

</LinearLayout>

Try the above data.I got this by the above data

enter image description here

Upvotes: 0

Custadian
Custadian

Reputation: 855

Try this,

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

tools:context=".MainActivity" >

      <ImageView
            android:id="@+id/couponImage"
            android:layout_width="wrap_content"
            android:layout_weight="0.7"
            android:layout_height="wrap_content"
            android:src="@drawable/coupon" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:gravity="center"
        android:text="continueButton"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal"
        android:text="CouponFinder"
        android:textColor="#FF0000"
        android:textSize="30sp"
        android:typeface="monospace" />

</RelativeLayout>

Upvotes: 1

Indra
Indra

Reputation: 548

Try this

[<TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.15"
                android:gravity="center"
                android:hint="asdasasdasdd"
                android:text="adasdasdasd"
                android:textAlignment="gravity"
                android:textColor="#FFffff"
                android:textSize="30sp"
                android:typeface="monospace" />]

enter image description here

Upvotes: 4

Hariharan
Hariharan

Reputation: 24853

Try this..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
        android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
        android:background="#000000" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="CouponFinder"
            android:textSize="30sp"
            android:textColor="#FF0000"
            android:typeface="monospace" />

        <ImageView
            android:id="@+id/couponImage"
            android:layout_width="match_parent"
            android:layout_weight="0.7"
            android:layout_height="0dp"
            android:src="@drawable/icon" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:text="continueButton"
            android:textSize="30sp" 
            android:gravity="center"/>

    </LinearLayout>

Upvotes: 0

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

Try this way:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:gravity="center"
    android:text="CouponFinder"
    android:textColor="#FF0000"
    android:textSize="30sp"
    android:typeface="monospace" />

<ImageView
    android:id="@+id/couponImage"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    android:src="@drawable/icon" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:gravity="center"
    android:text="continueButton"
    android:textSize="30sp" />

  </LinearLayout>

Upvotes: 0

Gil Moshayof
Gil Moshayof

Reputation: 16761

  1. You should make the RelativeLayout a LinearLayout, and get rid of the internal LinearLayout, unless you plan to make modifications and add more things inside the RelativeLayout and outside of the LinearLayout.

  2. Try setting the layout-gravity on the items instead of gravity.

Hope this helps :)

Upvotes: 0

Related Questions