CardView layout_width=match_parent does not match parent RecyclerView width
The docs for inflate
:
Inflate a new view hierarchy from the specified xml resource. Throws
InflateException if there is an error.Parameters
resource ID for an XML layout resource to load (e.g.,
R.layout.main_page) root
view to be the parent of the
generated hierarchy (if attachToRoot is true), or else simply an
object that provides a set of LayoutParams values for root of the
returned hierarchy (if attachToRoot is false.)
attachToRoot Whether
the inflated hierarchy should be attached to the root parameter? If
false, root is only used to create the correct subclass of
LayoutParams for the root view in the XML. Returns The root View of
the inflated hierarchy. If root was supplied and attachToRoot is true,
this is root; otherwise it is the root of the inflated XML file.
It is important here to not supply true, but do supply the parent:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);
Supplying the parent
View lets the inflater know what layoutparams to use. Supplying the false
parameter tells it to not attach it to the parent just yet. That is what the RecyclerView
will do for you.
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);
RecyclerView item width layout_width=“match_parent” does not match parent
I fixed the problem:
1- Get the screen size (width).
2- make the ViewHolder width same as screen size.
Below my code if any one need it.
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight();
view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));
CardView width is not matching to width of the screen?
The problem lies with your inflater. Check out the documentation for LayoutInflater, specifically for the inflate method.
Try this:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.exercises_card_view_layout, parent, false);
Instead of supplying null, supply the parent view. This way the inflater knows what LayoutParameters to use. Supplying false parameter tells the LayoutInflater not to attach it to the parent just yet. The RecyclerView will handle this for you.
Unable to make RecyclerView Width as match parent
I found it:
All I had to change the following line of code in my custom adapter as follows:
View view = inflater.inflate(R.layout.record_logs_card, null);
to
View view = inflater.inflate(R.layout.record_logs_card, parent, false);`
Related Topics
Hiding the Actionbar on Recyclerview/Listview Onscroll
Android - Extracting Cookies After Login in Webview
Phonegap/Cordova Android Development
Answer Incoming Call Using Android.Telecom and Incallservice
Full Screen Background Image in an Activity
Android Volley + JSONobjectrequest Caching
How to Force an Entire Layout View Refresh
Is Using Serializable in Android Bad
How to Animate a View with Translate Animation in Android
How to Create a Relativelayout Programmatically with Two Buttons One on Top of the Other
Android O - Old Start Foreground Service Still Working
How to Share a Sharedpreferences File Across Two Different Android Apps
Passing String Array Between Two Class in Android Application