1.How do I center the text horizontally and vertically in a TextView
in Android, so that it appears exactly in the middle of the TextView
?
A1. I'm assuming you're using XML layout.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>
and
in java:
<TextView
.setGravity(Gravity.CENTER);
A2. In xml
android:gravity="center"
or
android:layout_centerInParent="true"
A3. In java
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
A4. For Linear Layout:
In XML use something like this
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text goes here"
/>
To do this at run time use something like this in your activity
TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
For Relative Layout:
in XML use some thing like this
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:text="Your Text goes here"
/>
To do this at run time use something like this in your activity
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams =
RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);
A5. in Xml
is better than,
which is better than,
|
Comments
Post a Comment