androidandroid-linearlayoutcardview

LinearLayout inside CardView not filling the whole space


Why is my Linear Layout not taking up the whole space inside the CardView? When I use the preview and expand the LinearLayout to match the CardViews width it sets the width to match_parent (which I tried manually) but then goes back to how it is now. In other words: I want the red background to go all the way to the right and also the right TextView aligned to the right.

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:id="@+id/itemCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:layout_marginBottom="3dp"
    android:layout_marginTop="3dp"
    android:background="@color/Mat"
    android:padding="3dp"
    app:cardElevation="4dp"
    app:cardCornerRadius="1dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:background="@color/Mat"
        android:orientation="horizontal"
        android:padding="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">


        <TextView
            android:id="@+id/textView2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/circle"
            android:gravity="center"
            android:text="DEU"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Frau Hardt"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textView4" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="O03"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

Edit: After removing the min-width and setting the width of the whole layout to match_parent it goes all the way to the right. Now how do I get to center the TextView with the name horizontally and have the text view on the right align to the right of the whole layout?

I included a picture because it looks all nice right next to each other. I hope the code (especially since it's not a whole lot) doesn't have to be as text. If so I'll edit the question of course! Thanks!


Solution

  • Make your middle TextView as this;

    <TextView
       android:layout_width="0dp"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       ... />
    

    It fills the remaining middle space.

    You also should use match_parent instead of fill_parent since it is deprecated.