Skip to content Skip to sidebar Skip to footer

Programmatically Set Textview Gravity Right In A Tablerow

I know this has been asked several times over, but can't seem to get it to work correctly in my situation. I am trying to get the last column to align right in a table that is gene

Solution 1:

In order for setGravity() to work you must first modify the width field of yourTextView as follows in your layout:

android:layout_width="fill_parent"

Now you should be able to call:

tvPeriodHours.setGravity(Gravity.RIGHT)

Solution 2:

I think there are two ways of doing this.

  1. If your TableLayout has a width not based on it's contents (such as if the columns widths are set using android:stretchColumns) you can set the TextView width to match_parent and then assign the gravity on the TextView using textView.setGravity(Gravity.END).

  2. If your TextView width is smaller than the bounds of the TableLayout cell you can call the gravity on the TableRow instead using tableRow.setGravity(Gravity.END).

I have a TableLayout where my TextViews fill the width of the cell but not the height so I'm using:

textView.setGravity(Gravity.CENTER);
tableRow.setGravity(Gravity.CENTER_VERTICAL);

To get my text in the very centre of the cell.

In case it helps anyone, I have spent ages trying to get the TextView aligned to the bottom of the cell but nothing seems to work, including tableRow.setGravity(Gravity.CENTER_VERTICAL), no idea why but I have given up and in the centre is fine.

Post a Comment for "Programmatically Set Textview Gravity Right In A Tablerow"