android – Can I set property of Textview like justify?-ThrowExceptions

Exception or error:

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

How to solve:

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.

###

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

###

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> 

###

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

###

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"

###

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.

###

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.

###

Example : For left justification add this to your TextView

android:layout_gravity="center_vertical|left"

Leave a Reply

Your email address will not be published. Required fields are marked *