Can I set property of Textview like justify? [closed]
Asked Answered
M

8

21

I want to set TextView property like right align, left align, of justify

Moraine answered 12/5, 2011 at 10:28 Comment(1)
possible duplicate of Android TextView Justify TextAnora
Y
17

No, you can't set the property like gravity. But still you can set the justification to your text by taking webview instead of text view.

You may refer to this.

Yasmin answered 1/9, 2012 at 8:45 Comment(1)
Link is dead (2020)Stablish
G
11

What I understand from your question is, you want to know, how to set the alignment of text inside a TextView. You can align text in TextView to left, right, or center.

To do it from your layout (XML) file, use the following attribute in your layout file;

android:gravity=alignment

where,

alignment can be

  • left to align text to left
  • right to align text to right
  • center to center the text

To do the same from code, use the following;

TextView textRegion = (TextView) findViewById(R.id.textRegion);
textRegion.setGravity(gravity);

where,

gravity =

Gravity.LEFT to align to left
Gravity.RIGHT to align to right
Gravity.CENTER to center the text

Note that, justify is not available yet for text alignment in TextView.

Update as of Android 8 (Oreo, API Level 26):
Justification is now possible but look quite jagged at right end.
See setJustificationMode

Gout answered 12/5, 2011 at 10:55 Comment(0)
D
4

look at the following code..

<TextView style="@style/TitleBarText1" android:text="@string/app_name" />

Style is defined below.. here most of the field may not necessary but helpfull .. see the gravity attribute and you can align the textview with this attribute...
enter image description here

 <style name="TitleBarText1">
    <item name="android:id">@id/title_text</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">@dimen/text_size_medium</item>
    <item name="android:paddingLeft">12dip</item>
    <item name="android:paddingRight">12dip</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/title_text</item>
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">end</item>
  </style> 
Djebel answered 12/5, 2011 at 10:38 Comment(0)
J
3

Yes.

It's called gravity.

On XML:

android:gravity="left" or "right"

Justify is not available.

Check: http://developer.android.com/reference/android/view/Gravity.html

Jodyjoe answered 12/5, 2011 at 10:35 Comment(0)
R
2

To align the text

<TextView
        android:id="@+id/tv_intro"
        style="@style/light_brown_bold_important"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_welcome"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="@string/intro"
        android:gravity="center" />

The important part is the

android:gravity="center"
Relegate answered 13/1, 2013 at 9:55 Comment(0)
P
1

Use TextView.setGravity. For example, use Gravity.RIGHT for right-aligned text and Gravity.CENTER_HORIZONTAL to center it. Justified text is not currently supported.

If you need more sophisticated layout control of text (for display only), use a WebView.

Powerful answered 12/5, 2011 at 10:36 Comment(0)
C
0

Use RelativeLayout as the parent layout for textView and use the xml proeprties of TextView like:

android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"

other prpertioes are also avialble to set the alignment and postion.

This link may help you too.

Cyan answered 12/5, 2011 at 10:39 Comment(0)
I
0

Example : For left justification add this to your TextView

android:layout_gravity="center_vertical|left"
Incretion answered 12/5, 2011 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.