I'm trying to create a button dynamically in xamarin c# according to number of data in database.After linear layout declaration entire code is not working.
try
{ var client = new System.Net.Http.HttpClient();
var response = await client.GetAsync("http://");
string jsonResponse = await response.Content.ReadAsStringAsync();
JSONArray a = new JSONArray(jsonResponse);
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout1);
for (int i = 0; i < a.Length(); i++)
{
JSONObject json = a.GetJSONObject(i);
String id = json.GetString("id");
String name = json.GetString("name");
String status = json.GetString("status");
// Toast.MakeText(this, id + name + status, ToastLength.Long).Show();
Button button = new Button(this);
button.Text = name;
button.SetBackgroundColor(Android.Graphics.Color.Black);
button.SetTextColor(Android.Graphics.Color.White);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
layoutParams.BottomMargin = 5;
button.LayoutParameters = layoutParams;
//Toast.MakeText(this, id, ToastLength.Long).Show();
linearLayout.AddView(button);
}
}
catch (Exception e)
{
Toast.MakeText(this, "Excep", ToastLength.Long).Show();
}
Toast.MakeText(this, "Excep", ToastLength.Long).Show();with this:Toast.MakeText (this, $"{e}", ToastLength.Long).Show ();and post what you get.