match_parent width does not work in RecyclerView

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:

  • Modify class in jar: android build failed
  • Does Google Cloud Messaging (GCM) work with Kindle Fire?
  • What is the difference between a Layout and a Container in Android?
  • FragmentActivity Junit Testing
  • Change Application Package name through Android Studio
  • How to restrict the plugin to switch rear/back camera in android / cordova
  • <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>
    

    Related posts:

    How to start developing applications for Android?
    Game Development, strange image effect while moving camera
    Android ListView Universal Image Loader Changes Images on Scroll
    Change the color of Navigation Drawer indicator icon
    difference between layout-finger and layout directory
    Permission Denial: Accessing service ComponentInfo {&hellip;} from pid=-1
  • How do I get Google refund orders ?
  • Tricks to keep phone from sleeping in mobile web browser using JavaScript/HTML?
  • How to Draw TextView on Canvas in android..?
  • How to search a file with adb.exe?
  • How to Overlap items in LinearLayoutManager - RecyclerView (like stacking cards)
  • change Item text color of NavigationView
  • 5 Solutions collect form web for “match_parent width does not work in RecyclerView”

    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);
    

    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);
    

    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;
    

    I solved this with:

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

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.