Yuriy Seredyuk
Yuriy Seredyuk

Reputation: 1673

Set ImageView's width to fill white space (keeping aspect ratio) inside RecyclerView with GridLayoutManager

I have RecyclerView with GridLayoutManger

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.movie_posters_recycler_view);
recyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));

Each RecyclerView's item represented by following layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/movie_item_poster_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />

</FrameLayout>

Currently I have such result

layout1

I want to do this

layout2

Upvotes: 4

Views: 2654

Answers (5)

am5a03
am5a03

Reputation: 587

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/movie_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>
</FrameLayout>

This should work

Upvotes: 4

AhmedAbdelaal
AhmedAbdelaal

Reputation: 286

Using android:scaleType="fitXY" in ImageView attributes should stretch the image inside the ImageView.

Upvotes: 0

Rafa0809
Rafa0809

Reputation: 1752

Take a look at this answer: https://stackoverflow.com/a/26575808/4729523.

You just need to keep the image's aspect ratio (3/4 or 9/16).

Upvotes: 0

Sayam Abbas
Sayam Abbas

Reputation: 1

please add to scaltype to image view in your adapter to fitxy

Upvotes: 0

Atiq
Atiq

Reputation: 14825

Replace your image view with this one

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/movie_item_poster_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

Upvotes: 0

Related Questions