Question

I am trying to use GridLayout programmatically.

Here is my code:

    GridLayout p = new GridLayout(this);
    p.setRowCount(20);
    p.setColumnCount(20);
    View c = new View(this);
    c.setBackgroundColor(this.getResources().getColor(R.color.darkbluetheme));
    Spec specusata = android.support.v7.widget.GridLayout.spec(1,2);
    Spec specusata2 = android.support.v7.widget.GridLayout.spec(1,2);
    p.addView(c,new LayoutParams(specusata,specusata2));
    setContentView(p);

Here is the result I get:

enter image description here

While i was expecting something like this:

enter image description here

Please what am I doing wrong???

Thanks !!!

EDIT EDIT EDIT

This happens even if i set the LayoutParams of the View to WRAP_CONTENT:

    GridLayout p = new GridLayout(this);
    p.setRowCount(20);
    p.setColumnCount(20);
    View c = new View(DayActivity.this);
    c.setBackgroundColor(this.getResources().getColor(R.color.darkbluetheme));
    Spec specusata = android.support.v7.widget.GridLayout.spec(1);
    Spec specusata2 = android.support.v7.widget.GridLayout.spec(1);
    c.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    p.addView(c,new android.support.v7.widget.GridLayout.LayoutParams(specusata,specusata2));
    setContentView(p);
Was it helpful?

Solution

IMHO: Your other rows and columns are empty, and thus zero width. The rest of your space is filled with that single cell, and takes all space.

Try adding more child views with other colors, and then you will understand what happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top