Zapnologica
Zapnologica

Reputation: 22566

Android GridView make images fill screen

How can I make my images in the grid view fill the screen?

Here is my layout code:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:padding="10dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >
</GridView>

I will post a picture of the screen .

But it is displaying the images small and not using the entire screen.

Here is my adapter class:

public class MenuCategoryAdapter extends BaseAdapter {

    private GlobalObjects global;
    private Context mContext;

    // Keep all Objects in array
    public ArrayList<MenuCategory> categories = new ArrayList<MenuCategory>();

    // Constructor
    public MenuCategoryAdapter(Context c, GlobalObjects _global) {
        mContext = c;
        global = _global;

        JSONArray categoriesObj = getCategoriesFromServer();
        if (categoriesObj != null) {
            putCategoriesIntoArray(categoriesObj);
        } else {
            // Categories is null
            Log.e("ImageAdapter", " Categories is null");
        }
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return categories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = categories.get(position).getImg();
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }
}

Upvotes: 0

Views: 2362

Answers (1)

Neha Patel
Neha Patel

Reputation: 24

here is grid view it will fill your screen. it works for me do it. if anything happens wrong inform me immediate.

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >

    </GridView>

Also You need To Edit In Your java Code. Make scale from 70,70 to fill_parant please.

I Have also sample app for it. if you require it please tell me i will upload it for you.

Upvotes: 1

Related Questions