Dibbus.com

Where design meets development

Equal width in Android TableLayout

| 2 Comments

Using table layout in Android is pretty nasty. As a webdeveloper I’m known to the default behaviour of tables. Using table layout in Android, it’s completely different en you have to think in an other way.
To create equal width for the cells is painfully hard to create in de layout.xml. It’s even harder to create the table dynamically. I’ve searched the smallest area’s of the web but couldn’t find a single nice piece of code, so I had te create my own.
The end result will look like:


I’m talking about the second row of textviews which are placed inside a table layout, all with equal width.

So what’s the code:
First create the tablelayout in your xml:

<TableLayout
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/tblScore"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content" android:layout_marginTop="1dp">
	    <TableRow android:id="@+id/questionRow">
    	</TableRow>
</TableLayout>

And in your class, create textviews and place them insite the table row:

public void SetQuestionsInTopRow(int numberOfQuestions){
		TableRow tr = (TableRow) findViewById(R.id.questionRow);
		tr.setWeightSum(numberOfQuestions);
		for (int i = 1; i <= numberOfQuestions; i++) {
			tableScore.setColumnStretchable(i-1, true);
			TextView tv = new TextView(this);
			tv.setText(String.valueOf(i) + " ");
			tv.setTextSize(12);
			tv.setShadowLayer(1, 1, 1, this.getResources().getColor(R.color.black));
			tv.setBackgroundResource(R.drawable.circle_black);
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
			tv.setId(100+i);
			tv.setTextColor(tv.getContext().getResources().getColor(R.color.white));
			tr.addView(tv);
		}
	}

Important in this code is the weightsom for the table row:

tr.setWeightSum(numberOfQuestions);

and the setcolumnsstretchable for the table:

tableScore.setColumnStretchable(i-1, true);

Thit code will create dynamically generated textviews inside a tablerow with equal width.

Author: Folkert

I'm a webdeveloper, looking for the best experience, working between development and design. Just a creative programmer. When I'm getting tired of programming C#, i'd love to create 3D images in 3D Studio Max, play the guitar, create an app for Android or crush some plastics on a climbing wall or try to stay alive when i´m descending some nice white powdered snowy mountains on my snowboard.

2 Comments

  1. just came across this, you can relatively easy do it by the stretchColumns attributes:

  2. Thanks a ton,
    I was searching for this solution and got meddled with all sort of layout parameter tricks which didn’t work. However your solution worked like charm. On points like this android developer documents also don’t provide correct way of getting things done. Would like to just ask you how you found out this solution? I regularly get stuck when going through android developer docs to find which functionality i need to fiddle with.
    Thanks again,

Leave a Reply

Required fields are marked *.

*


Page optimized by WP Minify WordPress Plugin