In this blogpost I will explain how to use Fragments in Xamarin.android in a easy way. To explain this I made a very small example. First of all make sure that the Android Support Library V4 is in the components folder. If it isn't then you should add it Right click on the components folder => Edit components => Get more components and search for it, if it didn't already shown up at the edit components screen.
To start with fragments, create a new project. In there you will find the regular main + main.axml files, in the main.axml file you will need a Framelayout to put your fragment inside. This should look like this:
<?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:id="@+id/Content"
android:background="#ffffffff">
<FrameLayout
android:id="@+id/details"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:minHeight="20dp" />
</LinearLayout>
After doing this you need to create the fragment itself, create a new .axml file in the layout map. Call it whatever you want, in this example I call it Users.axml. Make sure it is an .axml. Should look like this:
<?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:id="@+id/Users">
<TextView
android:text="Here does the users come..."
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/textViewSettings"
android:gravity="center"
android:textColor="#000000"
android:layout_marginLeft="8dp" />
</LinearLayout>
This Users.axml could contain whatever you want but in this example I just create a TextView in it. Next thing you need to do is create a Android Fragment. Should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace FlyOutMenu
{
public class UsersFragment : Fragment
{
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Create your fragment here
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
View users = inflater.Inflate (Resource.Layout.Users, container, false);
return users;
}
}
}
In the fragment itself we created a view where we inflate the created .axml file in it, in this way it is also put into the container. Afterwards the view is returned.
Now you need to edit your mainactivity so it will load your fragment, we need to do this with the fragmentmanager that is given with Xamarin. Should look like this:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace FlyOutMenu
{
[Activity (Label = "Users", MainLauncher = true, Theme = "@android:style/Theme.Holo.Light.NoActionBar")]
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);
FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
fragmentTx.Add(Resource.Id.details, new UsersFragment());
fragmentTx.Commit();
}
}
}
So to start with putting the fragment into the Framelayout you need to create a FragmentTransaction wich will start the transaction. After that we can add the Fragment into the FrameLayout wich in this case is called details. After that we need to commit.
I hope this post helped to create fragments in Xamarin.Android, if you are having trouble with doing this or have a question please feel free to leave a message below.