0

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();

        }
6
  • What do you mean with "entire code is not working" ? Commented Feb 15, 2017 at 6:12
  • i have printed the value of id name and status once i started to call the linearlayout its throwing an exception. Commented Feb 15, 2017 at 9:58
  • i don't know how to create a button dynamically in linear layout please help me over this looking forward for a reply..!! Commented Feb 15, 2017 at 10:40
  • now the output of above program is "Excep". Commented Feb 17, 2017 at 7:37
  • user7379431 change Toast.MakeText(this, "Excep", ToastLength.Long).Show(); with this: Toast.MakeText (this, $"{e}", ToastLength.Long).Show (); and post what you get. Commented Feb 17, 2017 at 7:42

2 Answers 2

1

Doesn't look like you're adding it to your view as children.

Sign up to request clarification or add additional context in comments.

3 Comments

i don't know how to create a button dynamically in linear layout please help me over this looking forward for a reply..!!
Just add this line before your for loop ends. (Right after the Toast.MakeText(this, id, ToastLength.Long).Show(); }): linearLayout.AddView(button);
Try @apipineda's answer. Haven't tried it but it looks good to me.
0

Here's something:

    //You need a ViewGroup, you already have it:
    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");

        //You create the instance of your view in this case your Button
        Button button = new Button(this);
        button.Text = name;
        button.SetBackgroundColor(Android.Graphics.Color.Black);
        button.SetTextColor(Android.Graphics.Color.White);

        //define the button layout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);

        //in case you want to modify other properties of the LayoutParams you use the object
        layoutParams.BottomMargin = 5;

        //Assign the layoutParams to the Button.
        button.LayoutParameters = layoutParams;

        //If you want to do something with the buttons you create you add the handle here
        //button.Click += (sender, e) => DoSomething(id);

        //Add the button as a child of your ViewGroup
        linearLayout.AddView(button);
    }

And you are done. This should guide you.

3 Comments

Still I'm getting the same error. Actually the for loop is running in try{}. The output for the code is throwing the exception(catch{}).
@user7379431 But you haven't indicated the error you are receiving, with out this is kinda difficult to help you.
System.NotlmplementedException: The method or operation is not implemented. at VoltrentNetworks.login.FindViewByld[T] (System.Object myButton1) [0x00001] in C:\xamarin\VoltLogin\VoltLogin WoltLogin.Droid \login.cs:146 at VoltrentNetworks.login +<OnCreate>d__13.MoyeNext [0x001 c7] in C:\xamarin\VoltLogin\VoltLogin WoltLogin.Droid \login.cs:64

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.