Rabu, 24 Agustus 2016

Create Simple Menu in Android Using Visual Studio

To create simple menu or navigation on android is easy. You must add few code something like this.
Add this code to mymenu.xml.
mymenu.xml code:
 
<?xml version="1.0" encoding="utf-8" ?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/home"
        android:title="Beranda"/>
  <item android:id="@+id/kategori"
        android:title="Kategori"/>
  <item android:id="@+id/setting"
        android:title="Setting"/>
</menu>

And then put this into your mainactivity.cs
Mainactivity.cs code:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Simple_Menu
{
   [Activity(Label = "Simple_Menu", MainLauncher = true, Icon = "@drawable/icon")]


   //Bagian utama
   public class MainActivity : Activity
   {
      //Buat menu
      public override bool OnCreateOptionsMenu(IMenu menu)
      {
         MenuInflater.Inflate(Resource.Menu.mymenu, menu);
         return base.OnCreateOptionsMenu(menu);
      }
      //Menu Kita di Klik
      public override bool OnOptionsItemSelected(IMenuItem item)
      {
         switch (item.ItemId)
         {
            case Resource.Id.home:
               Button Homeact = FindViewById<Button>(Resource.Id.MyButton);
               Homeact.Click += delegate { Homeact.Text = string.Format("{0} clicks!", count++); };
               //Kode aksi or Action code
               return true;
            case Resource.Id.kategori:
               //Kode aksi or Action code
               return true;
            case Resource.Id.setting:
               //Kode aksi or Action code
               return true;
         }

         return base.OnOptionsItemSelected(item);
      }

      int count = 1;
      protected override void OnCreate(Bundle bundle)
      {
         base.OnCreate(bundle);

         // Set our view from the "main" layout resource
         SetContentView(Resource.Layout.Main);

         // Get our button from the layout resource,
         // and attach an event to it
         Button button = FindViewById<Button>(Resource.Id.MyButton);

         button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
      }
   }
}

For complete tutorial you can download project or see the video below. Download Project:
Download Project
Lihat Video:

0 komentar:

Posting Komentar

Kita bukanlah apa-apa tanpaNya, jika ada pertanyaan mengenai artikel silakan tinggalkan komentar anda melalui form komentar :)

SkyArs Corp. © 2016. Tweaked by: Soni Sitez Powered by: Blogger