Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

AddSubview and Custom PageRenderer in Android

$
0
0

Hi there

I am writing a PageRenderer that simply adds a menu control to the bottom left of the page. Using iOS the renderer is easy because I can simply use View.AddSubview (button); and it works great.

In Android I don't have as much knowledge in the way that layouts are used etc but all I want to do is add the control (which is in an Xml file) to the page in the same way as iOS but I can't work out how... Here is where I have got to...

`public override void OnWindowFocusChanged (bool hasWindowFocus)
{
base.OnWindowFocusChanged (hasWindowFocus);
var activity = this.Context as Activity;

        if (!hasWindowFocus) {
            return;
        }

        activity.SetContentView (MyTestApp.Droid.Resource.Layout.Main);
        button = activity.FindViewById<SatelliteMenuButton> (MyTestApp.Droid.Resource.Id.satmenu);

        if (button != null) {
            button.AddItems (new [] {
                new SatelliteMenuButtonItem (1, MyTestApp.Droid.Resource.Drawable.icon1),
                new SatelliteMenuButtonItem (2, MyTestApp.Droid.Resource.Drawable.icon2),
                new SatelliteMenuButtonItem (3, MyTestApp.Droid.Resource.Drawable.icon3),
                new SatelliteMenuButtonItem (4, MyTestApp.Droid.Resource.Drawable.icon4),
                new SatelliteMenuButtonItem (5, MyTestApp.Droid.Resource.Drawable.icon5),
                new SatelliteMenuButtonItem (6, MyTestApp.Droid.Resource.Drawable.icon6)
            });

            button.MenuItemClick += (sender, e1) => {
                Console.WriteLine ("{0} selected", e1.MenuItem);
            };

            FrameLayout.LayoutParams pos = new FrameLayout.LayoutParams(20, 20);
            pos.LeftMargin = 50;
            pos.BottomMargin  = 50;

            AddView (button, pos);

        }
    }`

But I get an error that it the item already belongs to a parent which I understand because it is originating from an Xml Layout file. Can anyone help as I don't have as much expertise in the Android side of things!

Alex


Viewing all articles
Browse latest Browse all 77050

Trending Articles