Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : set margin between buttons with LayoutParams

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 ! enter image description here

like image 320
johann Avatar asked Oct 26 '12 08:10

johann


2 Answers

You can try this:

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
layoutParams.bottomMargin += 20;
button.setLayoutParams(layoutParams);
like image 68
Cata Avatar answered Oct 15 '22 07:10

Cata


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);
like image 2
Nermeen Avatar answered Oct 15 '22 05:10

Nermeen