Xamarin Android – GridView and OnClick

Xamarin Android – GridView and OnClick



Language :
Platform :
Rating : No ratings yet.




Guys today we cover a C# Xamarin Android - Simple GridView and OnClick tutorial.

But then if you do prefer to watch a video tutorial with maybe more explanations,then we have one here :

[embedyt] http://www.youtube.com/watch?v=dE2hlZu1u4I[/embedyt]

Remeber to subscribe and leave us a big like.

Cheers.

PART A: OUR MAINACTIVITY

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections;
namespace Simple_GridView
{
    [Activity(Label = "Simple GridView", MainLauncher = true, Icon = "@drawable/simplesmile")]
    public class MainActivity : Activity
    {
        GridView gv;
        ArrayAdapter adapter;
        ArrayList players;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            gv = FindViewById<GridView>(Resource.Id.gv);
            getData();
            adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, players);
            //BIND
            gv.Adapter = adapter;
            gv.ItemClick += gv_ItemClick;
        }
        void gv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Toast.MakeText(this, players[e.Position].ToString(), ToastLength.Short).Show();
        }
        //FILL DATA
        private void getData()
        {
            players = new ArrayList();
            players.Add("Juan Mata");
            players.Add("Juan Mata");
            players.Add("Jesus Navas");
            players.Add("John Barnes");
            players.Add("John Doe");
            players.Add("Joel CampBell");
            players.Add("John Moh");
            players.Add("Jose Mou");
            players.Add("Joe Aurorah");
            players.Add("Julius Finch");
            players.Add("July Sasha");
            players.Add("Joan Emmy");
        }
    }
}

PART B : OUR MAIN LAYOUT

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <GridView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gv"
        android:numColumns="3" />
</LinearLayout>

PART C: OUR RESULT  C# Xamarin Android - Simple GridView and OnClick



Rating :

    COMMENTS