Tuesday, October 25, 2016

android - GridView item click effect

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="#af9993"
    >
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;

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;
    private int mGridViewBGColor = Color.parseColor("#FF86A38B");


    @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
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(
                mContext,
                android.R.layout.simple_list_item_1,flowers
        );

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

        // Set a background color for GridView
        mGridView.setBackgroundColor(mGridViewBGColor);

        // Set an item click listener for grid view
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Get the selected item
                String selectedItem = adapterView.getItemAtPosition(i).toString();

                // Display the selected item text on snack bar
                Snackbar snackbar = Snackbar.make(
                        mCLayout,
                        "Selected : " + selectedItem,
                        Snackbar.LENGTH_LONG
                );
                snackbar.getView().setBackgroundColor(Color.parseColor("#FF66729B"));
                snackbar.show();

                // Initialize a new color drawable array
                ColorDrawable[] colors = {
                        new ColorDrawable(Color.RED), // Animation starting color
                        new ColorDrawable(mGridViewBGColor) // Animation ending color
                };

                // Initialize a new transition drawable instance
                TransitionDrawable transitionDrawable = new TransitionDrawable(colors);

                // Set the clicked item background
                view.setBackground(transitionDrawable);

                // Finally, Run the item background color animation
                // This is the grid view item click effect
                transitionDrawable.startTransition(600); // 600 Milliseconds
            }
        });
    }
}