To set the width of the items in a horizontal RecyclerView
to be half of the screen width in Android, you can use the RecyclerView.LayoutManager
and set the width of the item view to be match_parent
and the height to be wrap_content
.
Here's an example of how to do it:
First, create a layout file for the item view with the desired dimensions:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- other layout elements go here --> </LinearLayout>oSurce:www.lautturi.com
Then, in your RecyclerView.Adapter
, set the RecyclerView.LayoutManager
to be a LinearLayoutManager
with the horizontal orientation:
RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); recyclerView.setAdapter(new MyAdapter());
This will cause the items in the RecyclerView
to be displayed horizontally, with the width of each item being set to match_parent
(i.e., the full width of the screen).