Xamarin Android ActionBar – Dropdown Menu

0
User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

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.

Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

thirty five − 31 =

Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart