Android gravity problem with TableLayout

Trying to set layout gravity to the right in your Android app where you use TableLayout but nothing seems to happen? Try use to use stretchColumns!

How to get gravity right to work

If you just write the following nothing will happen since no column in your table is able to stretch itself to allow the gravity right command to do it’s work:

 

 

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=”Some text” />
    android:layout_gravity="right" > 

What you need to to is to add the stretchColumn command to the TableLayout command. This command determines what column/columns that you allow to be stretched (0 means the first column etc.). If you write the following in your TableLayout tag you allow the second column to stretch itself and the layout starts working as a result:

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:stretchColumns="1"

Leave a comment

Your email address will not be published. Required fields are marked *