Define padding in a view, programmatically.
To define the padding of a button programmatically, it is done using the method setPadding () , if you check the documentation, it indicates that the integer values that are defined as arguments are:
setPadding (left padding, top padding , right padding, bottom padding)
This is an example:
Button mybutton = new Button(getApplicationContext());
//mybutton.setPadding(left, top, right, bottom);
mybutton.setPadding(10,20,10,20);
Define margin in a view, programmatically.
To define a margin progamatically, it is done by defining a LayoutParams
, in which you can define margin values similar to setPadding()
, example:
Button mybutton = new Button(getApplicationContext());
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
//params.setMargins(left, top, right, bottom);
params.setMargins(10, 20, 10, 20);
mybutton.setLayoutParams(params);
Documentation:
Size, padding and margins