Search code examples
androidxamarin.androidandroid-gridlayout

Column Span of GridLayout is not working in Xamarin.android


I have a created a GridLayout dynamically specifying its rows and column, after that i am trying to add a Button inside it which is also created dynamically.

The problem is, i am unable to fit the Button inside 2 column span. Its filled in only one column, even when i am giving the column span as 2.

Below is the code which i tried. Please help, i am stuck.

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    
        RelativeLayout mainlayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutparameter = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, 
        RelativeLayout.LayoutParams.MatchParent);
        mainlayout.LayoutParameters = layoutparameter;
    
        GridLayout gl = getView();
    
        mainlayout.AddView(gl);
    
        SetContentView(mainlayout);
    }

 private GridLayout getView()
        {

            GridLayout gridlayout = new GridLayout(this);

            Button b = new Button(this);
            b.Text = "Button1";


            var p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,
            ViewGroup.LayoutParams.MatchParent);
            gridlayout.LayoutParameters = p;

            gridlayout.RowCount = 2;
            gridlayout.ColumnCount = 2;

            addViewToGridLayout(gridlayout, b, 0, 1, 0, 2);// Here the column span given as 2.

            return gridlayout;
        }
    
 private void addViewToGridLayout(GridLayout pGridLayout, View view, int row, int pRowSpan, int column, int pColumnSpan)
        {
            Spec rowSpan = GridLayout.InvokeSpec(row, pRowSpan);
            Spec colspan = GridLayout.InvokeSpec(column, pColumnSpan);
            GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                    rowSpan, colspan);
            pGridLayout.AddView(view, gridParam);
        }

Solution

  • The code could fit the Button inside 2 column span. But, if there is nothing in the column or row, it does not make change.

    Make the change with getView method.

     private GridLayout getView()
        {
    
            GridLayout gridlayout = new GridLayout(this);
    
            TextView textView1 = new TextView(this);
            textView1.Text = "csll0";
    
            TextView textView2 = new TextView(this);
            textView2.Text = "csll1";
    
            TextView textView3 = new TextView(this);
            textView3.Text = "csll3";
    
            TextView textView4 = new TextView(this);
            textView4.Text = "csll4";
    
            Button b = new Button(this);
            b.Text = "Button1";
    
    
            var p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,
            ViewGroup.LayoutParams.MatchParent);
    
            gridlayout.LayoutParameters = p;
    
            gridlayout.RowCount = 3;
            gridlayout.ColumnCount = 2;
    
            addViewToGridLayout(gridlayout, textView1, 0, 1, 0, 1);
            addViewToGridLayout(gridlayout, textView2, 0, 1, 1, 1); 
            addViewToGridLayout(gridlayout, textView3, 1, 1, 0, 1); 
            addViewToGridLayout(gridlayout, textView4, 1, 1, 1, 1);
            addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.
    
    
            return gridlayout;
        }
    

    Set the column span to 1.

     addViewToGridLayout(gridlayout, b, 2, 1, 0, 1); 
    

    enter image description here

    Set the column span to 2.

     addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.
    

    enter image description here