I followed the steps in the accepted answer (from @kcoppock) in the following thread. https://stackoverflow.com/a/15264039/3296263 or Gridview with two columns and auto resized images
Edit: Here is the code. Gridview code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/main_category_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>
</FrameLayout>
Grid Item Layout: main_category_list_content.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.praz.notespal.model.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
android:background="#55000000"/>
</FrameLayout>
Custom SquareImageView Class:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
Adapter Code:
private final class MyAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mItems.add(new Item("Red", R.drawable.tile_alevel));
mItems.add(new Item("Magenta", R.drawable.tile_grade10));
mItems.add(new Item("Dark Gray", R.drawable.tile_grade11));
mItems.add(new Item("Gray", R.drawable.tile_grade9));
mItems.add(new Item("Green", R.drawable.tile_alevel));
mItems.add(new Item("Cyan", R.drawable.tile_grade11));
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
System.out.println("viewing "+ i);
if (v == null) {
v = mInflater.inflate(R.layout.main_category_list_content, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item {
public final String name;
public final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
Activity Code where i set the adapter
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView)findViewById(R.id.main_category_list);
gridView.setAdapter(new MyAdapter(this));
}
Instead of showing all 6 items, i only see 2. If i increase the 'numColumns' property to 3, then it shows 3. So it only shows the first row. I placed a console out inside the getView() method in the adapter to see the position of the grid that is being called. To my surprise, what i see is the below output.
I/System.out: viewing item 0
I/System.out: viewing item 1
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0
As you can see, it repeats 0th position instead of moving to 3rd. After alot of searching I still have not been able to figure out what is going on. Any help is highly appreciated.
Please note that i have created a new question, because i cannot reply to his answer because of reputation points shortage.