Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align item in table row (left - center - right)

I'm trying to align items in a table row (left/center/right positions), but i could dot get it work can someone help me looking at below snippet,,,

Table Row

should align to textview1 = left textview2 = center imgview1 = right

xml for table row

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


    <TextView
        android:id="@+id/focast_day"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="left"
        android:ellipsize="end"
        android:maxLines="1"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:text="@string/na_msg"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />


     <TextView
        android:id="@+id/focast_day_temp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="1"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:text="@string/na_msg"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/focast_day_skyIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="right"
        android:scrollHorizontally="false" />

</TableRow>

myTableLayout

        <TableLayout
            android:id="@+id/weatherforcastTable"
            android:layout_width="fill_parent"
            android:stretchColumns="2"
            android:layout_height="wrap_content"
            android:layout_below="@+id/today_temp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="34dp" >
        </TableLayout>

code for filling the data to table row

 for (Forecastday forecastday : forcastDayList) {


                        View view = inflater.inflate(R.layout.weather_forcast_day_view, null);
                        TextView focast_day = (TextView) view.findViewById(R.id.focast_day);
                        TextView focast_day_temp = (TextView) view.findViewById(R.id.focast_day_temp);
                        ImageView  focast_day_skyIcon = (ImageView) view.findViewById(R.id.focast_day_skyIcon);

                        TableRow row = new TableRow(_appContext);
                        focast_day.setText(fmtDate);


                        focast_day_temp.setText(forecastday.getHigh().getCelsius() +ApplicationConstants.STRING_SPACE +ApplicationConstants.CELSIUS_DEGREES);
                        focast_day_temp.setId(count);


                        focast_day_skyIcon.setImageBitmap(forecastday.getSkyiconBitMap());


                        row.addView(view);
                        weatherforcastTable.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT));
                    }
like image 633
Sam Avatar asked Feb 18 '13 16:02

Sam


People also ask

How do you align items in a table?

Table data defaults to left alignment; table headers to center. In order to change the alignment in one cell, insert the appropriate "ALIGN=" attribute within the code for that cell. In order to change the alignment in all cells in a row, insert the appropriate alignment attribute within the code for that row.

How do you center align text in a table row in HTML?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.


3 Answers

Could you try this : stretchColumns="0,1,2" on the TableLayout

<TableLayout
        android:id="@+id/weatherforcastTable"
        android:layout_width="fill_parent"
        android:stretchColumns="0,1,2"
        android:layout_height="wrap_content"
        android:layout_below="@+id/today_temp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp" >
</TableLayout>
like image 93
lokoko Avatar answered Oct 18 '22 12:10

lokoko


Did you try using "layout_gravity" instead of "gravity" on the first and last columns in the xml layout for the row? This might fix your problem by aligning forecast_day to the left of the parent table and forecast_day_skyIcon to the right.

Here's a good visualization of gravity vs. layout_gravity

http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity.html

like image 38
frenziedherring Avatar answered Oct 18 '22 13:10

frenziedherring


This one worked for me.

<TableRow 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

Hope it helps.

like image 20
Christine Tuazon Avatar answered Oct 18 '22 13:10

Christine Tuazon