17 Jul 2012

Grid view Tutorial | Grid view Example in Android.

In this sample code contain display images in Thumb-view and also you can sweep it and also you can set it as wallpaper of Device.

Put couple of images in drawable folder.






Main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="IMAGE In ThumbnailVIew " />

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>


Activity_ThumbView:


public class Activity_ThumbView extends Activity
{

GridView gridview; 

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{

Toast.makeText(Activity_ThumbView.this, "" + position, 2).show();
id=parent.getPositionForView(v);

Intent i=new Intent(Activity_ThumbView.this,Activity_ImageView.class);
i.putExtra("position", position);
startActivity(i);
finish();
}
});
}

public class ImageAdapter extends BaseAdapter
{

Context context; 

public ImageAdapter(Context c )
{
context = c ;
}
public int getCount() {


return mThumbIds.length;
}


public Object getItem(int position) {

return null;
}


public long getItemId(int position) {

return mThumbIds[position];
} 

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}

}

public static Integer[] mThumbIds = {
R.drawable.a,R.drawable.icon,
R.drawable.b, R.drawable.s,
R.drawable.c, R.drawable.r,
R.drawable.d, R.drawable.q,
R.drawable.e, R.drawable.p,
R.drawable.f, R.drawable.o,
R.drawable.g, R.drawable.n,
R.drawable.h, R.drawable.m,
R.drawable.i, R.drawable.l,
R.drawable.j, R.drawable.k,
R.drawable.t,R.drawable.y,
R.drawable.u,R.drawable.x,
R.drawable.v,R.drawable.s,
R.drawable.cd,R.drawable.z,
R.drawable.bc,R.drawable.ab
};

}


image.xml

<?xml version="1.0" encoding="utf-8"?>
<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/thumb_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="IMAGE In ThumbnailVIew " />

    <ImageView
        android:id="@+id/thumb_txt_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="HOME" >
        </Button>

        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Set As Wallpaper" >
        </Button>
    </LinearLayout>

</LinearLayout>

Activity_ImageView


import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Activity_ImageView extends Activity
{
ImageView thumb_imgview;
ViewFlipper viewFlipper;
Button b_wall;
Button b_home;

// Animation a,b;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
int j;

WallpaperManager myWall;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image);

j=getIntent().getExtras().getInt("position");
myWall = WallpaperManager.getInstance(getApplicationContext());

b_wall=(Button) findViewById(R.id.button3);
b_home=(Button) findViewById(R.id.button1);

thumb_imgview=(ImageView) findViewById(R.id.thumb_txt_image);
thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

gestureDetector = new GestureDetector(new MyGestureDetector());

b_wall.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {



try {

myWall.setResource(Activity_ThumbView.mThumbIds[j]);

} catch (IOException e) {

e.printStackTrace();
}


}
});
b_home.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
Intent i=new Intent(Activity_ImageView.this,Activity_ThumbView.class);
startActivity(i);
finish();

}
});

//         a = AnimationUtils.loadAnimation(this, R.anim.translate);
//         b = AnimationUtils.loadAnimation(this, R.anim.rotate);
}

public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener
{
public boolean onFling(MotionEvent m1, MotionEvent m2, float velocityX, float velocityY)
{
try
{
if (Math.abs(m1.getY() - m2.getY()) > SWIPE_MAX_OFF_PATH)
return false;

if(m1.getX() - m2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{

if(Activity_ThumbView.mThumbIds.length>j)
{
j++;


// a.reset();

//                         thumb_imgview.clearAnimation();
//                         thumb_imgview.startAnimation(a);
thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

}


}

else if (m2.getX() - m1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{

if(j>0)
{
j--;

thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);
//b.reset();

//                       thumb_imgview.clearAnimation();
//                       thumb_imgview.startAnimation(b);
}


}
}
catch (Exception e)
{

}
return false;
}
}

public boolean onTouchEvent(MotionEvent event)
{
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}

permission:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

Resources:

Grid LazyLoading : Download full source code from here GridLazyLoading

Grid with UIL : Download Sample

From SDcard : Link:

For more Ref:
Link1:
Link2:
staggeredgridview
http://upadhyayjiteshandroid.blogspot.in/2012/12/android-custon-grid-of-buttons.html

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)


3 comments:

  1. Hi I have problem with gridview... can you please help ? Thanks in advance. Please follow the link for my question problem : http://stackoverflow.com/questions/25373825/android-gridview-scrolling

    ReplyDelete
    Replies
    1. Thanks got your Query,

      I did not get your Query on stack overflow, Kindly post here

      Delete
  2. Hi, Can you please tell me, how can i add admob ads code to this application..
    Thanks

    ReplyDelete