JasperMoneyshot
JasperMoneyshot

Reputation: 357

Change row height in gridview

I'm new to Android development and making my first app, it's a wordsearch app for a project and I am using a GridView to display the letters for the search. I was able to change the width of the cells in the grid using the layout xml's, however I can't change the height and at the moment it's far too much.

I tried to Google answers on how to do this but everything I found said about overriding getView in a custom arrayAdapter, and I don't really follow how to do that or how to set the height to a fixed amount when doing it. I have the row width set to 25dp in the layout files and want to change the height to match that, but I can't seem to find any easy to follow way of doing it online.

Cheers in advance if you can help.

Edit: Here is where I fill the grids:

//fill GridView with the puzzle input array from the puzzle class
ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray);
wordsearchGrid.setAdapter(gridAdapter);

//fill ListView with the searchWords array from the puzzle class
ArrayAdapter<String> wordsAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.searchWords);
wordsList.setAdapter(wordsAdapter);

Here is the layout for the gridView (I'm using two, one for the wordsearch and one to store the search words:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
tools:context="little.keith.wordsearch.TodaysActivity" >

<GridView
    android:id="@+id/wordsearchGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_margin="4dp"
    android:background="@android:color/black"
    android:columnWidth="25dp"
    android:gravity="top"
    android:horizontalSpacing="2dp"
    android:numColumns="12"
    android:stretchMode="none"
    android:verticalSpacing="2dp" >
</GridView>

<GridView
    android:id="@+id/wordsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:background="@android:color/black"
    android:numColumns="auto_fit"
    android:layout_below="@+id/wordsearchGrid"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"
    android:layout_centerHorizontal="true" >
</GridView>

And here is the cell_layout.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

Upvotes: 2

Views: 6953

Answers (1)

JasperMoneyshot
JasperMoneyshot

Reputation: 357

Managed to figure out how to do this myself, just sticking the answer up in case anyone else gets stuck on the same problem. To fix the row heights I wrote a custom ArrayAdapter which allowed me to define the cell dimensions with setLayoutParams:

public class GridAdapter extends ArrayAdapter
{
    Context context;
    Object[] items;
    int resource;

    LayoutInflater inflater;

    public GridAdapter(Context context, int resource, Object[] items) 
    {
        super(context, resource, items);
        this.context = context;
        this.resource = resource;
        this.items = items;

        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

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

        int cellWidth = 40;
        int cellHeight = 50;


        if (convertView == null) 
        {
            // If convertView is null then inflate the appropriate layout file
            convertView = inflater.inflate(resource, null);
        }

        cell = (TextView) convertView.findViewById(R.id.gridCell);

        cell.setText((CharSequence) items[position]);

        // Set height and width constraints for the image view
        cell.setLayoutParams(new LinearLayout.LayoutParams(cellWidth, cellHeight));

        // Set Padding for images
        cell.setPadding(1, 1, 1, 1);

        return convertView;
    }
}

Then just called it in the Activity:

//fill GridView with the puzzle input array from the puzzle class
GridAdapter gridAdapter = new GridAdapter(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray);
wordsearchGrid.setAdapter(gridAdapter);

Upvotes: 3

Related Questions