How set the android:gravity to TextView from Java side in Android
Asked Answered
S

7

253

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?

Snorter answered 23/9, 2010 at 5:45 Comment(0)
P
596
labelTV.setGravity(Gravity.CENTER | Gravity.BOTTOM);

Kotlin version (thanks to Thommy)

labelTV.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM

Also, are you talking about gravity or about layout_gravity? The latter won't work in a RelativeLayout.

Popcorn answered 23/9, 2010 at 7:34 Comment(5)
Thanks that worked. I feel a little silly now for missing that. Originally the problem was setting the margins on a text field. I guess I assumed because i couldnt find a setMargin() method that setGravity wouldnt exist either.Snorter
Thanks +1, I found the documentation, but as usual it only goes half way, not clearly stating where I get the gravity constants from (Gravity.CENTER etc).Shauna
For anyone who comes here from Google, like me, the Kotlin way is: it.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOMGlennieglennis
To me the kotlin way is confusing, why is it or and not and ?!??!Electrolyse
@Electrolyse It's not a Logical operator (||) but a Bitwise operator (|). The bitwise OR takes the bits of the two values and checks if one (or both) of the bits on the same location is 1, and if so will output this as a 1. For example: 0010 | 1000 = 1010, 1010 | 0101 = 1111, 1100 | 1101 = 1101Lone
H
40

This will center the text in a text view:

TextView ta = (TextView) findViewById(R.layout.text_view);
LayoutParams lp = new LayoutParams();
lp.gravity = Gravity.CENTER_HORIZONTAL;
ta.setLayoutParams(lp);
Horntail answered 23/9, 2010 at 5:54 Comment(5)
That didn't work. "lp.gravity cannot be resolved or is not a field"Snorter
Thats strange because this code works and compiles just fine for me. How would you set the weight of a textview as their is no setWeight() method?Horntail
I found the problem. Nate is importing android.view.ViewGroup. That class has different kinds of methods and doesn't compile. You should import android.view.WindowManager instead.Fronnia
Should better import android.widget.LinearLayout.LayoutParams when text view is inside linear layout. Othervise you get ClassCastExceptionTriturate
Thanks, it works. But it is not a complete solution because a type of LayoutParams is not defined here and can vary, so it won't compile. You should know a parent layout (for instance, LinearLayout) and write it here.Celesta
M
10

We can set layout gravity on any view like below way-

myView = findViewById(R.id.myView);
myView.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
 or
myView.setGravity(Gravity.BOTTOM);

This is equilent to below xml code

<...
 android:gravity="center_vertical|right"
 ...
 .../>
Mathur answered 19/9, 2016 at 5:4 Comment(1)
Layout gravity is not same as gravityRatepayer
I
3

You should use textView.setGravity(Gravity.CENTER_HORIZONTAL);.

Remember that using

LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams2.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

won't work. This will set the gravity for the widget and not for it's text.

Irrelievable answered 17/3, 2017 at 7:1 Comment(0)
M
2

Use this code

        TextView textView = new TextView(YourActivity.this);
        textView.setGravity(Gravity.CENTER | Gravity.TOP);
        textView.setText("some text");
Mcmillen answered 7/7, 2017 at 9:35 Comment(0)
T
2
textView.setGravity(Gravity.CENTER | Gravity.BOTTOM);

This will set gravity of your textview.

Twotime answered 7/7, 2017 at 10:2 Comment(0)
O
1

Solved this by doing a few things, first getting the height of my TextView and diving it by the text size to get the total amount of lines possible with the TextView.

int maxLines = (int) TextView.getHeight() / (int) TextView.getTextSize();

After you get this value you need to set your TextView maxLines to this new value.

TextView.setMaxLines(maxLines);

Set the Gravity to Bottom once the maximum amount of lines has been exceeded and it will scroll down automatically.

if (TextView.getLineCount() >= maxLines) {
    TextView.setGravity(Gravity.BOTTOM);
}

In order for this to work correctly, you must use append() to the TextView, If you setText() this will not work.

TextView.append("Your Text");

The benefit of this method is that this can be used dynamically regardless of the height of your TextView and the text size. If you decide to make modifications to your layout this code would still work.

Ommatidium answered 2/9, 2019 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.