Pada postingan kali ini saya akan mengaplikasikan widget galeri pada android. sebelum memulai baca doa dulu ya... hehehe..
langsung saja pada tutorialnya.. o ya jangan lupa siapin gambar-gambar yang akan ditampilkan dalam galeri android ini. jangan lupa juga kasih nama gambarnya dengan huruf kecil ya... Kalo sudah, langsung saja ke tutorialnya.. cekidot..
Selesai...
===>>>SALAM<<<=====
-Galery di Android
langsung saja pada tutorialnya.. o ya jangan lupa siapin gambar-gambar yang akan ditampilkan dalam galeri android ini. jangan lupa juga kasih nama gambarnya dengan huruf kecil ya... Kalo sudah, langsung saja ke tutorialnya.. cekidot..
- Buka Eclipse dan buat android project baru
- Copy gambar-gambar yang akan digunakan dan paste di /res/drawable
- buat main.xml seperti berikut:
<?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" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Gallery
android:id="@+id/galeri"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/gambar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.64" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Gallery
android:id="@+id/galeri"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/gambar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.64" />
</LinearLayout>
- Setelah itu sekarang kita buat juga attrs.xml di res/values seperti berikut
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Sekarang saatnya ke javaya:<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
package org.dharmatin.ImageGaleri;
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 ImageGaleriActivity extends Activity {
Integer[]foto={
R.drawable.arsenal125,
R.drawable.arsenal9,
R.drawable.arsenalbelt,
R.drawable.arsenalold,
R.drawable.arsenalold2,
};
String[]Nama={"Arsenal 125 thn","Arsenal Wallpaper","Arsenal Belt",
"Arsenal Dulu","Arsenal Jadul"};
ImageView iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery ga= (Gallery)findViewById(R.id.galeri);
ga.setAdapter(new ImageAdapter(this));
iv = (ImageView)findViewById(R.id.gambar);
ga.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(),"Picture of " + (Nama[arg2]), Toast.LENGTH_LONG).show();
iv.setImageResource(foto[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context ctx;
int imageBackground;
public ImageAdapter(Context c){
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.HelloGallery);
imageBackground = ta.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return foto.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(foto[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
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 ImageGaleriActivity extends Activity {
Integer[]foto={
R.drawable.arsenal125,
R.drawable.arsenal9,
R.drawable.arsenalbelt,
R.drawable.arsenalold,
R.drawable.arsenalold2,
};
String[]Nama={"Arsenal 125 thn","Arsenal Wallpaper","Arsenal Belt",
"Arsenal Dulu","Arsenal Jadul"};
ImageView iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery ga= (Gallery)findViewById(R.id.galeri);
ga.setAdapter(new ImageAdapter(this));
iv = (ImageView)findViewById(R.id.gambar);
ga.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(),"Picture of " + (Nama[arg2]), Toast.LENGTH_LONG).show();
iv.setImageResource(foto[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context ctx;
int imageBackground;
public ImageAdapter(Context c){
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.HelloGallery);
imageBackground = ta.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return foto.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(foto[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Selesai...
===>>>SALAM<<<=====
Widget by Css Reflex | TutZone