3

I'm trying to set the height of the first view on my recyclerview to match_parent. That is, the first view should cover the entire device. Here's the XML for this first view, note height and width set to match_parent

<android.support.v7.widget.CardView 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="match_parent"
app:contentPadding="14dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/main_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="IC"
            android:textSize="100sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/temp"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:gravity="center"
                android:text="12°"
                android:textColor="@color/normal_text"
                android:textSize="86dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="LIGHT SNOW"
                android:textColor="@color/light_text"
                android:textSize="14dp" />


        </LinearLayout>


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="20dp"

        android:paddingRight="20dp"
        android:paddingTop="20dp">

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/humidity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf07a;"
            app:sub="Humid." />

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/tempMax"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf050;"
            app:sub="East" />

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/tempMin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf079;"
            app:sub="Press." />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="20dp">

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/clouds"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf013;"
            app:sub="Cluds" />

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/precipitation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf084;"
            app:sub="0.29 mm" />

        <com.feresr.rxweather.UI.views.InfoDisplay
            android:id="@+id/feels_like"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:ic="&#xf055;"
            app:sub="Feels like " />
    </LinearLayout>
</LinearLayout>

For some reason, when running this on my device I see the view as wrap content instead. Any kind help will be appreciated.

frankelot
  • 13,666
  • 16
  • 54
  • 89

3 Answers3

2

I ended up calculating the height at runtime and setting it to my view dinamically.

            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int height = size.y;
            currentlyViewHolder.view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));

If anyone know how to do it on XML I can change the accepted answer.

frankelot
  • 13,666
  • 16
  • 54
  • 89
1

You are right, the vertical recyclerView doesn't have height is match Parent if you use WindowManager and Point.size. I'll explain how your answer work in this Image:

enter image description here

To resolve this problem You can use ViewTreeObserver like this in Activity's onCreate:

ViewTreeObserver vto = recyclerViewItems.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            recyclerViewItems.getViewTreeObserver().removeOnPreDrawListener(this);
            //get height of RecyclerView's Match Parent
            finalHeight = recyclerViewItems.getMeasuredHeight();           

            LinearLayoutManager itemsLayoutManager = new LinearLayoutManager(getApplicationContext());
            itemsLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerViewItems.setLayoutManager(itemsLayoutManager);
            VerticalAdapter verticalAdapter = new VerticalAdapter(DataList<>());
            recyclerViewItems.setAdapter(verticalAdapter);
            return true;
        }
    });
TranHieu
  • 914
  • 6
  • 23
1

whether or not match_parent in an XML layout works depends on how it is inflated. So our list item views should be inflated as follows in order to make the android:layout_height="match_parent" in the view holder to work:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.viewholder, recyclerView, false);

A couple of important things to note about the code above is that:

  • LayoutInflater.inflate(resource,root,attachToRoot) is the method used to inflate the list item views.

  • a reference to the recycler view was passed as the root parameter into the inflate method. This is important because when match_parent is encountered for the root node of the inflating XML, it will match the dimensions of the root View.

  • false is passed into the inflate method for the attachToRoot parameter. This is important because we don't actually want the inflater to attach the inflated view to theroot... the recycler view will take care of attaching the view to itself.

This other stackoverflow answer is very related.

Community
  • 1
  • 1
Eric
  • 16,397
  • 8
  • 68
  • 76