views:

5094

answers:

5

Hey,

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it does not work (even if you change vertical-center to center_vertical, etc).

Cheers,

Pete

+3  A: 

I do not believe Android supports full justification.

CommonsWare
Upon further analysis, you could give android:gravity="fill_horizontal" a shot. That is described as "Grow the horizontal size of the object if needed so it completely fills its container", but I do not know how they "grow" the text.
CommonsWare
android:gravity="fill_horizontal" didn't work either. It looks like android doesn't support justification after all, oh well :)
Peter
+4  A: 

You have to set

android:layout_height="wrap_content"

and

android:layout_centerInParent="true"
Lukas
+3  A: 

So I had this exact same problem. I was creating a basic 2-column form (labels on the left and text fields on the right). I wanted the labels on the left to be right justified so they would appear flush up against their text fields.

In the XML layout file I was able to get the TextViews elments themeslves to align to the right by adding the following attribute inside all of my TextViews doing this:

<TextView>
   ...
   android:layout_gravity="center_vertical|right"
   ...
</TextView>

However, if the text wrapped to multiple lines, the text would still be left justified inside the TextView. Adding the following attribute made the actual text right justified (ragged left) inside the TextView:

<TextView>
   ...
   android:gravity="right"
   ...
</TextView>

So gravity attribute specifies how to align the text inside the TextView layout_gravity specifies how to align/layout the TextView element itself.

James
A: 

this worked for me

<TextView>
   ...
   android:gravity="center_vertical|right"
   ...
</TextView>
Sam Quest
oops i guess mine is for Right Justification only
Sam Quest
+1  A: 

This worked for me:

    TextView tv = new TextView(context);
    tv.setWidth(40);
    tv.setGravity(Gravity.RIGHT); // attempt at justifying text 
    tv.setMaxLines(1);
    tv.setText("Hi");

    this.addView(tv);

The critical line is

tv.setWidth(40);

Without that the text doesn't justify!

Maybe Google should update their SDK docs with a some real code world examples! http://developer.android.com/reference/android/widget/TextView.html is a great overview, but they really need to append the document with some simple how to's/explanations/caveats. , it took me a good while to figure this out and some frustration to do something as simply as justify my text! GRrrrrr! Bad that the assumption might simply be "this can't be done".

Lemonsanver
this did it for me! thanks
binnyb