1)Main Activity:
package
com.venky.gridandgallery;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.GridView;
import
android.widget.ImageView;
public class
GridandGalleryActivity extends Activity {
/** Called when the activity is first
created. */
GridView gridView;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView1);
ImageAdapter ia = new ImageAdapter();
gridView.setAdapter(ia);
}
class ImageAdapter extends BaseAdapter {
public int getCount() {
// TODO
Auto-generated method stub
return images.length;
}
public Object getItem(int position) {
// TODO
Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO
Auto-generated method stub
return 0;
}
public View getView(final int position, View
convertView,
ViewGroup parent) {
// TODO
Auto-generated method stub
ImageView image = new
ImageView(getApplicationContext());
image.setLayoutParams(new
GridView.LayoutParams(100, 100));
image.setImageResource(images[position]);
image.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
// TODO
Auto-generated method stub
Intent intent = new
Intent(getApplicationContext(),
GalleryView.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
return image;
}
}
int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11,
R.drawable.a12 };
}
2)Gallery Activity:
package
com.venky.gridandgallery;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.Gallery;
import
android.widget.Gallery.LayoutParams;
import
android.widget.ImageView;
public class GalleryView extends Activity {
Gallery gallery;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Intent intent = getIntent();
int pos =
intent.getIntExtra("position", 0);
gallery = (Gallery) findViewById(R.id.gallery1);
ImageAdapter ia = new ImageAdapter();
gallery.setAdapter(ia);
gallery.setSelection(pos);
}
class ImageAdapter extends BaseAdapter {
public int getCount() {
// TODO
Auto-generated method stub
return images.length;
}
public Object getItem(int position) {
// TODO
Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO
Auto-generated method stub
return 0;
}
public View getView(int position, View
convertView, ViewGroup parent) {
// TODO
Auto-generated method stub
ImageView imageView = new
ImageView(getApplicationContext());
imageView.setLayoutParams(new
Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setImageResource(images[position]);
return imageView;
}
}
int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11,
R.drawable.a12 };
}
3)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:background="#FFFFFF"
android:orientation="vertical"
>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="3"
>
</GridView>
</LinearLayout>
4)gallery.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="vertical"
>
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
5)Download this Project Click Here
No comments:
Post a Comment