This is an android tutorial to show a dropdown menu in the action bar. We use Xamarin as our Framework with C# as our language.

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

namespace ActionBarMenu
{
    [Activity(Label = "ActionBarMenu", MainLauncher = true,  Icon = "@drawable/icon")]
    public class MainActivity : Activity,ActionBar.IOnNavigationListener
    {
        private readonly string[] menuItems = {"Politics","Technology","Sports","Finance"};
        private ArrayAdapter<string> getMyAdapter()
        {
            return new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1,menuItems);
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);
            ActionBar ab = this.ActionBar;
            ab.NavigationMode=ActionBarNavigationMode.List;
            ab.SetListNavigationCallbacks(getMyAdapter(), this );
        }
        public bool OnNavigationItemSelected(int itemPosition, long itemId)
        {
            Toast.MakeText(this,menuItems[itemPosition],ToastLength.Short).Show();
            return false;
        }
    }
}

Main.axml

Here’s our empty layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</LinearLayout>

Best Regards.

Categorized in: