1

dynamic button code:

final Button button = new Button(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
Resources resources = SecondActivity.this.getResources();  
Drawable drawable = resources.getDrawable(R.drawable.buttonpg);
button.setBackgroundDrawable(drawable);
button.setText(String.valueOf(i));
button.setText(" " + nameArray[i]);
button.setId(i);
button.setWidth(180);
button.setHeight(60);

button.setTextColor(Color.rgb(255,69,0));
button.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
button.setGravity(Gravity.CENTER);

how to add Margintop for this button ....

Thanks for your help....

AndyN
  • 1,742
  • 1
  • 15
  • 30
  • http://stackoverflow.com/questions/12728255/android-set-margin-programmatically-in-dp-on-button – baboo Mar 01 '13 at 11:15

3 Answers3

0

use the code

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);button.setLayoutParams(params);
Danny
  • 1,050
  • 3
  • 11
  • 26
0
final Button button = new Button(this);
LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,      
    LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
button.setLayoutParams(params);
duggu
  • 37,851
  • 12
  • 116
  • 113
0

You could create an instance of LinearLayout.LayoutParams, assign value for topMargin property and set that instance to button. Hope that help.

nlt
  • 555
  • 7
  • 11