Thursday 8 November 2012

Selection Widget - membuat Galerry Widget Android

Gallery Widget merupakan salah satu widget yang disediakan Android guna melihat tampilan foto secara lebih mudah. Untuk membuatnya membutuhkan 3 file, yaitu 1 file XML dan 2 file java.
1. File XML --> activity_main.xml (berfungsi untuk desain tampilan layout Android saat program dijalankan
2. File JAVA --> MainActivity.java (file utama) dan ImageAdapter.java ( file yang berisi script konfigurasi gambar/foto yang akan ditampilkan)

File 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="SDK1.5 /samples/.../view/Gallery1.java"/>
 <TextView
android:id="@+id/mySelection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff" />
<Gallery
android:id="@+id/myGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" />

 </LinearLayout>




****

File Java :
1. MainActivity.java :
****

package wulan.novi.gallerywidget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
TextView mySelection;
Gallery myGallery;
@Override
public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
        mySelection = (TextView) finGallery) findViewById(R.id.myGallery);dViewById(R.id.mySelection);
        myGallery = (
        myGallery.setAdapter(new ImageAdapter(this));
        myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {
        mySelection.setText(" selected option: " + arg2 );
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        mySelection.setText("Nothing selected");
        }
        });
    }}


****

2. ImageAdapter.java :
****

package wulan.novi.gallerywidget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4};

public ImageAdapter(Context c) {
this.myContext = c;
}

public int getCount() {
return this.myImageIds.length;
}
public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(this.myImageIds[position]);
iv.setLayoutParams(new Gallery.LayoutParams(95, 70));
return iv;
}
}


****

NB : Sebelum itu letakkan gambar/image/foto yang akan di tampilkan pada folder workspace yang telah dibuat workspace\GalleryWidget\res\drawable-hdpi
--Saya memilih drawable-hdpi karena resolusi gambar yang kecil. Usahakan dengan format gambar .png

Berikut hasil running programnya :


No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...