Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - "wrap_content" doesn't wrap the content

I'm trying to make my Recycler view to not take all the screen space when there are just a few items on it using wrap_content, but that doesn't seem to work. What's the problem?

enter image description here Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal"
        android:weightSum="1">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/smoke_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:paddingTop="4dp"
            android:paddingRight="4dp"
            android:paddingLeft="4dp"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="25dp"
            android:background="#FFF"
            android:elevation="2dp" />
    </LinearLayout>
</RelativeLayout>
like image 334
Milen Pivchev Avatar asked Jan 29 '26 22:01

Milen Pivchev


2 Answers

The most important thing is that RecyclerView does not support wrap_content by default. You have to struggle a bit and provide it with a LayoutManager that can force your RecyclerView to wrap its contents.

Take a look at this and this.

like image 141
Bartek Lipinski Avatar answered Jan 31 '26 12:01

Bartek Lipinski


use android:layout_weight="1" in RecyclerView not 0.8 and Linear layout android:layout_width="wrap_content" not fill_parent

like image 38
Anil Avatar answered Jan 31 '26 11:01

Anil