Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question