Let us first define the layout for your fragment. In this example, I have used two TextViews and Button. My layout looks as follows:
<?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:padding="10dp">
<TextView
android:text="Lorem ipsum"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<TextView
android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit...."
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginTop="10dp" />
<Button
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CloseButton"
android:layout_marginTop="10dp" />
</LinearLayout>
<?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:padding="10dp">
<TextView
android:text="Lorem ipsum"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<TextView
android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit...."
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginTop="10dp" />
<Button
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CloseButton"
android:layout_marginTop="10dp" />
</LinearLayout>
<?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:padding="10dp"> <TextView android:text="Lorem ipsum" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" /> <TextView android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit...." android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_marginTop="10dp" /> <Button android:text="Close" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/CloseButton" android:layout_marginTop="10dp" /> </LinearLayout>
Now let us inflate the layout from OnCreateView() method. My DialogFragment class looks as follows:
public class DialogFragment1 : DialogFragment { public static DialogFragment1 NewInstance(Bundle bundle){ DialogFragment1 fragment = new DialogFragment1 (); fragment.Arguments = bundle; return fragment; } public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Use this to return your custom view for this Fragment View view = inflater.Inflate(Resource.Layout.DialogFragment1Layout, container, false); Button button = view.FindViewById<Button> (Resource.Id.CloseButton); button.Click += delegate { Dismiss(); Toast.MakeText(Activity ,"Dialog fragment dismissed!" , ToastLength.Short).Show(); }; return view; } }
We are pretty much done!. Add the following code snippet in your Activity to instantiate and display the dialog:
FragmentTransaction ft = FragmentManager.BeginTransaction(); //Remove fragment else it will crash as it is already added to backstack Fragment prev = FragmentManager.FindFragmentByTag("dialog"); if (prev != null) { ft.Remove(prev); } ft.AddToBackStack(null); // Create and show the dialog. DialogFragment1 newFragment = DialogFragment1.NewInstance(null); //Add fragment newFragment.Show(ft, "dialog");
References :
http://javatechig.com/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android