Gallery in Mono for Android

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="320px"
        android:layout_height="250px" />
</LinearLayout>

Activity1.cs

namespace gridview
{
    [Activity(Label = "gridview", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            Gallery gallery = FindViewById<Gallery>(Resource.Id.gallery);
            ImageView iv = FindViewById<ImageView>(Resource.Id.imageView1);
            gallery.SetFadingEdgeLength(40);

            gallery.Adapter = new ImageAdapter(this);


            gallery.ItemClick += delegate(object sender, Android.Widget.AdapterView.ItemClickEventArgs args)
            {
                Toast.MakeText(this, args.Position.ToString(), ToastLength.Short).Show();

            };
        }

        public class ImageAdapter : BaseAdapter
        {
            Context context;


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

            public override int Count { get { return thumbIds.Length; } }

            public override Java.Lang.Object GetItem(int position)
            {
                return null;
            }

            public override long GetItemId(int position)
            {
                return 0;
            }

            // create a new ImageView for each item referenced by the Adapter
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                ImageView i = new ImageView(context);
                i.SetImageURI(Android.Net.Uri.Parse(thumbIds[position].ToString()));
                i.SetImageResource(thumbIds[position]);
                i.LayoutParameters = new Gallery.LayoutParams(200, 100);

                i.SetScaleType(ImageView.ScaleType.FitXy);
                i.SetPadding(10, 10, 10,10);
                return i;
            }

            // references to our images
            int[] thumbIds = {
            Resource.Drawable.sample_0,
            Resource.Drawable.sample_1,
            Resource.Drawable.sample_2,
            Resource.Drawable.sample_3,
            Resource.Drawable.sample_4,
            Resource.Drawable.sample_5,
            Resource.Drawable.sample_6
             };
        }
    }

}


Do you want more TechChaitu Updates ?
Enter your email address:

Comments

Popular posts from this blog

Download Visakhapatnam Tirupati AC Double Decker Express Act