Search code examples
androidalignmenttablerow

TextView inside a TableRow does not display text, properly, in Android


I'm writing a simple Android application that needs some text which should be written from right to left. So I used a TableRow as follows:

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView
        android:id="@+id/ada"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_column="1"
            android:text="Open..."
            android:padding="3dip" />

    </TableRow>

    <TableRow>
        <TextView
        android:id="@+id/myview"
        android:layout_marginRight="34dip"
        android:layout_marginLeft="6dip"
        android:layout_height="100dip"
        android:layout_width="wrap_content"
        android:layout_column="1"
            android:text="Ctrl-O"
            android:gravity="right"
             />

    </TableRow>


</TableLayout>

And inside activity, I wrote:

 TextView myView=(TextView)findViewById(R.id.myview);
  myView.setText("welcome 1 ");

It works well and the "welcome 1" is positioned on the right ( as desired). But when the length of text increases, TextView does not display the text properly and end of each line is out of screen. Here is a screenshot, if I change above code as follows:

myView.setText("welcome 1  welcome 2  welcome 3  welcome 4  welcome 5 welcome 6  welcome 7 welcome 8 welcome 9 welcome 10 ");

enter image description here

Your help would be appreciated.


Solution

  • Your TextView is being pushed to the right by your android:layout_marginLeft attribute. Margins are outside the view, which you've set to wrap all your content. You probably want to use android:layout_paddingLeft instead. Padding is placed inside the view itself, so does not add to the space it takes up in the parent.