CorayThan CorayThan - 11 months ago 49
Android Question

Android ViewFlipper causes images to not adjust size in table

I'm using a TableLayout to create a square gameboard in Android. I want to set the dp of the table's width and height, and have its contents automatically expand and contract to fit the table.

I got this working with the entries in the tableRows as LinearLayouts with a single ImageView inside, but I need to change what images are displayed with click actions, so I thought I would use a ViewFlipper.

Now that I'm using the ViewFlipper, though, my image sizes have gone all wonky. Does anyone know if there's a setting I can set on ViewFlipper to get the images to auto adjust their size in a table?

Here's my xml:

android:stretchColumns="*" >

And here's the code I'm programmatically creating the table with:

TableLayout gameBoard = (TableLayout) findViewById(;
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 0);
for (int y = settings.getBoardSide() - 1; y > -1; y--) {
TableRow row = new TableRow(this);
row.setPadding(0, 0, 0, 0);

for (int x = 0; x < settings.getBoardSide(); x++) {

ViewFlipper flipper = new ViewFlipper(this);
flipper.setOnClickListener(new TileClick());

//default image
ImageView cloud = new ImageView(this);
flipper.addView(cloud, 0);

//second image
ImageView landscape = new ImageView(this);
flipper.addView(landscape, 1);

gameBoard.addView(row, rowParams);

Answer Source

It turned out that for my purpose a FrameLayout with its width and height set to a specific dp was best. That way I can control the view and ensure it has the right height and width. With the FrameLayout I can add and remove views at will, and layer different views on top of each other as well.