Sam
Sam

Reputation: 4284

Automatically adjusting size of cells in a gridview in android

Am developing a custom gallery using gridview in android. Now am facing the issue with the columns count ..my gridview xml is,

<GridView
    android:id="@+id/gv_gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:choiceMode="multipleChoice"
    android:stretchMode="columnWidth"
    android:gravity="center" 
    android:columnWidth="179dp">
</GridView>

my grid item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="179dp"
    android:layout_height="131dp"
    android:padding="1dp" >
    
    <ImageView
        android:id="@+id/iv_thumbnail"
        android:contentDescription="@string/iv_content_description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/tv_folder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:text="@string/tv_folder" 
        android:padding="12dp"
        android:textColor="@color/white"
        android:background="@color/transparent_black"/>

</RelativeLayout>

of course it's dynamically maintaining columns according to device width but there is different column spacing in different devices.. how i can overcome this issue.?

Upvotes: 1

Views: 1550

Answers (1)

AndroidEnthusiast
AndroidEnthusiast

Reputation: 6657

To distribute the size uniformly don't use a fixed size for each grid item.

Set the width to wrap_content and you can also specify the number of columns

Upvotes: 1

Related Questions