Tuesday, October 25, 2016

android - GridView row height

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#7a9e74"
    >
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:layout_margin="10dp"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:background="#ca147f"
        android:padding="2dp"
        />
</android.support.design.widget.CoordinatorLayout>
res/drawable/gridview_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#FFF21998"
                android:endColor="#fc3aab"
                android:angle="90"
                />
            <stroke android:width="1dp" android:color="#ca147f"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private GridView mGridView;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mGridView = (GridView) findViewById(R.id.grid_view);

        // Initialize a new list of flowers
        List<String> flowers = new ArrayList<>();
        flowers.add("Hosta");
        flowers.add("Hyacinth");
        flowers.add("Hydrangea");
        flowers.add("Hypericum");
        flowers.add("Ipomoea alba");
        flowers.add("Jaborosa");
        flowers.add("Jasmine");
        flowers.add("Kolkwitzia");
        flowers.add("Lantana");
        flowers.add("Lavender");

        // Initialize a new array adapter instance
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                mContext,
                android.R.layout.simple_list_item_1,flowers
        ){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Cast the grid view current item as a text view
                TextView tv_cell = (TextView) super.getView(position,convertView,parent);

                // Set the item background drawable
                tv_cell.setBackground(
                        ContextCompat.getDrawable(
                                mContext,R.drawable.gridview_item_background
                        )
                );

                // Put item item text in cell center
                tv_cell.setGravity(Gravity.CENTER);

                /*
                    void setHeight (int pixels)
                        Makes the TextView exactly this many pixels tall. You could do the same
                        thing by specifying this number in the LayoutParams. Note that
                        setting this value overrides any other
                        (minimum / maximum) number of lines or height setting.

                        Parameters
                            pixels : int
                */

                // Set the grid view item/cell/row height
                //tv_cell.setHeight(300); // In pixels

                // Another way to change grid view row height
                tv_cell.getLayoutParams().height = 300;

                // Return the modified item
                return tv_cell;
            }
        };

        // Data bind the grid view with array adapter
        mGridView.setAdapter(adapter);
    }
}