Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView item height and width change dynamically Android

Tags:

I am new to android development and I am developing an app where the app downloads pictures from server and show them in a gridView. But some extra spaces are coming up when the app is run on a bigger screen phone. Here are some screenshots. How do I solve this ? I have the model of each item with a fixed height and width of 110dp

My image model :

<?xml version="1.0" encoding="utf-8"?> <ImageView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="110dp"     android:layout_height="110dp"     app:srcCompat="@drawable/post2"     android:id="@+id/imageView2"     android:adjustViewBounds="true"     android:scaleType="centerCrop"     android:layout_margin="1.5dp" /> 

My Recycler View :

<android.support.v7.widget.RecyclerView                 android:id="@+id/gallerygrid"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_marginTop="4dp"                 android:numColumns="auto_fit"                 android:stretchMode="columnWidth"                 android:visibility="visible"                 android:layout_gravity="center">              </android.support.v7.widget.RecyclerView> 

On smaller screen phone

On bigger screen phone

like image 510
Shakil Mahmud Shanto Avatar asked Mar 15 '17 11:03

Shakil Mahmud Shanto


People also ask

How to set RecyclerView height dynamically in android?

You write params. height = itemHeight * numberOfItem; When you know number of items you can set height of recycleviewer. But you probably want fixed height of item a set height of recycleviewer.

How to get RecyclerView item height in android?

With the addOnGlobalLayoutListener() method, the height value is obtained before the TextView is drawn. And then save it in a member variable. The key is to modify the UI inside and outside the implementation of the listener so that there are no rendering problems (when the views are redrawn).

What is a RecyclerView adapter?

RecyclerView. Adapter base class for presenting List data in a RecyclerView , including computing diffs between Lists on a background thread. Base class for an Adapter. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView .

When to use Recycler View?

Android recyclerview is the most advanced version of the listview. basically, an android listview is used to present a simple data set. if you want to display large data set in your app, you should use recyclerview.


1 Answers

Set height and width dynamically according to the screen size. this is the best way to get perfect result.

DisplayMetrics displaymetrics = new DisplayMetrics();         ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);         //if you need three fix imageview in width         int devicewidth = displaymetrics.widthPixels / 3;          //if you need 4-5-6 anything fix imageview in height         int deviceheight = displaymetrics.heightPixels / 4;          holder.image_view.getLayoutParams().width = devicewidth;          //if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height         holder.image_view.getLayoutParams().height = deviceheight; 

try this in adapter in getview hope you get better result in all device

EDIT: Or to set same height same as width you can set devicewidth in height also like below:

holder.image_view.getLayoutParams().height = devicewidth; 
like image 143
Zaki Pathan Avatar answered Sep 19 '22 07:09

Zaki Pathan