Xamarin Android: Master Detail View

Introduction

Master detail view means a simple design flow, where a list of items is displayed to the user in the first screen also called  Master View and when a user selects  any item from the list, then he/she is navigated to another view called Detail View containing all the details of the selected item. For example in Gmail android application, in the first view all the emails are displayed in a list and when we click on any email, then the contents of the email are shown in another screen. One more point that we need to consider is that, when user navigates to detail view, then there a back button at the top using which we can navigate back to original screen (Master View) again.

In this article, we will implement a similar concept. We will create a master view containing a simple button. When user click on that button, he/she will navigate to detail view having back button at the top. So let`s get started.

Steps:

  1. To get started, open Visual Studio 2015 and select File menu, New, then Project option. It will open a project dialog box as shown below (F:1)



  2. Select the Blank App (Android) project template and enter the name of the project as MasterDetailApp and click on Ok button. After clicking on Ok button, a blank Xamarin Android application will be created.

  3. Blank App (Android) project template will create a default activity for us (MainActivity.cs file with android layout Main.axml). Main.axml file will be our master view. Replace the content of Main.axml file with the following code.

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout  
    3.     xmlns:android="http://schemas.android.com/apk/res/android"  
    4.         android:orientation="vertical"  
    5.         android:layout_width="fill_parent"  
    6.         android:layout_height="fill_parent">  
    7.     <TextView  
    8.         android:text="Master View"  
    9.         android:textAppearance="?android:attr/textAppearanceMedium"  
    10.         android:layout_width="match_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:gravity="center"  
    13.         android:id="@+id/textView1"  
    14.         android:paddingTop="20dp"  
    15.         android:paddingBottom="20dp" />  
    16.     <Button  
    17.         android:id="@+id/btnClick"  
    18.         android:layout_width="fill_parent"  
    19.         android:layout_height="wrap_content"  
    20.         android:text="Click To Navigate" />  
    21. </LinearLayout>  
    Above code will add a TextView with simple text "Master View" and a button using which we can navigate to our detail screen.

  4. Now we need to add another activity which will be our detail view. To add another activity, right click on project and select Add=>New Item option as shown below (F:2)



    It will open up a dialog box. Select the Activity item template and name it as DetailActivity.cs as shown below (F:3).



  5. We also need to add an Android Layout file which will represent the UI for our detail activity. To add an Android Layout file, right click on Resources\layout folder and select Add, then New Item option (Shown in step 4). It will open a dialog box as shown in image (F:3). Select the Android Layout item template and name it as Detail.axml and then click on Ok button.

  6. Add the following code in Detail.axml file.

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout  
    3.     xmlns:android="http://schemas.android.com/apk/res/android"  
    4.         android:orientation="vertical"  
    5.         android:layout_width="match_parent"  
    6.         android:layout_height="match_parent"  
    7.         android:minWidth="25px"  
    8.         android:minHeight="25px">  
    9.     <TextView  
    10.         android:text="Detail View"  
    11.         android:textAppearance="?android:attr/textAppearanceMedium"  
    12.         android:layout_width="match_parent"  
    13.         android:layout_height="match_parent"  
    14.         android:gravity="center"  
    15.         android:id="@+id/textView1" />  
    16. </LinearLayout>  

    Above code will add a TextView with simple text "Detail View".

  7. Add the following code in DetailActivity.cs file.

    1. using Android.App;  
    2. using Android.OS;  
    3. namespace MasterDetailApp  
    4. {  
    5.     [Activity(Label = "Detail Activity")]  
    6.     public class DetailActivity: Activity  
    7.     {  
    8.         protected override void OnCreate(Bundle savedInstanceState)  
    9.         {  
    10.             base.OnCreate(savedInstanceState);  
    11.             // Create your application here  
    12.             SetContentView(Resource.Layout.Detail);  
    13.         }  
    14.     }  
    15. }  

  8. Now let`s go back to MainActivity.cs file and attach a click event handler to our button, so that when user click on it we can navigate to our detail activity. To attach click event to button, add the following code in MainActivity.cs file.

    1. using Android.App;  
    2. using Android.Widget;  
    3. using Android.OS;  
    4. namespace MasterDetailApp  
    5. {  
    6.     [Activity(Label = "Master Detail App", MainLauncher = true, Icon = "@drawable/icon")]  
    7.     public class MainActivity: Activity  
    8.     {  
    9.         protected override void OnCreate(Bundle bundle)  
    10.         {  
    11.             base.OnCreate(bundle);  
    12.             // Set our view from the "main" layout resource  
    13.             SetContentView(Resource.Layout.Main);  
    14.             // Get our button from the layout resource,  
    15.             // and attach an event to it  
    16.             Button button = FindViewById < Button > (Resource.Id.btnClick);  
    17.             button.Click += delegate  
    18.             {  
    19.                 StartActivity(typeof(DetailActivity));  
    20.             };  
    21.         }  
    22.     }  
    23. }  

    In above code, using the StartActivity() method we can navigate to our DetailActivity.cs.

  9. Last step is to establish the master detail relationship between our Main Activity and Detail Activity. To add the relationship, we need to set the ParentActivity property of Activity attribute in DetailActivity.cs file as shown below (F:4).



  10. Output of our application is as shown below (F:5)

    Android

Conclusion

In this article, we talked about how to create a master detail view in Xamarin Android application. I hope you enjoyed reading the article.


Similar Articles