MataMix
MataMix

Reputation: 3296

Android how to center vertically text when I set a background to the textView

I need to center the TextView in the center of the purple background enter image description here

This is the XML:

<TextView
    android:id="@+id/widget33"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="somePurplePNG"
    android:text="TextView"
    android:gravity="left"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" />

Any ideas? thanks PS: I can't create a layout that wraps the textview and defines a background.

Upvotes: 1

Views: 6783

Answers (5)

Hadi Samadbin
Hadi Samadbin

Reputation: 237

you can align text in a text view programmatically in your java class

TextView YourTextView = (TextView)findViewById(R.id.textViewid);
//This will align text in both horizontal and Vertical of its container
YourTextView.setGravity(Gravity.CENTER);

//Place object in the vertical center of its container
YourTextView.setGravity(Gravity.CENTER_HORIZONTAL);

//Place object in the vertical center of its container
YourTextView.setGravity(Gravity.CENTER_VERTICAL);

Upvotes: 1

Shah Shah
Shah Shah

Reputation: 11

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

<TextView
    android:id="@+id/widget33"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="TextView" />

</LinearLayout>    

Upvotes: 1

MAC
MAC

Reputation: 15847

change Gravity of your text view like this

android:gravity="center_horizontal"

or

android:gravity="center"

Upvotes: 6

Khan
Khan

Reputation: 7605

Use TextView as this way

 <TextView
    android:id="@+id/widget33"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    aandroid:background="@drawable/somePurplePNG"
    android:text="TextView"
    android:gravity="center"/>

Upvotes: 1

MataMix
MataMix

Reputation: 3296

Solved using

android:gravity="center_vertical"

Upvotes: 1

Related Questions