Wednesday, October 26, 2016

android - GridView with image and text

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="#b4c9b1"
    >
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:layout_margin="10dp"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:background="#eaf5e4"
        android:padding="2dp"
        />
</android.support.design.widget.CoordinatorLayout>
res/layout/gridview_custom_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:padding="0dp"
    >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="4dp"
        card_view:cardMaxElevation="6dp"
        card_view:contentPadding="5dp"
        android:layout_margin="5dp"
        >
        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:padding="0dp"
            >
            <ImageView
                android:id="@+id/iv_icon"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_margin="5dp"
                />
            <TextView
                android:id="@+id/app_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:layout_toRightOf="@id/iv_icon"
                android:layout_toEndOf="@id/iv_icon"
                android:fontFamily="sans-serif-condensed"
                android:ellipsize="end"
                android:maxLines="1"
                />
            <TextView
                android:id="@+id/app_package"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:layout_below="@id/app_label"
                android:layout_toRightOf="@id/iv_icon"
                android:layout_toEndOf="@id/iv_icon"
                android:fontFamily="sans-serif-condensed"
                android:textStyle="italic"
                android:ellipsize="end"
                android:maxLines="1"
                />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

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


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

    private CoordinatorLayout mCLayout;
    private GridView mGridView;

    private Random mRandom = new Random();


    @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);

        // Get a list of installed apps package names
        final List<String> packages = getInstalledPackages();

        // Initialize a new array adapter instance
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                mContext,
                android.R.layout.simple_list_item_1,packages
        ){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Get the custom view
                LayoutInflater inflater = mActivity.getLayoutInflater();
                View cellView = inflater.inflate(R.layout.gridview_custom_item_view,null,true);

                // Get current package name
                String packageName = packages.get(position);

                // Display the app package name
                TextView tv_package = (TextView) cellView.findViewById(R.id.app_package);
                tv_package.setText(packageName);

                // Get the app label
                TextView tv_label = (TextView) cellView.findViewById(R.id.app_label);
                tv_label.setText(getApplicationLabelByPackageName(packageName));

                // Get the card view
                CardView cardView = (CardView) cellView.findViewById(R.id.card_view);

                // Generate a random background color for card view
                cardView.setBackgroundColor(getRandomDarkerHSVColor());

                // Generate the icon
                ImageView iv_icon = (ImageView) cellView.findViewById(R.id.iv_icon);
                Drawable icon = getAppIconByPackageName(packageName);
                iv_icon.setImageDrawable(icon);

                // Finally, return the view
                return cellView;
            }
        };

        // 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) {
                CardView cardView = (CardView) view.findViewById(R.id.card_view);
                TextView tv_label = (TextView) view.findViewById(R.id.app_label);
                TextView tv_package = (TextView) view.findViewById(R.id.app_package);

                // Display the clicked items details on snack bar
                Snackbar snackbar = Snackbar.make(
                        mCLayout,
                        "Clicked : " + tv_label.getText() + "\n"+tv_package.getText(),
                        Snackbar.LENGTH_LONG);
                snackbar.getView().setBackgroundColor(Color.parseColor("#FF4B6947"));
                snackbar.show();

                // Initialize a new color drawable
                ColorDrawable[] color = {
                        new ColorDrawable(getRandomDarkerHSVColor()),
                        new ColorDrawable(getRandomDarkerHSVColor())
                };
                // Initialize a new transition drawable
                TransitionDrawable trans = new TransitionDrawable(color);
                trans.startTransition(300); // duration 300 milliseconds

                // Animate the background color of card view
                cardView.setBackground(trans);
                trans.startTransition(300); // duration 3 seconds
            }
        });

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

    // Get a list of installed app
    public List<String> getInstalledPackages(){
        // Initialize a new Intent which action is main
        Intent intent = new Intent(Intent.ACTION_MAIN,null);

        // Set the newly created intent category to launcher
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        // Set the intent flags
        intent.setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK|
                        Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
        );

        // Generate a list of ResolveInfo object based on intent filter
        List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent,0);

        // Initialize a new ArrayList for holding non system package names
        List<String> packageNames = new ArrayList<>();

        // Loop through the ResolveInfo list
        for(ResolveInfo resolveInfo : resolveInfoList){
            // Get the ActivityInfo from current ResolveInfo
            ActivityInfo activityInfo = resolveInfo.activityInfo;

            // Add the package to the list
            packageNames.add(activityInfo.applicationInfo.packageName);
        }

        return packageNames;
    }

    // Custom method to get application icon by package name
    public Drawable getAppIconByPackageName(String packageName){
        Drawable icon;
        try{
            icon = mContext.getPackageManager().getApplicationIcon(packageName);
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
            // Get a default icon
            icon = ContextCompat.getDrawable(mContext,R.drawable.ic_add_circle_outline_black_48dp);
        }
        return icon;
    }

    // Custom method to get application label by package name
    public String getApplicationLabelByPackageName(String packageName){
        PackageManager packageManager = mContext.getPackageManager();
        ApplicationInfo applicationInfo;
        String label = "Unknown";
        try {
            applicationInfo = packageManager.getApplicationInfo(packageName, 0);
            if(applicationInfo!=null){
                label = (String)packageManager.getApplicationLabel(applicationInfo);
            }

        }catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return label;
    }

    // Custom method to generate random darker HSV color
    public int getRandomDarkerHSVColor(){
        // Generate a random hue value between 0 to 360
        int hue = mRandom.nextInt(361);
        // We make the color depth full
        float saturation = 1.0f;
        // We make a full bright color
        float value = 0.8f;
        // We avoid color transparency
        int alpha = 255;
        // Finally, generate the color
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        // Return the color
        return color;
    }
}