I want to automatically generate buttons vertically with a bottom margin of 20px between buttons. I try to set margin with a LayoutParams object, without success.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regions_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="30dip"
android:orientation="vertical" >
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
...
for (Region region : regionsList) {
//create new button
Button button = new Button(mContext);
//set infos
int id = Integer.parseInt(Long.toString((Long) region.getId())); button.setId(id);
button.setText(region.getName() + "( " + region.getStores_nb() + " )");
//Layoutparams setting
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 20);
button.setLayoutParams(params);
myLinear.addView(button);
}
As you can see on image, there is no space between images. Someone knows why ? Thank you !
You can try this:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
layoutParams.bottomMargin += 20;
button.setLayoutParams(layoutParams);
Try to use LinearLayout.LayoutParams
instead of FrameLayout.LayoutParams
, as in your xml you are using LinearLayout
..
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With