Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set recycler view item width size base on screen size?

I want my RecyclerView item width and height equal and change it's size dynamic for screen size?

how can I do this any advice or sample code please?

Here my code:

    mrv = findViewById(R.id.rv_segments);
    list = new ArrayList<>();
    mAdapter = new SegmentsAdapter(this, list,station.getWidthCount());

    //mAdapter.setOnItemClickListener(this);

    mLayoutManager = new LinearLayoutManager(getApplicationContext());
    mLayoutManager = new GridLayoutManager(getActivity(), station.getWidthCount());


    mrv.setLayoutManager(mLayoutManager);
    mrv.setAdapter(mAdapter);

and here is my adapter:

public SegmentsAdapter.BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view1 = LayoutInflater.from(context).inflate(R.layout.recyler_item_segments,parent,false);
    BaseHolder viewHolder1 = new BaseHolder(view1);
    return viewHolder1;
}

And My recycler item xml like :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_ln_border"
    android:padding="2dp">
<LinearLayout
    android:id="@+id/ln"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="centerCrop"
    android:orientation="horizontal"
    android:background="@color/menu3"
    />
</LinearLayout>
like image 499
tsniso Avatar asked Mar 30 '19 10:03

tsniso


People also ask

What if recyclerview have only 30% of screen width and 40% height?

will not works if RecyclerView contains NOT whole screen. What if RecyclerView have only 30% of screen width and 40% of screen height (for sample)? for those who want to do it in the xml layout (recyclerView item) file you can use a ConstraintLayout by combining and don't forget to provide all the constraint that way you can set

How to set the random background for recyclerview?

This example demonstrates How to set the random background for Recyclerview by creating a beautiful student records app that displays student name with age. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to add recycler View Library in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Open build.gradle and add Recycler view library dependency.

How to draw top margin in recyclerview?

Use decorator item. Add the below class below. There’s a logic that draw margin on left, right and bottom. The top margin would only be drawn for the first cell. Now you have your custom decorator, then add it to your recyclerView as below. You would do it when you setup your recyclerView layout manager etc. That’s it. All done.


2 Answers

Your Recyclerview should take a LayoutManager with Column Span size Specified in it. which is something like this:

mList.setLayoutManager(new GridLayoutManager(getContext(), 2));

So now you just need to manage Height of the Items. right? This will do:

@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View myView = inflater.inflate(R.layout.item_home, parent, false);
  GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) myView.getLayoutParams();
  params.height = (parent.getMeasuredHeight() / 2) - dim24;
  myView.setLayoutParams(params);
  return new myViewHolder(myView);
}

So. this (parent.getMeasuredHeight() / 2) - dim24 is where you can adjust your view "HEIGHT"

And dim24 is just a padding Integer, you can get rid of it

Notice I used "2" Columns so I devided item Heights by "2"

like image 200
Shahin Avatar answered Oct 17 '22 10:10

Shahin


Pls Use ConstraintLayout inside recyclerview item .. Constraints are updated and Better than Relative and Linear Layouts.. Constraint Layout will automatically adjusts based on Screens...

like image 1
satyan_android Avatar answered Oct 17 '22 09:10

satyan_android