Programmatically Set Textview Gravity Right In A Tablerow
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.
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 tomatch_parent
and then assign the gravity on the TextView usingtextView.setGravity(Gravity.END)
.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"