I want to change the layout gravity for a TextView.
Via XML, you'd do that via android:layout_gravity="value"
.
I know that in order to change the gravity itself, via XML you'd do android:gravity="value"
, and in Java you'd do textview.setGravity(Gravity.VALUE);
But sadly, there's no textview.setLayoutGravity(VALUE);
, so I'm stuck.
Thanks!

- 3,185
- 1
- 22
- 42

- 37
- 1
- 8
4 Answers
You will need to use the correct sub class. For example if your TextView
is in a FrameLayout
, you would need to:
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) textView.getLayoutParams();
params.gravity = Gravity.CENTER;
textView.setLayoutParams(params);
Be aware that layout_gravity
or setLayoutGravity
sets your current elements gravity in its parent, gravity
or setGravity
sets gravity for your current elements content within the element. Refer the illustration below for more details.
Example 1:
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Some Text" />
</FrameLayout>
---------------------------------------------
|Some Text |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------
Example 2:
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Some Text" />
</FrameLayout>
---------------------------------------------
| |
| |
| |
| |
| |
| Some Text |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------
Example 3:
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Some Text" />
</FrameLayout>
---------------------------------------------
| |
| |
| |
| |
| |
| |
| |
| |
| Some Text |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------
Example 4:
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Some Text" />
</FrameLayout>
---------------------------------------------
| |
| |
| |
| |
| |
| |
| |
| |
| Some Text |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------
Example 5:
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Some Text" />
</FrameLayout>
---------------------------------------------
| |
| |
| |
| |
| |
| |
| |
| |
| Some Text |
| |
| |
| |
| |
| |
| |
| |
| |
---------------------------------------------

- 7,661
- 4
- 30
- 55
-
Thank you so much! Your answer combined with my answer gave me the solution! – avi12 Dec 20 '16 at 22:02
To set layout_gravity programmatically
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
youttextview.setLayoutParams(params);
Note: layout_gravity do not work with relative layout

- 5,721
- 2
- 27
- 34
-
The parent layout of the TextView is a relative layout. What should I do? – avi12 Dec 17 '16 at 14:08
-
See Redman answer for relative layout, if you want to add this programmatically then use addRule method like layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); – USKMobility Dec 17 '16 at 14:09
-
My wrong. The parent of the TextView is a relative layout, nested in a frame layout. – avi12 Dec 17 '16 at 14:12
In Linear Layout Use :
android:layout_gravity="value"
In Relative layout use properties :
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
as per your requirement

- 55,661
- 15
- 90
- 140

- 22,116
- 9
- 108
- 144
The TextView was surrounded by a HorizontalScrollView which was surrounded by a ScrollView.
I discovered that nothing worked, until I set in the XML file:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent" <!-- The value was "wrap_content" before, hence couldn't work -->
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/submit"
android:visibility="visible">
<!-- Some more code -->
Later on, I simply used:
textview.setGravity(Gravity.LEFT);
textview.setTextDirection(View.TEXT_DIRECTION_LTR);
And it solved my problem.
Thanks a lot to everybody!

- 37
- 1
- 8