Gallery View adalah layout yang menampilkan item-item
dalam bentuk image. Untuk
mempermudah memahaminya buatlah project dengan
ketentuan sebagai berikut:
Project Name :
Gallery View
Build Target : Android 4.2.2
Aplication name : gallery
Package name :
com.immobulus.gallery
Activity : gallery
Buatlah file
layout xml di res/layout dengan
nama file displayview.xml
displayview.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Images Kota Palembang" />
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="320px"
android:layout_height="250px"
android:scaleType="fitXY"
/>
</LinearLayout>
Untuk
tampilan gallery kita mempersiapkan
tujuh buah gambar seperti berikut:
Kemudian
coding gallery.java adalah sebagai
berikut:
gallery.java
package com.immobulus.gallery;
import
android.app.Activity;
import android.content.Context;
import
android.content.res.TypedArray;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.AdapterView;
import
android.widget.BaseAdapter;
import
android.widget.Gallery;
import android.widget.ImageView;
import
android.widget.Toast;
import
android.widget.AdapterView.OnItemClickListener;
public class gallery extends Activity
{
//---the images to display---
Integer[] imageIDs = {
R.drawable.palembang1,
R.drawable.palembang2,
R.drawable.palembang3,
R.drawable.palembang4,
R.drawable.palembang5,
R.drawable.palembang6,
R.drawable.palembang7,
};
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.displayview);
Gallery gallery = (Gallery)
findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new
OnItemClickListener()
{
public void
onItemClick(AdapterView parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position +
1) + "
selected",
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
{
context = c;
//---setting the style---
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View
convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new
Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
Sementara
ViewsActivity.java adalah sebagai berikut:
ViewsActivity.java
package com.immobulus.gallery;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class ViewsActivity extends Activity
{
/** Called
when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, gallery.class));
}
}
Sumber: PEMROGRAMAN APLIKASI MOBILE SMARTPHONE DAN TABLET PC BERBASIS ANDROID. oleh : Nazruddin Safaat H
Tidak ada komentar:
Posting Komentar