3

How can I add views to TableRow right-to-left? the default mode is left-to-right. I tried android:gravity="right" and android:layout_gravity="right" inside both TableLayout and TableRow but didn't work. for example:

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layoutDirection="rtl"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:layoutDirection="rtl">

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text" />
    </TableRow>
</TableLayout>

the result is button1 at the left and textView1 at the right but I need vice versa .

STF
  • 1,485
  • 3
  • 19
  • 36
Soheil Setayeshi
  • 2,343
  • 2
  • 31
  • 43

2 Answers2

3
ViewCompat.setLayoutDirection(tableRow,ViewCompat.LAYOUT_DIRECTION_RTL);
Soheil Setayeshi
  • 2,343
  • 2
  • 31
  • 43
2

Your answer is simple. As with any table the first entry you chose will appear first, in your case button1, you need this to be TextView for it to be placed before the button1.

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layoutDirection="rtl"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:layoutDirection="rtl">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />


    </TableRow>
</TableLayout>
furkick
  • 423
  • 6
  • 18
  • 1
    You completely misunderstood my question. "first" is where? right or left? :) the default is LTR , I need RTL dude. I told you that I'm doing this dynamically , this XML is just an example – Soheil Setayeshi May 03 '14 at 11:18
  • If you check the XML I have provided then button1 appears to the right and the text is now at the left, as you stated you wanted? – furkick May 03 '14 at 11:20
  • 1
    :| I don't want to change the order of adding views dude! consider I have an array of views from index 0 to n , I want to add view[0] to the right of the TableRow not view[n] – Soheil Setayeshi May 03 '14 at 11:23
  • You need to clarify your question more then. You are never going to get an answer if you don't post the correct source code, that's not just me that will be the same for anyone on here. – furkick May 03 '14 at 11:26
  • Like I said post the Java code, then expect an answer. – furkick May 03 '14 at 11:36
  • 1
    It was pretty clear to me sorry I dont have answer, im looking for the same thing – Edon Freiner Oct 03 '17 at 01:05