Membuat gallery di Android

Bismillah….

Entah mengapa, saya sangat senang ketika berhasil membuat gallery di Android.

Meskipun masih terbilang simple, namun menurut saya widget yang satu ini sangat aplikatif untuk menampilkan gambar.

Yupz, lebih ‘eye catching’ sepertinya..

codingnya seperti ini:

package app.seamolec.sheilla;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Toast;

public class GallleryActivity extends Activity {

Integer[] imageIDs = { R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7 };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery GallleryActivity = (Gallery) findViewById(R.id.gallery1);
GallleryActivity.setAdapter(new ImageAdapter(this));
GallleryActivity.setOnItemClickListener(new OnItemClickListener() {

@SuppressWarnings(“rawtypes”)
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageIDs[position]);

Toast.makeText(getBaseContext(),
“pic” + (position + 1) + “selected”, Toast.LENGTH_SHORT)
.show();
}
});

}

public class ImageAdapter extends BaseAdapter {

private Context context;

private int ItemBackground;

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

public int getCount() {
// TODO Auto-generated method stub
return imageIDs.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(getBaseContext());
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
return imageView;
}
}
}

dan kode untuk tampilannya:

package app.seamolec.sheilla;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ViewActivity extends Activity{

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, GallleryActivity.class));

}}

kemudian untuk main.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
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=”gambar Sheilla” />

<Gallery
android:id=”@+id/gallery1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<ImageView
android:id=”@+id/imageView1″
android:layout_width=”320px”
android:layout_height=”250px”
android:scaleType=”fitXY” />

</LinearLayout>

Maka…outputnya seperti ini:

 

Begitulah, percobaan yang telah saya buat.. Simple kan?

Semoga bermanfaat!

About these ads

About Sheilla Rizkia Ferianty

Semangat!

Posted on 12/02/2012, in Just to know and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: