Question

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:

<TableLayout
    android:id="@+id/gameBoard"
    android:layout_width="@dimen/boardWidth"
    android:layout_height="@dimen/boardWidth"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:shrinkColumns="*"
    android:stretchColumns="*" >
</TableLayout>

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

TableLayout gameBoard = (TableLayout) findViewById(R.id.gameBoard);
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);
        cloud.setImageResource(R.drawable.transparent_cloud);
        cloud.setAdjustViewBounds(true);
        flipper.addView(cloud, 0);

        //second image
        ImageView landscape = new ImageView(this);
        landscape.setImageResource(R.drawable.land_scape);
        landscape.setAdjustViewBounds(true);
        flipper.addView(landscape, 1);

        row.addView(flipper);
    }
    gameBoard.addView(row, rowParams);
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top