Friday, October 25, 2013

PopupWindow in Monodroid


Main.axml:
------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello" />
</LinearLayout>

Activity1.cs:
-------------

using System;

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

namespace ProgressBarPopup
{
    [Activity(Label = "ProgressBarPopup", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;
        PopupWindow popUp;
        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 += new EventHandler(button_Click);
        }

        void button_Click(object sender, EventArgs e)
        {
            PopupWindow popUp;
            popUp = new PopupWindow(this);
            LinearLayout mainLayout;
            mainLayout = new LinearLayout(this);
            ImageView img;
            img = new ImageView(this);
            img.SetImageResource(Resource.Drawable.loading);
            mainLayout.SetBackgroundColor(Android.Graphics.Color.Transparent);
            mainLayout.SetGravity(GravityFlags.Center);
            Android.Widget.LinearLayout.LayoutParams param = new Android.Widget.LinearLayout.LayoutParams(Android.Widget.LinearLayout.LayoutParams.WrapContent, Android.Widget.LinearLayout.LayoutParams.WrapContent);
            mainLayout.Orientation = Orientation.Vertical;
            mainLayout.AddView(img, param);
            popUp.ContentView = mainLayout;
            popUp.ShowAtLocation(mainLayout, GravityFlags.Center, 0, 0);
            Display d = this.WindowManager.DefaultDisplay;
            popUp.Update(d.Width, d.Height);
        }
    }
}