Adriano Kerber
Adriano Kerber

Reputation: 51

How to obtain layout divider size?

I have a TableLayout that receives some TableRows. What I'm trying to do is resize the elements on the row so all the elements fill the entire row each with the same relative size.

The resources::

TableView:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/attributesTableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="20dp"
    android:paddingEnd="20dp">

    <TableRow
        android:divider="@drawable/vertical_divider"
        android:dividerPadding="10dp"
        android:showDividers="middle"
        android:visibility="gone"
        tools:visibility="visible">

        <include layout="@layout/cell_attribute" />

        <include layout="@layout/cell_attribute" />

        <include layout="@layout/cell_attribute" />

        <include layout="@layout/cell_attribute" />

    </TableRow>

</TableLayout>

@layout/cell_attribute (The layout for the element):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/attributeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/backgroundFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#008888"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/attributeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@color/white"
            android:src="@mipmap/attribute_icon_pet"/>

        <TextView
            android:id="@+id/attributeValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="Dog"/>

    </LinearLayout>

</FrameLayout>

@drawable/vertical_divider:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="1dip" />
    <solid android:color="@color/colorMediumGrey" />
</shape>

I know that a perfect solution to have equal sizes for each item on the TableRow layout is to define an equal weight for each item. Eg:

<include layout="@layout/cell_attribute"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

But the problem is that if the rows don't have the same number of items on each, the items will have different widths. So since I instantiate the items and rows dynamically to the TableView, a solution would be to calculate the size of each item on a row based on the maximum quantity of items per row (The number of columns). The only problem is that I couldn't figure out the correct relative size for the divider on the layout.

So back to my original question: How to obtain layout divider size?

Upvotes: 1

Views: 74

Answers (1)

Adriano Kerber
Adriano Kerber

Reputation: 51

I found a way to correctly calculate the width of the elements on a column. The missing piece was how to obtain the divider's drawable relative size, that was achieve using the method getIntrinsicWidth. So the solution found was calculating the elements width (The columnWidth variable) based on the max of elements per row. The code created to do the job is:

// Divider drawable for each row
Drawable dividerDrawable = context.getResources().getDrawable(R.drawable.vertical_divider);

// Measure column width (Elements width)
int totalDividersPerRowSize = dividerDrawable.getIntrinsicWidth() * (maxItemsByRow - 1); // The separators' total size
int columnWidth = table.getMeasuredWidth() - totalDividersPerRowSize; // columnWidth subtracting dividers
columnWidth -= table.getPaddingStart() + table.getPaddingEnd();
columnWidth /= maxItemsByRow; // max quantity of items divided by the table total width

Upvotes: 1

Related Questions