info@thistimebd.com

Sunday 24th of November 05:12:46am

Grid View Android App Source Code

MainActivity.java

=================


package com.sam.web.gridview;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.GridView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private int[]flags={R.drawable.australia,R.drawable.bangladesh,R.drawable.brazil,R.drawable.hong_kong,

            R.drawable.india,R.drawable.italy,R.drawable.laos,R.drawable.new_zealand,

            R.drawable.pakistan,R.drawable.russia,R.drawable.uk};


    String[] countryNames;

    private GridView gridView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        countryNames=getResources().getStringArray(R.array.country_names);


        gridView=(GridView) findViewById(R.id.gridViewId);


        CustomAdapter adapter=new CustomAdapter(this,countryNames,flags);

        gridView.setAdapter(adapter);


        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String value=countryNames[position];

                Toast.makeText(MainActivity.this,value,Toast.LENGTH_LONG).show();

            }

        });

    }

}

========================================================================================

activity_main.xml

==================



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.sam.web.gridview.MainActivity">


    <GridView

        android:verticalSpacing="10dp"

        android:horizontalSpacing="10dp"

        android:listSelector="#22fff0"

        android:numColumns="3"

        android:id="@+id/gridViewId"

        android:layout_width="match_parent"

        android:layout_height="match_parent"></GridView>


</LinearLayout>

--------------------------------------------------------------------------------

string.xml

============


<resources>

    <string name="app_name">GridView</string>

    <string-array name="country_names">

        <item>Australia</item>

        <item>Bangladesh</item>

        <item>Brazil</item>

        <item>HongKong</item>

        <item>India</item>

        <item>Italy</item>

        <item>Laos</item>

        <item>NewZialand</item>

        <item>Pakistan</item>

        <item>Russia</item>

        <item>UK</item>

    </string-array>

</resources>

=====================================================================================

sample_view.xml

==============


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:padding="5dp"

    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <ImageView

        android:id="@+id/imageViewId"

        android:layout_width="match_parent"

        android:layout_height="100dp"

        android:scaleType="fitXY"

        android:layout_gravity="center_horizontal"

        android:src="@drawable/australia"

        />

    <TextView

        android:id="@+id/textViewId"

        android:textAppearance="?android:attr/textAppearanceMedium"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:text="Australia"


        />


</LinearLayout>


--------------------------------------------------------------------------------------


CustomAdapter.java

==================


package com.sam.web.gridview;


import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


/**

 * Created by maudud on 1/15/18.

 */


public class CustomAdapter extends BaseAdapter {

    Context context;

    int[] flags;

    String[] countryNames;

    private LayoutInflater inflater;

    CustomAdapter(Context context,String[] countryNames, int[] flags){


        this.context=context;

        this.countryNames=countryNames;

        this.flags=flags;


    }


    @Override

    public int getCount() {

        return countryNames.length;

    }


    @Override

    public Object getItem(int position) {

        return null;

    }


    @Override

    public long getItemId(int position) {

        return 0;

    }


    @Override

    public View getView(int position, View convertView, ViewGroup parent) {


        if(convertView==null){


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

            convertView= inflater.inflate(R.layout.sample_view,parent,false);

        }


        ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewId);

        TextView textView=(TextView) convertView.findViewById(R.id.textViewId);

        imageView.setImageResource(flags[position]);

        textView.setText(countryNames[position]);

        return convertView;

    }

}