Tuesday, October 25, 2016

android - Highlight GridView selected item

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="#2a3b8c"
    >
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:listSelector="@drawable/states_gridview"
        android:layout_margin="25dp"
        />
</android.support.design.widget.CoordinatorLayout>
res/drawable/grid_view_item_selected.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="#3c77d6"
                android:endColor="#82b2ff"
                android:angle="90"
                />
            <stroke android:width="1dp" android:color="#16254b"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
</selector>
res/drawable/states_gridview.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- grid view item pressed state -->
    <item
        android:state_pressed="true"
        android:drawable="@android:color/holo_red_light"
        />
    <!-- grid view item selected state -->
    <item
        android:drawable="@drawable/grid_view_item_selected"
        />
</selector>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
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;


    @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 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("#FF108714"));
                snackbar.show();
           }
        });
    }
}