Melon Therapy Melon Therapy - 1 month ago 10
Android Question

Make a view's width at least as big to different view

I have two view below each other. The bottom view needs to be at least as big as the top view.

I am able to constrain the bottom view to have same size as the top one, but it probihits the view to have bigger width.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Base textview"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/module_color_blue"
android:id="@+id/textView"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Smaller"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:background="@color/red"
app:layout_constraintLeft_toLeftOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintRight_toRightOf="@+id/textView"/>

</android.support.constraint.ConstraintLayout>


On the other hand I am able to have the bottom view unconstrained, however view can get smaller that the top one.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Base textview"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/module_color_blue"
android:id="@+id/textView"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Much longer that top textview"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:background="@color/red"
app:layout_constraintLeft_toLeftOf="@+id/textView"
/>

</android.support.constraint.ConstraintLayout>


Can I achieve that with existing layout or do I need special layout for that ?

Scenario A:

enter image description here

The bottom view is smaller that top so they have the same width

Scenario B:

enter image description here

The bottom view is bigger that top its not constrained

Answer

Put both views inside LinearLayout and set LinearLayout width as wrap_content and top view with as match_parent and bottom view width as wrap_content :

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text1"/>
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text2text2"/>
</LinearLayout>