Tuesday, 28 August 2018

Xamarin Popup Menu

https://www.sonjukta.com/css-range-input-generator.phpXamarin has several options for menu. Side menu, Popup menu etc. In this article will discuss about the popup menu, how a popup menu can be create and how we will use this menu for real life programming purpose. Popup menu can be populated in two ways, from XML file or it be populated from dynamically via program.

Popup menu  programming from XML file

1)First create XML file,in the menu folder and Resource.


Resources>>menu>>Popup_menu.xml

Now add code for menu and item. 
xml version = "1.0" encoding="utf-8"?>
    <menu xmlns:android = "http://schemas.android.com/apk/res/android">
       <item
       android:id = "@+id/file_settings"
       android:title = "Settings"
        android:showAsAction = "ifRoom">

       <item
        android:id = "@+id/new_game1"
        android:title = "New File Settings"/>

       <item
        android:id = "@+id/help"
         android:title = "Help" />

       <item
        android:id = "@+id/about_app"
         android:title = "About app"/>
       </item>
</menu>

2)In the Layout .axml file , add a button.The code and the output is.


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

<Button
         android:id="@+id/BtnLogin"
         android:layout_width="100dp"
         android:layout_height="45dp"
         android:text="Open Popup"
         android:background="#F18D00"
         android:gravity="center" />
</LinearLayout>





3)In activity ,Place this code.Button click will open the Popup menu.You have to mention your xml file in the resource menu.

 

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Runtime;

namespace App6
{
             [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
           public class MainActivity : AppCompatActivity
           {

              protected override void OnCreate(Bundle savedInstanceState)
                 {
                   base.OnCreate(savedInstanceState);

                    SetContentView(Resource.Layout.activity_main);


                                   Button objMenu = FindViewById

                                   objMenu.Click += (s1, arg1) =>
                    {

                                        PopupMenu menu = new PopupMenu(this, objMenu);
                                        menu.Inflate(Resource.Menu.Popup_menu);

                                         menu.Show();

                                   };

                           }

        }
}

Output :




 



Popup menu dynamically populate

A popup menu can be dynamically populated by programming also. 
First we have to declare a Popup menu variable, then add menu item to it . Each menu have a item it entities corresponding text to display.We have added a button to display the menu.

Layout Page

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

<Button
android:id="@+id/BtnLogin"
android:layout_width="100dp"
android:layout_height="45dp"
android:text="Open Popup"
android:background="#F18D00"
android:gravity="center" />
</LinearLayout>

Activity.cs



Button objMenu = FindViewById

     objMenu.Click += (s1, arg1) => 
   {

          PopupMenu menu = new PopupMenu(this, objMenu);

          menu.Menu.Clear();

           menu.Menu.Add("New Menu 1");
           menu.Menu.Add("New Menu 2");
           menu.Menu.Add("New Menu 3");
           menu.Menu.Add("New Menu 4");

           menu.Show();
};


Now, we have learn how to populate a Menu. The next text  is how to handle Menu click event. From Menu click event you have to move forward the layout you want to display. Because in real life software, each menu item display unique layout. Here is the example how menu click event can be handled.


PopupMenu menu = new PopupMenu(ctts, menuObject);
 
Android.Content.Intent i;

menu.MenuItemClick += (s1, arg1) =>
{
           Android.Content.Intent i;

          switch (arg1.Item.ItemId)
         {
              case 0:
                   i = new Android.Content.Intent(ctts, typeof(logout_activity));
                  this.StartActivity(i);
              break;
                default:
                    //do a different thing
              break;
          }

};

In the above example , menu clicked was handled.Menu click transfer layout from one layout to another layout.

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...