How to set LayoutRules.Below with a dynamic ID?

GunbitGunbit DEMember ✭✭

`
RelativeLayout masterLayout = FindViewById (Resource.Id.relativeLayout1);

        for (int i = 0; i < 3; i++)
        {
            var valueA = new Button(this);
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams (341, RelativeLayout.LayoutParams.WrapContent);

            valueA.Text = "Test A" + i;
            valueA.Id = i;
            Console.WriteLine (i + " new Content ");
            if (i == 0) {
                btnParams.AddRule (LayoutRules.Below, Resource.Id.textLeft); //works fine


                masterLayout.AddView(valueA, btnParams);
            } else {
                btnParams.AddRule (LayoutRules.Below, i-1); // not working button is on the top 


                masterLayout.AddView(valueA, btnParams);
            }

`

Add a screen to show the Problem.

Best Answer

Answers

  • BatzeeBatzee LKMember ✭✭

    I don't know why you are using i-1, but when adding an Id of a Dynamic element use the element object name.Id

    Replace this
    btnParams.AddRule (LayoutRules.Below, i-1);

    With
    btnParams.AddRule (LayoutRules.Below, valueA.Id);

  • GunbitGunbit DEMember ✭✭

    Hey Batzee,
    thx for the Answer.
    I use i-1 because i will set the ID form the previous button which i have add.
    I have change some code but it is the same.

    ` for (int i = 0; i < 3; i++)
    {
    var valueA = new Button(this);
    RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams (341, RelativeLayout.LayoutParams.WrapContent);
    var last_btn = 0;

                valueA.Text = "Test A" + i;
                valueA.Id = View.GenerateViewId();
                Console.WriteLine (i + " new Content ");
                if (i == 0) {
                    btnParams.AddRule (LayoutRules.Below, Resource.Id.textLeft);
    
                    last_btn = valueA.Id;
                    masterLayout.AddView(valueA, btnParams);
                } else {
                    btnParams.AddRule (LayoutRules.Below, last_btn);
    
    
                    last_btn = valueA.Id;
                    masterLayout.AddView(valueA, btnParams);
                }
    
            }`
    

    Can you Help me?

Sign In or Register to comment.