Pages

Wednesday, June 1, 2016

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 in.blogspot.kantapp.galleryviewinandroid;

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.all_family_of_goku, R.drawable.dbz, R.drawable.goku,
            R.drawable.goku_family, R.drawable.naruto, R.drawable.vegito, R.drawable.fairy_tail,
            R.drawable.pa_logo ,R.drawable.gogeta_ssj ,R.drawable.goku};
}

No comments:

Post a Comment