I can use android:gravity="bottom|center_horizontal"
in xml on a textview to get my desired results, but I need to do this programmatically. My textview is inside a tablerow
if that matters in a relativelayout
.
I have tried:
LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
labelTV.setLayoutParams(layoutParams);
But if I understand correctly, that would apply it to the tablerow
, not the textview?
Use this code
This will center the text in a text view:
You should use
textView.setGravity(Gravity.CENTER_HORIZONTAL);
.Remember that using
won't work. This will set the gravity for the widget and not for it's text.
We can set layout gravity on any view like below way-
This is equilent to below xml code
This will set gravity of your textview.
Also, are you talking about gravity or about layout_gravity? The latter won't work in a RelativeLayout.