Android - How to set margins programmatically in gridlayout

Ray Tayek picture Ray Tayek · Nov 22, 2015 · Viewed 8.9k times · Source

Trying to get a few rows of buttons in a grid layout.

The buttons size ok, but the margins seems broken.

package ray.gui;
import android.app.Activity;
import android.content.pm.*;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        int size=150;
        int rows=2;
        int columns=5;
        Integer[] colors=new Integer[columns];
        colors[0]=Color.RED;
        colors[1]=Color.YELLOW;
        colors[2]=Color.GREEN;
        colors[3]=Color.BLUE;
        colors[4]=Color.rgb(255,165,0);
        GridLayout gridLayout=new GridLayout(this);
        gridLayout.setRowCount(rows);
        gridLayout.setColumnCount(columns);
        GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams();
        layoutParams.setMargins(size*30/100,size*30/100,size*30/100,size*30/100);
        gridLayout.setLayoutParams(layoutParams);
        for(int i=0;i<rows*columns;i++) {
            Button button=new Button(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size,size);
            params.width = size;
            params.height= size;
            button.setLayoutParams(params);
            button.setText(""+i);
            //button.setLayoutParams(layoutParams);
            if(i/columns%2==1)
                button.setBackgroundColor(colors[i%columns]);
            gridLayout.addView(button);
        }
        setContentView(gridLayout);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
}

edit: the code below worked.

    Button button=new Button(this);
    GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams();
    layoutParams.setMargins(size*20/100,size*20/100,size*20/100,size*20/100);
    layoutParams.width=size;
    layoutParams.height=size;
    button.setLayoutParams(layoutParams);

Answer

Xiaoyu Yu picture Xiaoyu Yu · Nov 22, 2015

A view's layoutParams is for its parent to use, not itself. So a gridLayout's layoutParams shouldn't be a GridLayout.LayoutParams because its parent is not a GridLayout.

Try replacing

    GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams();
    layoutParams.setMargins(size*30/100,size*30/100,size*30/100,size*30/100);

with

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout
            .LayoutParams.WRAP_CONTENT);
    layoutParams.leftMargin = 45;
    layoutParams.rightMargin = 45;
    layoutParams.topMargin = 45;
    layoutParams.bottomMargin = 45;