2

I am following this tutorial, it is very simple, and yet my GridView images are much much smaller than the one in the tutorial. Does anything stand out as wrong code? Below I've included my main activity, my adapter class, and my xml with the GridView.

It should be like this:

enter image description here

Instead, mine is like this:

enter image description here

SitesActivity.java

package org.azurespot.cutelinks;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

import org.azurespot.R;

public class SitesActivity extends ActionBarActivity {

    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sites);

        // with fragments, make sure you include the rootView when finding id
        gridView = (GridView) findViewById(R.id.sites_grid);
        // Set the Adapter to GridView
        gridView.setAdapter(new GridViewSitesAdapter(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sites, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

GridViewSitesAdapter.java

package org.azurespot.cutelinks;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import org.azurespot.R;

/**
 * Created by mizu on 2/11/15.
 */
public class GridViewSitesAdapter extends BaseAdapter {

    public Context mContext;

    public GridViewSitesAdapter(Context c) {
        mContext = c;
    }

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.cute_overload, R.drawable.attack_of_the_cute,
            R.drawable.zoo_borns, R.drawable.cutest_paw,
            R.drawable.mochimochiland, R.drawable.baby_mugging,
            R.drawable.cutest_food, R.drawable.tiny_cute_things,
            R.drawable.etsy_robot_plush
    };

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;

    }
}

activity_sites.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.azurespot.cutelinks.SitesActivity"
    android:background="#2198bb">


    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/sites_grid"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:layout_margin="10dp"
        android:columnWidth="90dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:background="@drawable/button_border">

        </GridView>

</RelativeLayout>
Azurespot
  • 3,066
  • 3
  • 45
  • 73
  • 4
    Are your images relatively small? Try changing `imageView.setLayoutParams(new GridView.LayoutParams(70, 70));` to `imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));` and see if it fixes anything. – iRuth Feb 14 '15 at 02:59
  • Thanks iRuth, my images are bigger than they even need to be. Although I tried that code, but getting red error on the `LayoutParams inside the parenthesis. Any idea why? – Azurespot Feb 14 '15 at 03:15
  • 2
    Actually thanks iRuth, I tried to increase the 70 number and they are getting bigger. Thanks for narrowing my focus to that point! I'm not used to programmatically writing my xml, so this is a good lesson for that. – Azurespot Feb 14 '15 at 03:20

1 Answers1

1

Why don't you just calculate the width of gridview item based on the screenwidth

columnWidth = (int) ((getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

Hope this sample line of code will help you understand. Let me know if you need any help on it.

Thanks :)

Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Thanks Fahim! Nice of you to post. I did manage to figure it out by altering the image number in the java class. Looks like the tutorial I was following did half and half of programmatically and `xml`. – Azurespot Feb 14 '15 at 05:27
  • 1
    Solution I gave u will work on all kind of resolution phones and tabs. Where just changing no of columns won't work – Fahim Feb 14 '15 at 05:33