Xamarin Dialog Fragment - With Simple ListView

xamarin dialogfragment

Hi guys.We discuss how to make and use a xamarin android dialogfragment with  a simple listview.You click a button inside your mainactivity and this displays a dialog fragment.Inside the dialogfragment we have as simple listview.

SECTION 1 : Our TVShowFragment class

  • Subclasses Android.App.DialogFragment
  • We inflate our fragment layout here.
  • We also populate our listview here and handle its itemclicks
using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;

namespace DialogFrag
{
    class TVshowFragment : DialogFragment
    {
        private ListView lv;
        private String[] tvshows = { "BlackList", "Crisis", "Blindspot", "Breaking Bad", "Gotham", "Banshee" };
        private ArrayAdapter adapter;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.Inflate(Resource.Layout.fraglayout, container, false);

            //SET TITLE FOR DIALOG
            this.Dialog.SetTitle("TV Shows");

            lv = v.FindViewById<ListView>(Resource.Id.lv);

            //ADAPTER
            adapter=new ArrayAdapter(this.Activity,Android.Resource.Layout.SimpleListItem1,tvshows);
            lv.Adapter = adapter;

            //ITEM CLICKS
            lv.ItemClick += lv_ItemClick;

            return v;
        }

        void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Toast.MakeText(this.Activity,tvshows[e.Position],ToastLength.Short).Show();
        }
    }
}

SECTION 2 : Our MainActivity

  • Launcher activity
  • We initialize our fragmentManager and pass it to our Fragment's show() method
  • Has a button that when clicked we show simple listview
using System;
using Android.App;
using Android.Widget;
using Android.OS;

namespace DialogFrag
{
    [Activity(Label = "DialogFrag", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private FragmentManager fm;
        private TVshowFragment tv;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += button_Click;

            fm = this.FragmentManager;
            tv=new TVshowFragment();

        }

        void button_Click(object sender, EventArgs e)
        {
            //SHOW DIALOG FRAGMENT
            tv.Show(fm, "TV_tag");
        }

    }
}

 SECTION 3 : Our Layouts

Our Main.axml

  • Contains our button to be clicked
<?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:background="#2f9bc1">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#33363c"
        android:text="Show" />
</LinearLayout>

Our Fragment Layout

  • Defines the outlook of our dialogfragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv" />
</LinearLayout>

Cheers.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post