I'm working on a project where I have to create an search menu o find specific files, but i need do to create a dinamic search menu because every section has diferents fields of search, and my question is: How can I create EditTexts,Spinners,etc through code on the activity?
Asked
Active
Viewed 1,084 times
0
-
enter this in google: "xamarin android create elements programmatically" – Dbl May 22 '17 at 11:29
1 Answers
0
//create "reference to your layout"
LinearLayout yourFormLayout = FindViewById<LinearLayout(Resource.Id.Linear_MS);
//create container for parameters
var parameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
//add some parameters
param.SetMargins(20, 20, 20, 10);
//create new element
Button button = new Button(this);
button.Text = "click me";
button.SetBackgroundColor(Android.Graphics.Color.Black);
button.SetTextColor(Android.Graphics.Color.White);
button.LayoutParameters = parameters;
//add some events to your element
button.Click += (sender, e) => DoStuff();
//Add the button
yourFormLayout.AddView(button);
AddView adds element as a child to nearest parent, so if you want some more sofisticated location you'll have to read about for example 'GetDeclaredField'

Arkadiusz Raszeja
- 862
- 7
- 18