199

My RecyclerView and item has match_parent width but the result is : enter image description here

    <view
    class="android.support.v7.widget.RecyclerView"
    android:layout_width="match_parent"

and items:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"

full:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:gravity="right"
>


<Button
    android:layout_width="0dp"
    android:layout_weight="15"
    android:layout_height="fill_parent"
    android:text="ملاحظات"
    android:id="@+id/button" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="20"
    android:gravity="center"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            fab:fab_plusIconColor="#ff56ff83"
            fab:fab_colorNormal="@color/d_red"
            fab:fab_colorPressed="#ff5c86ff"
            fab:fab_size="mini"
            fab:fab_icon="@drawable/ic_remove_white"
            android:id="@+id/fab_rmv" />
        <esfandune.ir.elmikarbordiardakan.other.CustomTxtView
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="0"
            android:gravity="right|center_vertical"
            android:id="@+id/txt_takhir_itm" />
        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            fab:fab_plusIconColor="@color/colorprimarylight"
            fab:fab_colorNormal="@color/colorprimarydark"
            fab:fab_colorPressed="@color/colorprimary"
            fab:fab_size="mini"
            fab:fab_icon="@drawable/ic_add_white"
            android:id="@+id/fab_add" />

    </LinearLayout>
</LinearLayout>
    <Spinner
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="10"
        android:id="@+id/sp_nomre_itm"

        android:entries="@array/degrees"/>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    android:gravity="center"
    >
    <!--LinearLayout baraye ine ke nameshod fab ro weight behosh dad-->
    <com.getbase.floatingactionbutton.FloatingActionButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        fab:fab_plusIconColor="#ff56ff83"
        fab:fab_colorNormal="@color/d_green"
        fab:fab_colorPressed="@color/d_orange"
        fab:fab_size="normal"
        fab:fab_icon="@drawable/ic_done_white"
        android:id="@+id/fab_hazr" />



</LinearLayout>
<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
    android:layout_weight="5"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="100"
    android:gravity="right|center_vertical"
    android:id="@+id/txt_ghybtNumber_itm" />

<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
        android:layout_weight="30"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="عباسعلی ملاحسینی اردکانی"
        android:gravity="right|center_vertical"
        android:id="@+id/txt_title_itm"
    android:layout_marginRight="10dp"
    />

<view
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    class="de.hdodenhof.circleimageview.CircleImageView"
    android:id="@+id/view"
    android:src="@drawable/mmrdf"
   />
</LinearLayout>
abbasalim
  • 3,118
  • 2
  • 23
  • 45

12 Answers12

625

In your adapter where you are inflating the item in onCreateViewHolder, is the second parameter of the inflate call null?.

If so change it to parent which is the first parameter in the onCreateViewHolder function signature.

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);

If you need the second parameter to be null then when you get the view reference on inflating, do the following

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);
SANAT
  • 8,489
  • 55
  • 66
prats110892
  • 6,599
  • 1
  • 12
  • 6
  • 2
    Doesn't work for me: if I set my rootView of the item to width="match_parent" but it "works" if I set it to 500dp for example. Any idea ? OK, it doesn't work with a LinearLayout but works with a relativeLayout on the item... wtf ? – Cocorico Aug 03 '16 at 15:08
  • @Cocorico can you provide more information.. a snippet of the code? – prats110892 Aug 04 '16 at 19:27
  • 6
    Sry, As it works with relativeLayout, I just change LinearLayout to RelativeLayout. But RecyclerView was match_parent, and item rootView (so the LinearLayout) was Match_parent too and it didn't work. First time I faced this. – Cocorico Aug 05 '16 at 09:03
  • @Cocorico I am not sure.. I would have to look at the xml of your rootView. It does seem weird that it works with RelativeLayout and not LinearLayout.. My first thoughts is that it might have something to do with the orientation of the LinearLayout.. but I am not sure.. If you paste your rootview xml I might be able to help more – prats110892 Aug 05 '16 at 16:22
  • 4
    Yeah as previously mentioned for some reason this didn't work with a LinearLayout and switching it to a RelativeLayout with this fix seemed to do the trick. – Nick Peppers Nov 30 '16 at 17:12
  • 14
    This does not work with Linearlayout or Constrainlayout. I had to switch to Relativelayout and then it worked – ymerdrengene Apr 18 '17 at 13:39
  • You should put `RecyclerView` into `RelativeLayout` instead of `Linearlayout`. It looks like a bug in Support lib. – Konstantin Konopko Jul 17 '18 at 10:40
  • 1
    Still not working for me. RecyclerView is in Relative Layout and item is in Relative layout. I have tried both of them. It stopped working. – Cyph3rCod3r Jul 23 '18 at 12:33
  • 1
    And this applies to the usage of Binding to inflate views into ViewHolder as well. `Binding.inflate(inflater,viewGroup,false)` fixed it for me. Thanks! – sud007 Apr 12 '20 at 05:57
  • 1
    If you are using data binding you can add those parameters when you inflate from the binding in onCreateViewHolder, such as ``val binding = ItemViewBinding.inflate(inflater, parent, false)`` – Tammytee Jun 14 '20 at 12:39
  • ymerdrengene, three times haha, also LinearLayoutCompat is here and also didnt get it working, see @scorpiodawg 's answer! – CodeToLife Mar 06 '21 at 23:07
  • val binding = bindingobject.inflate(LayoutInflater.from(parent.context),parent,false) in kotlin above is working with my constraintlayout – varotariya vajsi Oct 12 '21 at 09:58
  • This answer saves me everytime – Filipe Oliveira Jun 18 '23 at 21:28
  • Although, i made use of databinding and it worked perfectly even with LinearLayout – chisom emmanuel Jun 22 '23 at 12:39
27

Inside onCreateViewHolder(...) method of adapter where you are inflating the view.. you have to define the ViewGroup as the parent.This you will get from the 1st parameter of onCreateViewHolder(...) method.

see the below line in the second parameter i'm passing the ViewGroup. This will automatically match the view to its parent:

rowView=inflater.inflate(R.layout.home_custom_list, parent,false);

///the complete code is below

public View onCreateViewHolder(ViewGroup parent, int position) {
    // TODO Auto-generated method stub
    View rowView;
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
HAXM
  • 3,578
  • 4
  • 31
  • 38
14

I was using a FrameLayout with MATCH_PARENT for width and was seeing the same behavior with a RecyclerView + LinearLayoutManager. None of the above changes worked for me until I did the following in the onCreateViewHolder callback:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.note_layout, parent, false);
    v.setLayoutParams(new RecyclerView.LayoutParams(
          ((RecyclerView) parent).getLayoutManager().getWidth(),
          context.getResources()
                 .getDimensionPixelSize(R.dimen.note_item_height)));

    return new ViewHolder(v);
}

Clearly looks like a bug in (I'm guessing) the RecyclerView implementation.

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
  • 4
    Overriding layoutParams was the only solution that helped me. Anyone knows if this is a bug in RecyclerView? Would be grateful for a link to Google bug tracker. – soshial Dec 19 '19 at 11:20
6

I had done fix like this. In my case problem with activity layout file because i am using ConstraintLayout as root activity layout.Might be case for you too.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolBar"
        layout="@layout/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/accent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolBar" />
</android.support.constraint.ConstraintLayout>
Pankaj Kant Patel
  • 2,050
  • 21
  • 27
  • 2
    It's hard to believe, but just replacing my surrounding ConstraintLayout with a LinearLayout solved this for me too... – fjc Jan 29 '18 at 17:10
  • 1
    match_parent should not be used for direct children of ConstraintLayout. Instead, use 0dp with constraintLeft / Right / Bottom / Top attributes. – DoruAdryan Jun 26 '18 at 07:37
6

This worked for me.

replace this

   View view = View.inflate(parent.getContext(), R.layout.row_timeline, null);
   return new TimeLineViewHolder(view, viewType);

by this

 View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.setLayoutParams(lp);
        return new TimeLineViewHolder(rootView, viewType);
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
5

try this when you set layout params for your item in adapter.

 View viewHolder= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item, parent, false);
 viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
 ViewOffersHolder viewOffersHolder = new ViewOffersHolder(viewHolder);
 return viewOffersHolder;
Zahra.HY
  • 1,684
  • 1
  • 15
  • 25
5

In my case, the problem was in RecyclerView XML declaration, the layout_width was 0dp which means match_constraints, when I changed it to match_parent, items started to fill all RecyclerView width:

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="0dp"  <-- changed this to "match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="45dp"
            android:background="@android:color/transparent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_default="wrap"
            app:layout_constraintHeight_max="360dp"
            app:layout_constraintHeight_min="60dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/header"/>
chargerstriker
  • 486
  • 1
  • 5
  • 20
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
5

Simply adding a dummy View with 0 height and full width in root worked for me.

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".screen.gallery.GalleryFragment">

        <!----DUMMY VIEW----->
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            />

      
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/navigationList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
          />
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • Check what OP is looknig for then post your answer! The problem is not with the recyclerview. It is with the item of it! – Kishan Solanki Feb 02 '22 at 12:06
  • Thanks! I do hate that this is the only solution that I could get to work. In my case it was a constraint layout I had to put the dummy view in. – landnbloc Mar 08 '22 at 16:12
3

I was facing the same problem when using Linear layout and constraint layout for my compound view used in recycler view, but it worked when I change to relative layout for those compound views.

Pravin Desai
  • 527
  • 5
  • 18
  • 1
    After removing anything non-answering, this seems to not provide any additional insight. If you cannot [edit] towards that and according to [answer] please delete this. – Yunnosch Jan 17 '22 at 13:25
  • sorry if it is not clear enough, but let this answer stay, I wasted a lot of time finding a solution for this problem, this might help someone in the future. – Pravin Desai Jan 18 '22 at 04:36
  • The only solution detail you mention is "relative layout". That has been mentioned in other answers already. Sorry that it took you so long, but mentioning only that does not provide anything new here. It would be fine if you can go into detail about your solution and make sure that it actually is a solution for the problem described in the question at the top of this pave. As is, this post does not add anything. Please [edit] to improve the helpfulness of this. – Yunnosch Jan 18 '22 at 07:10
  • 2
    This solution works fine in my case – Hossam Ali Feb 15 '22 at 07:05
  • 1
    @PravinDesai just wasted 1 hour looking for this stupid error! Thank you for this handy solution. Don't know why am I still developing for android man! – MBH Jan 10 '23 at 17:53
2

I solved this with:

 myInflatedRowLayout.getLayoutParams().width = vg.getWidth();

It is replacing the MATCH_PARENT with the actual width of the RecyclerView.

Amir Uval
  • 14,425
  • 4
  • 50
  • 74
1

I've been stuck with this problem for a while, and the solution that worked for me was placing 2 views with match_parent, one inside the other.

If I did this on my list item's layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#F00"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

</RelativeLayout>

although it is a relative layout as others have mentioned, and I am passing the parent view but false in the inflater, it would simply not show up at all (red background to check).

But, when I did this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:background="#0F0"
        android:layout_height="match_parent"/>

</RelativeLayout>

The green layout showed up and took up the whole space.

So just having a child view inside the main one with match_parent solves the problem, no idea why.

Matheus Valin
  • 191
  • 2
  • 8
0

Can't see your full code, but can guess, that some of the views inside your LinearLayout are 'wrap_content'. You need to make one or some of them expand to the full width by using 'android:layout_weight="1"'

update: you have a lot of redundant layout_weight's. Make them all 'wrap_content' and for only one of them add layout_weight=1 - for the last CustomTextView. This way, it will occupy all the blank space.

khusrav
  • 5,267
  • 5
  • 27
  • 38