Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make buttons programmatically using xamarin?

I'm trying to create buttons programmatically using the xamarin ide (c#). What code do I need to create the button, set its size, set its text, set its background color, and set its constraints? Is there a way to define the button as 1/4 of the width of the screen? Thanks in advance.

like image 233
parker88 Avatar asked Dec 05 '22 03:12

parker88


1 Answers

First create the button

UIButton button = new UIButton(); 
CGRect ScreenBounds = UIScreen.MainScreen.Bounds;
float buttonWidth = (float)ScreenBounds.X / 4;
button.Frame = new CGRect (0f, 0f, buttonWidth, 50f); 
button.SetTitle ("Title", UIControlState.Normal);
button.BackgroundColor = UIColor.Green;

And then add it as a subview to the active view

this.AddSubview (button);

And to add event for TouchUpInside

button.TouchUpInside += (object sender, System.EventArgs e) => {
   Debug.WriteLine( "Button Clicked!");
};
like image 138
iamIcarus Avatar answered Jan 06 '23 07:01

iamIcarus