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
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
You have to set
android:layout_height="wrap_content"
and
android:layout_centerInParent="true"
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.
this worked for me
<TextView>
...
android:gravity="center_vertical|right"
...
</TextView>
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".