Hazed 2.0
Hazed 2.0

Reputation: 133

Trying to get table rows equal height in table layout

I am using a TableLayout where each table row will consist of three cells which will each display an image. I want each imageview/cell to be of equal height and width. The problem is, the tablerow height by default wraps to the size of the imageview/cell with the largest height, so the rows are not equal height.

        TableLayout tableLayout = (TableLayout) getActivity().findViewById(R.id.tableLayout);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(tableLayout.getWidth()/3, ViewGroup.LayoutParams.WRAP_CONTENT, 0.3f);
            TableRow tableRow = new TableRow(getActivity().getApplicationContext());
            lp.setMargins(1,1,1,1);
            tableRow.setLayoutParams(lp);
            imageView.setImageBitmap(b);
            imageView.setLayoutParams(lp);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            tableRow.addView(imageView);
            tableLayout.addView(tableRow);

Here is the xml

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:stretchColumns="*"
                android:weightSum="1"
                app:layout_constrainedHeight="false"
                app:layout_constraintHeight_percent="0.5"
                app:layout_constraintWidth_percent="1">
            </TableLayout>

Here is what it looks like currently enter image description here enter image description here

Upvotes: 1

Views: 291

Answers (1)

alireza daryani
alireza daryani

Reputation: 835

you can use 2 way for fix your problem

One:

set something like this code in your xml

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="200dp"
            android:layout_height="200dp"
            app:layout_constrainedHeight="false"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintWidth_percent="1">
        </TableLayout>

in this way you say to your layout that use the width and height that you want like Instagram.

Two: send same size images to your app or resize it to what you want

i prefer both ways together.

Update me in comments

Upvotes: 1

Related Questions