Sunday 27 January 2013

GalleryView in Android


activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

MainActivity.java:
package com.ram.galleryview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Gallery g = (Gallery) findViewById(R.id.gallery1);
ImageAdapter ia = new ImageAdapter();
g.setAdapter(ia);

}

class ImageAdapter extends BaseAdapter {

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

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View view, ViewGroup vg) {
// TODO Auto-generated method stub
ImageView image = new ImageView(getApplicationContext());
image.setImageResource(images[position]);
return image;
}

}

int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
R.drawable.p8, R.drawable.p9, R.drawable.p10 };
}

2 comments:

  1. sir,
    where these pictures are stored;



    one problem occuring while making the layout

    The following classes could not be found:
    - Gallery (Change to android.widget.Gallery, Fix Build Path, Edit XML

    ReplyDelete