A
A
Andreey2018-11-09 21:17:54
Android
Andreey, 2018-11-09 21:17:54

How to add padding between buttons when created from code in Android?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GridLayout gridLayout = new GridLayout(this);
        gridLayout.setRowCount(5);
        gridLayout.setColumnCount(7);
    setContentView(gridLayout);

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
        layoutParams.setMargins(10,10,10,10);

    for(int i=1;i<31;i++) {
        Button button = new Button(this);
            button.setText(String.valueOf(i));
            button.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
            button.setBackgroundResource(R.drawable.button);
        gridLayout.addView(button,layoutParams);
    }


}
}

5be5c8f8df0f9994040427.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2018-11-12
@andreey

GridLayout should use GridLayout.LayoutParams and not FrameLayout.LayoutParams

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question