Reputation: 3296
I need to center the TextView in the center of the purple background
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
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
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
Reputation: 15847
change Gravity of your text view like this
android:gravity="center_horizontal"
or
android:gravity="center"
Upvotes: 6
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