Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate TableLayout with android:layout_weight programmatically

I am trying to populate a tablelayout with cells and rows stretches to fill the whole screen. Like this:

http://imageshack.us/photo/my-images/69/device20120201005942.png/

This layout designed in Graphical Layout window in Eclipse gives what I want (android:layout_weight solves vertical stretching):

<?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:layout_gravity="center"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" android:layout_weight="1">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_gravity="center"
            android:src="@drawable/add" />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_gravity="center"
            android:src="@drawable/bound" />
        <ImageView
            android:id="@+id/imageView3"
            android:layout_gravity="center"
            android:src="@drawable/cod" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

    <ImageView
        android:id="@+id/imageView4"
        android:layout_gravity="center"
        android:src="@drawable/delete" />
    <ImageView
        android:id="@+id/imageView5"
        android:layout_gravity="center"
        android:src="@drawable/delete2" />
    <ImageView
        android:id="@+id/imageView6"
        android:layout_gravity="center"
        android:src="@drawable/floppy" />
    </TableRow>
</TableLayout>

I am trying to create the same look within code but failed so far. Here is the oncreate of my Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablelo);

    TableLayout tl = (TableLayout) findViewById(R.id.table);
    Log.d(TAG, "Width :" + tl.getWidth());
    for (int row = 0; row < 3; row++) {
        TableRow tr = new TableRow(this);
        tr.setId(100 + row);

        for (int cell = 0; cell < 3; cell++) {
            ImageView image = new ImageView(this);
            image.setImageResource(imageIDs[row * 3 + cell]);
            tr.addView(image);
        }

        tr.setGravity(Gravity.CENTER);
        LayoutParams params = new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);

        tr.setLayoutParams(params);

        tl.addView(tr, params);
    }
    tl.setStretchAllColumns(true);
}

I am missing the code to set ImageView gravity, but this attemp causes crash:

image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));

This code results 3 rows created, each stretch horizontally but not vertically:

http://imageshack.us/photo/my-images/835/gridc.png/

I can not find what I am missing here. I also tried to prepare a tablerow in designer and inflate and add to table during runtime but result did not change.

So, somehow, I want to get programmatically the same layout that I could created with editor .

Help, please!

like image 693
volkan Avatar asked Jan 31 '12 23:01

volkan


1 Answers

I've spent an hour and a half trying to figure out what was going on. And then I found it :) Here's a complete listing of my TableActivity, please note the usage of different LayoutParams for table rows and images:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.*;
import android.widget.TableLayout;
import android.widget.TableRow;

public class TableActivity extends Activity {

    private static final String TAG="TableActivity";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_activity);

        TableLayout tl = (TableLayout) findViewById(R.id.table);
        Log.d(TAG, "Width :" + tl.getWidth());

        for (int row = 0; row < 3; row++) {
            TableRow tr = new TableRow(this);
            tr.setId(100 + row);

            //Note that you must use TableLayout.LayoutParams, 
            //since the parent of this TableRow is a TableLayout
            TableLayout.LayoutParams params = new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f);

            for (int cell = 0; cell < 3; cell++) {
                ImageView image = new ImageView(this);
                image.setImageResource(android.R.drawable.btn_plus);
                //Note that here you must use TableRow.LayoutParams
                //since TableRow is the parent of this ImageView
                TableRow.LayoutParams imageParams = new TableRow.LayoutParams();

                //And this is how you set the gravity:
                imageParams.gravity = Gravity.CENTER;
                image.setLayoutParams(imageParams);
                tr.addView(image);
            }
            tr.setLayoutParams(params);
            tl.addView(tr, params);
        }
        tl.setStretchAllColumns(true);
    }
}

table_activity.xml is just an empty table layout:

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

So to put it short, the simplest solution would be to use layouts defined in xml files :) But if you have to use JAVA, you must be careful with what king of LayoutParams you are using, you should always use the one of a view's parent.

like image 50
Anton Cherkashyn Avatar answered Nov 15 '22 05:11

Anton Cherkashyn