0

I am developing an Android app. In my app I am adding checkboxes programmatically to a LinearLayout. But after the checkboxes are added, it does not properly fit to the layout.

Screenshot:

enter image description here

As you can see in screenshot "x-samll" text and its checkbox are not fitted properly. What I want is both checkbox and its text together go to new line when there is not enough space. How can I achieve it?

This is how I programmatically add checkboxes:

if(attrs.length()>0)
                                {
                                    LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext());
                                    attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL);
                                    LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                                    attrControlsSubContainer.setLayoutParams(layoutParams);
                                    for(int i=0;i<attrs.length();i++)
                                    {
                                        CheckBox chkAttribute = new CheckBox(getBaseContext());
                                        chkAttribute.setText(attrs.getJSONObject(i).getString("name"));
                                        chkAttribute.setTextColor(Color.BLACK);
                                        chkAttribute.setId(attrs.getJSONObject(i).getInt("id"));
                                        attrControlsSubContainer.addView(chkAttribute);
                                    }
                                    attributeControlContainer.addView(attrControlsSubContainer);
                                }
halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Related [question](http://stackoverflow.com/questions/14528381/android-horizontal-linearlayout-wrap-elements) – OJ7 Mar 21 '16 at 16:40

2 Answers2

0

use below code:

if(attrs.length() > 0) {
      ScrollView mScrollView = new HorizontalScrollView(getApplicationContext());
      mScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      mScrollView.setFillViewport(true); 
      LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext());
      attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL);
      LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);                               attrControlsSubContainer.setLayoutParams(layoutParams);
      for(int i=0;i<attrs.length();i++)
      {
         CheckBox chkAttribute = new CheckBox(getBaseContext());
         chkAttribute.setText(attrs.getJSONObject(i).getString("name"));
         chkAttribute.setTextColor(Color.BLACK);
         chkAttribute.setId(attrs.getJSONObject(i).getInt("id"));
         attrControlsSubContainer.addView(chkAttribute);
                                    }
       mScrollView.addView(attrControlsSubContainer);
       attributeControlContainer.addView(mScrollView);  


 }
Haniyeh Khaksar
  • 774
  • 4
  • 20
0

LinearLayout is not enough for this you must use FlowLayout

<com.wefika.flowlayout.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start|top">


</com.wefika.flowlayout.FlowLayout>

Gradle Dependancy :- compile 'org.apmem.tools:layouts:1.10@aar'

Then add check box dynamically in FlowLayout

use FlowLayout.LayoutParams

 FlowLayout.LayoutParams params = new FlowLayout.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
N J
  • 27,217
  • 13
  • 76
  • 96