Title: Place three items of recyleview in the screen programmatically
Tags: android;android-recyclerview
Question: I want to set width and height of each item programmatically (textview + recyleview) so that I can place three ones no matter which device renders it (when devices differ I got different width and height because of density metrics). My code for doing this issue is as following:
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView playlistTitle;
protected Button playlistMoreButton;
protected RecyclerView playlist_recycler_view_list;
public ItemRowHolder(View view) {
super(view);
this.playlistTitle = (TextView) view.findViewById(R.id.playlist_title);
this.playlist_recycler_view_list = (RecyclerView) view.findViewById(R.id.playlist_recycler_view_list);
this.playlistTitle.measure(0,0);
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int screenHeight = (int )displayMetrics.heightPixels;
int titleHeight = this.playlistTitle.getMeasuredHeight();
int rcvHeight =  this.playlist_recycler_view_list.getLayoutParams().height;
int layoutHeight = 0;
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(5, 20, 5, 20);
view.requestLayout();
}
layoutHeight = (int)(screenHeight - displayMetrics.density* titleHeight-displayMetrics.density*120)/3;
Log.d("MOMOPix","ydpi: "+ displayMetrics.density);
Log.d("MOMOPix","Screen height: "+ screenHeight);
Log.d("MOMOPix","RCV height: "+ rcvHeight);
Log.d("MOMOPix","Title height: "+titleHeight);
Log.d("MOMOPix","Layout height: "+layoutHeight);
this.playlist_recycler_view_list.getLayoutParams().height=  layoutHeight;
layout is as following:
<?xml version="1.0" encoding="utf-8"?&gt;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical"&gt;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"&gt;
<TextView
android:id="@+id/playlist_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Sample title"
android:textColor="@android:color/white"
android:textSize="18dp" /&gt;
</RelativeLayout&gt;
<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler_view_list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" /&gt;
</LinearLayout&gt;
for some devices I get desired results and I want for all devices
Thanks in advances.


Here is another answer: You're doing it in very weird and error prone way.
If you want to alter how items are laid out, you should override RecyclerVIew.LayoutManager (which is specifically made to lay out the views as its name suggests) instead of performing weird measurement hacks in your ViewHolder.
To have RecyclerView fit three items vertically (I assume it's LinearLayoutManager, your code sample does not mention it) we only have to override one method in LayoutManager:
recyclerView.setLayoutManager(new LinearLayoutManager(context){
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force height of viewHolder to be a third of RecyclerView
// this will override layout_height from xml
lp.height = getHeight() /3;
return true;
}
});

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:06/22/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-recyclerview>', 'Title': 'Place three items of recyleview in the screen programmatically', 'ViewCount': 48, 'id': 50989133, 'user_AboutMe': '', 'user_AccountId': 9315059.0, 'user_DisplayName': 'Halil RAŞO', 'user_Reputation': 207.0, 'user_Views': 40.0, 'user_id': 6915641.0}}