Back to All Articles

Articles tagged xamarin:

Xamarin Android - Fragments and Orientations/Rotation

When you rotate/change the orientation of your Android application the Activity will be destroyed and recreated.  What this means is that any variables, fragments will be destroyed when the user changes the orientation of their device (say going from portrait to landscape).  Your users will hate you.  For example, If they are filling out a form and by accident the orientation changes all fields entered will be lost.

Read More
Xamarin Android - ListViews Part 2 of 2

Continuing on from an older post, we will modify our listview to display 2 columns.  Ultimately, we will put an image in the left column and text in the right column.  Currently we have created an ArrayAdapter which takes a list/collection of strings and displays each row in a template.  The template was defined in a layout TextViewItem.axml

Read More
Xamarin Android - DrawerLayout

To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer. <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerLayout"    android:layout_width ="match_parent"     android:layout_height="match_parent">   <!-- Content -->   <FrameLayout         android:id ="@+id/frameLayout"         android:layout_width ="match_parent"         android:layout_height="match_parent" />   <!-- Navigation drawer menu -->   <ListView         android:id="@+id/drawerListView"         android:layout_gravity="start"         android:choiceMode    ="singleChoice"         android:layout_width  ="240dp"         android:layout_height ="match_parent"         android:background ="?android:attr/windowBackground" /> </android.support.v4.widget.DrawerLayout> The above layout uses a DrawerLayout with two child views: a FrameLayout to contain the main content which will be populated with a Fragment at runtime, and a ListView for the contents of the drawer. Now to the MainActivity.cs coding.. let’s get a reference to the drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);  You will find that DrawerLayout is not defined.  You need to add a reference via Nuget to Android Support Library v4.  Open Nuget and search for Xamarin.android.support.v4 Android Support Libraries : The Android Support Libraries are a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running older versions of Android.  Including the Support Libraries in your Android project is considered a best practice for application developers. Adding a using statement for Android.Support.V4.Widget will complete the reference to DrawerLayout. _menuItems = new string { "Welcome", "Options", "About" }; _drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout); _drawerList = FindViewById<ListView>(Resource.Id.drawerListView); _drawerList.Adapter = new ArrayAdapter<string>(this, Resource.Layout.ListViewMenuRow, Resource.Id.menuRowTextView, _menuItems); We have a List which should be populated by an Adapter (ArrayAdapter or SimplerCursorAdaptor) At this point we have (left) and if you swipe toward the right you see the menu.  Hooking up the menu click looks like.. _drawerList.ItemClick += _drawerList_ItemClick; private void _drawerList_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {     OnMenuItemClick(e.Position); }private void OnMenuItemClick(int position) {     this.Title = _menuItemsposition; }

Read More