0

I'm creating a ToggleButton dynamically in a GridView. I can show all the ToggleButton that I'm sending from the server. The only problem is that I can't set any margin to separate the ToggleButtons among them. I'm adding ToggleButtons programmatically.

I need to do it with the gridlayout.

How to set margin between ToggleButtons programmatically.

Class:

  for (int i = 0; i < arrayList.size(); i++) {
            final ToggleButton toogleBtn = new ToggleButton (RegisterThreeActivityNew.this);
            toogleBtn.setPadding(50,10,30,20);
            toogleBtn.setWidth(140);
            toogleBtn.setHeight(20);
            toogleBtn.setTextSize(12);
            toogleBtn.setText(arrayList.get(i).getName());
            toogleBtn.setTextOff(arrayList.get(i).getName());
            toogleBtn.setTextOn(arrayList.get(i).getName());
            toogleBtn.setTextColor(ResourcesCompat.getColor(getResources(), R.color.white, null));
            toogleBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_category, null));

            final long id = arrayList.get(i).getId();
            toogleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        toggleEnabled(toogleBtn);
                        categories.add(id);
                    } else {
                        toggleDissabled(toogleBtn);
                        categories.remove(id);
                    }
                }
            });
            gridLayout.addView(toogleBtn);
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="@style/screen"
    android:id="@+id/ly_general"
    android:fitsSystemWindows="true"
    android:background="@color/colorPrimary">



<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        style="@style/screen"
        android:id="@+id/scroll">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/reg_three_grid_Category"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:columnCount="3"
            android:layout_marginTop="@dimen/padding_40">
        </GridLayout>
</ScrollView>

        <include layout="@layout/footer"
        android:id="@+id/include"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="134dp"
        android:layout_height="45dp"
        android:src="@drawable/logo"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/text_init_session"
        android:layout_alignStart="@+id/text_init_session" />
</RelativeLayout>
S.P.
  • 2,274
  • 4
  • 26
  • 57
  • Possible duplicate of [Set margins in a LinearLayout programmatically](http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically) – ashkhn Jan 10 '17 at 09:40
  • Is not the same problem. I need it in GridLayout and this solution don't work for me – S.P. Jan 10 '17 at 12:02
  • Yes it is.. Instead of `LinearLayout.LayoutParams` you need to use `GridLayout.LayoutParams`.. – ashkhn Jan 10 '17 at 13:22

0 Answers0