4

I have a table layout in my android application and want the first column in right and the second one in left , I try to use android:layoutDirection="rtl" but it's just work with version 4.2 and above , what can i use to set the direction from right to left in older version .

my xml file is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/form"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/barbac"
        android:gravity="right" >

        <ImageView
            android:id="@+id/imgtit"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/noticetit"
            android:layout_marginBottom="6dp"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ajeer_logo_trans2" />

        <TextView
            android:id="@+id/noticetit"
            style="@style/textstl"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_toLeftOf="@+id/imgtit"
            android:text="@string/worknotice" />
    </RelativeLayout>

    <TableLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="60dp"
        android:layoutDirection="rtl"
         >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" 
           >


 <TextView
                android:id="@+id/laborname"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/laborname" 
                />
            <TextView
                android:id="@+id/Vlaborname"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />

        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/iqamanum"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/iqamanum" />

            <TextView
                android:id="@+id/Viqamanum"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/instname1"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instname1" />

            <TextView
                android:id="@+id/Vinstname1"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" >
            </TextView>
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/instnum"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instnum" />

            <TextView
                android:id="@+id/Vinstnum"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/instactive"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instactive" />

            <TextView
                android:id="@+id/Vinstactive"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/instname2"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instname2" />

            <TextView
                android:id="@+id/Vinstname2"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/instnum2"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instnum2" />

            <TextView
                android:id="@+id/Vinstnum2"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/sdate"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/sdate" />

            <TextView
                android:id="@+id/Vsdate"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/edate"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/edate" />

            <TextView
                android:id="@+id/Vedate"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/lacation"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/location" />

            <TextView
                android:id="@+id/Vlocation"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginBottom="80dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@drawable/roundrectangle" >

            <TextView
                android:id="@+id/expdate"
                style="@style/expdatestl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="30dp"
                android:layout_marginTop="9dp"
                android:layout_weight="1"
                android:text="@string/expdate" />

            <TextView
                android:id="@+id/vexpdate"
                style="@style/expdatestl1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"

                android:layout_marginTop="9dp"
                android:layout_weight="1" />
        </TableRow>
    </TableLayout>
<TextView 
    android:id="@+id/copyrights"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:gravity="center"
      android:layout_alignParentBottom="true"
android:text="@string/copyrights"    />
</RelativeLayout>

Thanks in advance

2 Answers 2

3

LayoutDirection is a new property that was added in API v17. Before that this kind of task was done using the gravity or the layout_gravity for each specific cell. I never remember which one is it.

0

For me what worked eventually was a webview. Because then I just used CSS to design my text. You will also need an onTouchListener if you want it to do something when clicked

change your TextView to Webview:

<WebView
        android:id="@+id/noticetit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

My code:

WebView webView = (WebView) findViewById(R.id.noticetit);
private void setWebView() {
            String htmlcss = "<html lang=\"he\"><head><meta charset=\"utf-8\" />"
                    + "<style type=\"text/css\">.rtl {direction: rtl;color:black;font:20px arial;}</style></head>"
                    + "<body class=\"rtl\"><div class=\"rtl\">" + webView
                    + "</div></body></html>";

            webView.loadDataWithBaseURL(null, htmlcss, "text/html", "utf-8", null);
            webView.setOnTouchListener(new OnTouchListener() {
                // Removed @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (v.getId() == R.id.maintitle
                            && event.getAction() == MotionEvent.ACTION_DOWN) {
                        //Put your code here
                    }
                    return false;
                }
            });
        }

Hope this helps you out!

Not the answer you're looking for? Browse other questions tagged or ask your own question.