Download Code

Xamarin Fragments Example Android

In this tutorial you will learn about Xamarin Fragments Example Android and developing a simple android gallery app.

So let me give some introduction about

  • Fragment
  • FragmentManager
  • FragmentTransaction
  • LayoutInflater

Xamarin Fragments

A fragment is a particular portion of UI in an activity.

One Activity can contain multiple fragments to build flexible UI.

Fragments can also reuse in different activities.

Fragments can be added at runtime and have their own life cycle just like activity.

Fragments introduced in Android 3.0 (API Level 11).The basic purpose is to design dynamic and flexible UI for different screen sizes.

xamarin fragments,xamarin fragments example,xamarin fragments example android,fragments example android

In above picture fragments design to support tablets and Handset depending on screen sizes.Below image is fragments life cycle just like activity life cycle.

xamarin fragments,xamarin fragments example,xamarin fragments example android,fragments example android

Fragment Manager

Fragment Manager class used to interact with fragment object inside of an activity.

Fragment Transaction

Fragment Transaction used to perform operations on fragments in your activity like add,remove,replace,transition style.

Layout Inflater

Layout Inflater is used to set layout xml in particular View using inflate() method.

Create new android application project.

Then create two layouts by right clicking on  Layout > New File > Layout.And put some UI components in that layout.I have used ImageView.

xamarin fragments,xamarin fragments example,xamarin fragments example android,fragments example android

Create two classes for Fragments.
Codec.cs Class

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

namespace XamarinFragments
{
	[Activity(Label = "codec")]
	public class codec : Fragment
	{
		public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
		 
			return inflater.Inflate(Resource.Layout.codec, container, false);
		}
	}
}


first.cs Class

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


namespace XamarinFragments
{
	[Activity(Label = "first")]
	public class first : Fragment
	{
		public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			 
			 return inflater.Inflate(Resource.Layout.first, container, false);
		}
	}
}


After that go to your Main.axml file.Add Frame Layout and two buttons.

xamarin fragments,xamarin fragments example,xamarin fragments example android,fragments example android

Now MainActivity.cs create button variable’s and apply click event.

MainActivity.cs

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


namespace XamarinFragments
{
	[Activity(Label = "XamarinFragments", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{




		Button prv, nxt;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			prv = FindViewById< Button >(Resource.Id.prev);
			prv.Click += delegate {
				FragmentTransaction tran = this.FragmentManager.BeginTransaction();
				var c = new codec();
				tran.Replace(Resource.Id.fragment_container, c);             // replace fragment with existing


				tran.Commit();
				Toast.MakeText(this, "Fragment Replaced", ToastLength.Short).Show();
};


			nxt = FindViewById< Button >(Resource.Id.next);
			nxt.Click += delegate {
				FragmentTransaction tran = this.FragmentManager.BeginTransaction();
				var f = new first();
				tran.Replace(Resource.Id.fragment_container, f);             // replace fragment with existing


				tran.Commit();
				Toast.MakeText(this, "Fragment Replaced", ToastLength.Short).Show();
};

		}

	}
}



Run your Xamarin Fragments Example Android application project.

xamarin fragments,xamarin fragments example,xamarin fragments example android,fragments example android

that’s it for Xamarin Fragments Example Android tutorial.

Thanks.