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?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Nate
  • 2,720
  • 2
  • 16
  • 17

7 Answers7

592
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.

Maragues
  • 37,861
  • 14
  • 95
  • 96
  • 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. – Nate Sep 23 '10 at 09:17
  • 7
    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). – Hein du Plessis Oct 09 '12 at 05:41
  • 4
    For anyone who comes here from Google, like me, the Kotlin way is: ```it.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM``` – Thommy May 31 '19 at 13:50
  • To me the kotlin way is confusing, why is it `or` and not `and` ?!??! – ZooMagic Jan 17 '20 at 10:21
  • 2
    @ZooMagic 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 = 1101` – Sven van Zoelen Oct 09 '20 at 11:03
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);
z3ntu
  • 190
  • 7
  • 20
radiofrequency
  • 873
  • 8
  • 19
  • 5
    That didn't work. "lp.gravity cannot be resolved or is not a field" – Nate Sep 23 '10 at 06:01
  • 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? – radiofrequency Sep 25 '10 at 18:14
  • 4
    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. – Finnboy11 Jan 14 '12 at 14:34
  • Should better import android.widget.LinearLayout.LayoutParams when text view is inside linear layout. Othervise you get ClassCastException – tomm May 10 '15 at 12:38
  • 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. – CoolMind Oct 17 '16 at 09:32
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"
 ...
 .../>
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
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.

Irshu
  • 8,248
  • 8
  • 53
  • 65
2

Use this code

        TextView textView = new TextView(YourActivity.this);
        textView.setGravity(Gravity.CENTER | Gravity.TOP);
        textView.setText("some text");
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
2
textView.setGravity(Gravity.CENTER | Gravity.BOTTOM);

This will set gravity of your textview.

DAS
  • 442
  • 1
  • 6
  • 21
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.