Search code examples
androidxmlandroid-layoutandroid-linearlayout

Matching Width of TextView Items to Widths of Items with Different Parents using LinearLayout


Is it possible to match the widths of items with one parent with the widths of items with another parent?

I want to basically make a table where the upper portion contains a LinearLayout with horizontal TextViews that hold labels, basically a row and another scroll-enabled LinearLayout with horizontal TextViews that hold values that are updated in the app. I want the label row to have fixed width textviews and the results textviews to have the same widths. Right now I have the label widths to wrap_content, which is how I want it. How do I get the TextViews in the second layout to have the same width, even when the content will be changed?

Simply put: I want cousin textviews to have the same width, and one cousin to copy the other.

Below is my .xml code.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">



    <TextView
        android:id="@+id/resultsLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Results" />

    <TextView
        android:id="@+id/latLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Latitude" />

    <TextView
        android:id="@+id/longLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Longitude" />

    <TextView
        android:id="@+id/timeLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Time" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    android:background="@android:color/background_dark">

    <TextView
        android:id="@+id/resultsValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/latValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/longValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/timeValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        />


</LinearLayout>


Solution

  • I added LinearLayout nestings that had a main LinearLayout oriented vertically for the table, then horizontally for the columns, and then vertically again to hold the two textViews:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.android.stardustscanner.LogData">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/resultsLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center"
                    android:text="Results" />
    
                <TextView
                    android:id="@+id/resultsValue"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/latLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center"
                    android:text="Latitude" />
    
                <TextView
                    android:id="@+id/latValue"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/longLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center"
                    android:text="Longitude" />
    
                <TextView
                    android:id="@+id/longValue"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/timeLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center"
                    android:text="Time" />
    
                <TextView
                    android:id="@+id/timeValue"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:background="@color/cardview_light_background"
                    android:gravity="center" />
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:layout_width="match_parent"
                android:layout_margin="15dp"
                android:layout_height="wrap_content"
                android:text="Clear Log"/>
    
        </LinearLayout>
    
    
    </LinearLayout>