0

I am trying to understand how Android layouts work. If I make a simple Linear Layout and add some Text Fields, and set the width of the Linear Layout to wrap_content, I noticed an unusual effect when changing the width of the Text Fields. If the width of the Text Field is wrap_content, then the size of the Text Field is as expected (the width of the text). However, if I set the width of the Text Field to match_parent, the size of the Text Field grows to be the width of the device screen. I expected the size of the Text View to be the width of the text, not the size of the screen display.

In other words, if the width of the parent layout is wrap_content and the width of the child view is wrap_content, then it seems the width of the child view grows to be the width of the device screen, ie the same as setting the width of the parent view to be match_parent. Can someone explain why this happens, as it seems counter intuitive to me.

Here is some simple code to illustrate what I am talking about. Change the width of the Kunal Text View from wrap_content to match_parent.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray">

    <TextView
        android:text="VIP List"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4CAF50"
        android:textSize="24sp" />

    <TextView
        android:text="Kunal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4CAF50"
        android:textSize="24sp" />

    <TextView
        android:text="Kagure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4CAF50"
        android:textSize="24sp" />

    <TextView
        android:text="Lyla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4CAF50"
        android:textSize="24sp" />

</LinearLayout>
user1045680
  • 815
  • 2
  • 9
  • 19
  • Possible duplicate of [What's the difference between fill\_parent and wrap\_content?](http://stackoverflow.com/questions/432763/whats-the-difference-between-fill-parent-and-wrap-content) – Oscar LT Mar 10 '16 at 16:15
  • Does not happen to me. When parent wraps the content and children match the parent or wrap the content, the parent gets as wide as the widest child. On Android Marshmallow. Maybe it depends on the android version? – AlbAtNf Mar 10 '16 at 16:22

1 Answers1

0

make this.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray">

You will see that "kunal" and "kagure" take all the screen's width but "Vip list" and "lyla" get the width tha it need.

Cliff
  • 682
  • 8
  • 30