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>
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.
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).
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 .
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With