Home » Android » layout – Fit recyclerview items on android screen

layout – Fit recyclerview items on android screen

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m in trouble! I have this RecyclerView where I use a GridLayoutManager to achieve two columns and several rows.
But here goes my issue:
I have at most 8 items in this RecyclerView, and I would like to fit them according to screen size

So far I’ve got this:

enter image description here

using this piece of code:

        Rect rectangle = new Rect();
        Window window = ((Activity)context).getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
        int statusBarHeight = rectangle.top;
        int contentViewTop =
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int titleBarHeight= contentViewTop - statusBarHeight;

        final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
        int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();

        int softButtonsHeight = 0;

        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

        DisplayMetrics realMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            ((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics);

            if(realMetrics.heightPixels > metrics.heightPixels){
                softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels;
            }
        }

        ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe);

        float logoHeight = 0;
        //convertendo na mão tamanho do sponsor
        if(img_Logo.getVisibility() != GONE) {
            logoHeight = 100 * context.getResources().getDisplayMetrics().density;
        }

        double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight) / Math.round(list.size() / 2D);

        itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx);
        rvOptions.setAdapter(itensAdapter);

and inside OptionItensAdapter constructor at my onBindViewHolder:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx);
        holder.imageButton.setLayoutParams(params);

Do you have any idea that would make me achieve this?
Thanks in advance.

How to&Answers:

Exact solution for your problem is newer Flexible layout manager for reyclerView

More about Flexible layout

Example

Answer:

Have a look at this OnBindViewHolder code and change it as per your requirement 😀

 @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        final int pos = position;
        try {
//
            Resources r = activity.getResources();
            int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()); // i have bottom tabbar so yf you dont have any thing like this just leave 150 to 0.I think in your case height of image view an your top(Pifer)
            //this change height of rcv
            DisplayMetrics displaymetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int height = displaymetrics.heightPixels;
            int width = displaymetrics.widthPixels;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = (height - px) / 5; //height recycleviewer (there are 5 rows so divide by 5 but i think in your case there are 4 rows so divide by 4)
            viewHolder.itemView.setLayoutParams(params);
            viewHolder.nameTxt.setText(totalList.get(position).getName());
            viewHolder.icon.setImageResource(totalList.get(position).getIcon());
//           viewHolder.background.setBackground(ContextCompat.getDrawable(context, totalList.get(position).getBackground()));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

Just posting this viewHolder to see what all items.

public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView nameTxt;
        public RelativeLayout background;
        public ImageView icon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            nameTxt = (TextView) itemLayoutView.findViewById(R.id.menu_label);
            background = (RelativeLayout) itemLayoutView.findViewById(R.id.menu_background);
            icon = (ImageView) itemLayoutView.findViewById(R.id.menu_icon);
        }

Answer:

My proposal is to use a layout similar to this instead of RecyclerView, it fits on any screen. The layout will do all needed adjust by itself without any code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:src="@android:drawable/sym_def_app_icon" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:src="@android:drawable/sym_def_app_icon" />
    </LinearLayout>


</LinearLayout>

enter image description here

Answer:

A GridLayout or Constraint layout are much better choices here.

A RecyclerView is (as it’s name suggests) for recycling – you should use one when you have a lot of views/children and need to make sure only the few on screen are using memory.

A ConstraintLayout will instead allow you to include each view separately and define how they relate to each other to create the grid pattern.

A GridLayout like my example below will arrange the items for you, without recycling.

<GridLayout android:id="@+id/..."
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal".
    android:orientation="horizontal"
    android:columnCount="2"
    android:rowCount="4">

    <OptionItem ...
        android:weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <OptionItem ...
        android:weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <OptionItem ...
        android:weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    ...

</GridLayout>

In your code you then change visibility of any of your 8 buttons you want hidden

button8.setVisibility(View.INVISIBLE); //don't use GONE inside the grid

If you want to programmatically set the item widths (or heights), set useDefaultMargins="true" and change the layout params (as per this answer)

GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin;
child.setLayoutParams(params);

Answer:

I had just answered a similar question like this in this SO answer link

Basically, get screensize, then adjust your height accordingly, so the gist of it is :

DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

if(position == 0) {
    holder.itemView.getLayoutParams().width = displayMetrics.width;
    holder.itemView.getLayoutParams().height = displayMetrics.height / 8;
} else {
    holder.itemView.getLayoutParams().width = displayMetrics.width / 2;
    holder.itemView.getLayoutParams().height = displayMetrics.height / 8;
}

Answer:

Add custom grid row and set size there and then set autofit that will automatically adjust as per screen

Answer:

Why are you using RecyclerView for this??

GridLayout is the best option here if you have fixed number of items. You can play with weights of object.

Here is an example showing how to fit 6 LinearLayouts to a screen

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.waqasansari.hitwithme.main.fragments.Dashboard">


    <LinearLayout
        android:id="@+id/myMatches"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="0"
        app:layout_row="0"
        android:background="@drawable/border_gray"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_my_matches"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Matches"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/requestMatches"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="1"
        app:layout_row="0"
        android:background="@drawable/border_gray"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_match_requests"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Match Requests"/>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/proShop"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="0"
        app:layout_row="1"
        android:background="@drawable/border_gray"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_pro_shop"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pro Shops"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/rankings"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="1"
        app:layout_row="1"
        android:background="@drawable/border_gray"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_rankings"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rankings"/>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/courtsAndCoaches"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="0"
        app:layout_row="2"
        android:background="@drawable/border_gray"
        android:gravity="center"
        android:orientation="vertical">


        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_courts_coaches"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Courts &amp; Coaches"/>


    </LinearLayout>


    <LinearLayout
        android:id="@+id/inviteFriends"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_column="1"
        app:layout_row="2"
        android:background="@drawable/border_gray"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/dashboard_invite_friends"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Invite Friends"/>

    </LinearLayout>


</android.support.v7.widget.GridLayout>

You can add more items in a similar fashion

Answer:

If your menu are not dynamically changes, i.e you have the menu settings on API, then you don’t have to use Recyclerview or GridView to populate this layout. I prever to combine LinearLayout(s) with some constraints to populate static layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/content_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:weightSum="1">

    <ImageView
        android:layout_weight="0.8"
        android:src="@drawable/logo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_weight="0.2"
        android:weightSum="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:weightSum="1"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:weightSum="1"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:weightSum="1"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:weightSum="1"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="0.5"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/free2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="this is text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

This is the Result:

enter image description here

Answer:

If you have fix 8 items then you can use LinearLayout and SDP library for icon size like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="android.com.linearlayouthome.MainActivity">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="@dimen/_60sdp"
        android:layout_height="@dimen/_60sdp"
        android:layout_gravity="center"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <LinearLayout
                android:gravity="center"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp" />

                <TextView
                    android:gravity="center_horizontal"
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Using SDP library you dont need to write dimension file for different screen size

Screenshots :
Nexus 4 :

enter image description here

Nexus 5X :

enter image description here

Nexus 6 :

enter image description here

Answer:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater
            .from(parent.getContext())
            .inflate(R.layout.item_list, null);

    int height = parent.getMeasuredHeight() / 4;
    int width = parent.getMeasuredWidth();

    view.setLayoutParams(new RecyclerView.LayoutParams(width, height));

    return new ViewHolder(view);
}

Answer:

If you need to fix views to Screen than you don’t need to take recyclerView. You can play with Weight and make items fit to screen.

In your scenario you can follow below code

//llContainer main layout in which you want to put 8 values having orientation vertical
llContainer.setWeightSum(numberofRaws); // It will be 4 if you want to put 8 values

for(int i=1; i<=numberofRaws ; i++ ){
    //Inflate One LinearLayout which has height width Match Parent
    LinearLayout llRaw = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_plain_with_horizontal_orientation, null);
    llRaw.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARRENT, 1.0f));
    AddTwoViewForFaw(llRaw);

    llContainer.AddView(llRaw);


}


public void AddTwoViewForRaw(LinearLayout llRaw){

    View v1 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null);
    // Here you can set values for grid layout by v1.findViewbyId()
    v1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
    llRaw.addView(v1);


    View v2 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null);
    // Here you can set values for grid layout by v2.findViewbyId()
    v2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
    llRaw.addView(v2);
}

Hope It will work for you.