GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.
Example:
Simple GridView:
activity_simple_grid_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Simple GridView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:numColumns="2"
android:textAlignment="center" >
</GridView>
</LinearLayout>
activity_custom_grid_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Custom GridView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:numColumns="2"
android:textAlignment="center" >
</GridView>
</LinearLayout>
style_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
MainActivity.java
package in.androiddevelopmentanddiscussion.gridviewexample;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
Button simple,custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simple = (Button)findViewById(R.id.button1);
custom = (Button)findViewById(R.id.button2);
simple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, SimpleGridView.class);
startActivity(i);
}
});
custom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, CustomGridView.class);
startActivity(i);
}
});
}
}
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
Example:
Simple GridView:
Custom GridView:
Code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="GridView Example"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:text="Simple GridView" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="Custom GridView" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Simple GridView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:numColumns="2"
android:textAlignment="center" >
</GridView>
</LinearLayout>
activity_custom_grid_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Custom GridView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:numColumns="2"
android:textAlignment="center" >
</GridView>
</LinearLayout>
style_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
MainActivity.java
package in.androiddevelopmentanddiscussion.gridviewexample;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
Button simple,custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simple = (Button)findViewById(R.id.button1);
custom = (Button)findViewById(R.id.button2);
simple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, SimpleGridView.class);
startActivity(i);
}
});
custom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, CustomGridView.class);
startActivity(i);
}
});
}
}
SimpleGridView.java
package in.androiddevelopmentanddiscussion.gridviewexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class SimpleGridView extends Activity {
GridView gv;
String [] values = {"Java","Android","PHP","DotNet","HTML","CSS","Oracle","SQLite","MySQL"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_grid_view);
gv = (GridView)findViewById(R.id.gridView1);
ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,values);
gv.setAdapter(adp);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(values[arg2].equalsIgnoreCase("Java"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on Java", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("Android"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on Android", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("PHP"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on PHP", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("DotNet"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on DotNet", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("HTML"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on HTML", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("CSS"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on CSS", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("Oracle"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on Oracle", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("SQLite"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on SQLite", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("MySQL"))
{
Toast.makeText(SimpleGridView.this, "You Clicked on MySQL", Toast.LENGTH_SHORT).show();
}
}
});
}
}
CustomGridView.java
package in.androiddevelopmentanddiscussion.gridviewexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class CustomGridView extends Activity {
GridView gv;
String [] values = {"Java","Android","PHP","DotNet","HTML","CSS","Oracle","SQLite","MySQL"};
int [] images = {R.drawable.java,R.drawable.android,R.drawable.php,R.drawable.dotnet,R.drawable.html,R.drawable.css,R.drawable.oracle,R.drawable.sqlite,R.drawable.mysql};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_grid_view);
gv = (GridView)findViewById(R.id.gridView1);
CustomArrayAdapter adapter = new CustomArrayAdapter(CustomGridView.this,R.layout.style_activity, values,images);
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(values[arg2].equalsIgnoreCase("Java"))
{
Toast.makeText(CustomGridView.this, "You Clicked on Java", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("Android"))
{
Toast.makeText(CustomGridView.this, "You Clicked on Android", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("PHP"))
{
Toast.makeText(CustomGridView.this, "You Clicked on PHP", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("DotNet"))
{
Toast.makeText(CustomGridView.this, "You Clicked on DotNet", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("HTML"))
{
Toast.makeText(CustomGridView.this, "You Clicked on HTML", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("CSS"))
{
Toast.makeText(CustomGridView.this, "You Clicked on CSS", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("Oracle"))
{
Toast.makeText(CustomGridView.this, "You Clicked on Oracle", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("SQLite"))
{
Toast.makeText(CustomGridView.this, "You Clicked on SQLite", Toast.LENGTH_SHORT).show();
}
if(values[arg2].equalsIgnoreCase("MySQL"))
{
Toast.makeText(CustomGridView.this, "You Clicked on MySQL", Toast.LENGTH_SHORT).show();
}
}
});
}
}
CustomArrayAdapter.java
package in.androiddevelopmentanddiscussion.gridviewexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<String>{
Context context;
int layout;
int [] images;
String [] values;
public CustomArrayAdapter(Context context, int layout,String [] values,int [] images)
{
super(context, layout,values);
this.context = context;
this.layout = layout;
this.values = values;
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater infl = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= infl.inflate(layout, null);
}
TextView txt = (TextView)convertView.findViewById(R.id.textView1);
txt.setText(values[position]);
ImageView img = (ImageView)convertView.findViewById(R.id.imageView1);
img.setImageResource(images[position]);
return convertView;
}
}
Download full project from here
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
No comments:
Post a Comment