0

I am trying to achieve a dynamic list of textviews like in the image below :- enter image description here

Here is my code :-

        LayerDrawable dashboardResShape_community= (LayerDrawable) getResources().getDrawable(R.drawable.upcomingtask_tags_shape);
        // The background effect is by the layer list drawable from the above code

        LinearLayout tags_view2=(LinearLayout)findViewById(R.id.tags_view);
        LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT);
        lp.setMargins(10, 2, 2, 2);
        TextView[] tx = new TextView[15];

        for(int i=0; i<15; i++) {
            tx[i] = new TextView(getActivity());
            tx[i].setPadding(8, 4, 8, 4);
            tx[i].setBackground(dashboardResShape_community);
            tx[i].setLayoutParams(lp);
            tx[i].setText("Tag"+i);
            tags_view2.addView(tx[i]);
        }

and in my xml there is only a linear layout :-

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tags_view"
    android:orientation="horizontal" >

</LinearLayout>

This is what i achieve :- enter image description here

When i am adding 15 textviews, only 8 are shown like below, the rest should come in the next line but they are not.

If i add more textviews, it goes out of screen but i want to add the textview in the second line when the first line is full. What i am doing wrong here?

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66

3 Answers3

0

Its LinearLayout's limitation. If you want the explained behavior than

  1. You have to make your own Layout/View refer this link or
  2. Impliment LinearLayout Horizontal orientation with wrapping children like this
Community
  • 1
  • 1
Tabrej Khan
  • 894
  • 6
  • 15
0

you cannot get more text views on next line after linear layout is filled( screen width ), you already the made linear layout orientation as horizontal. Better solution add one more linear layout or use relative (do some child count coding and set parameters). The best solution i prefer for u is table layout. Easier to code code and handle

Sush
  • 3,864
  • 2
  • 17
  • 35
0

What you can do is add as many textviews as will fit on the screen to your linearlayout, but then when a textview would go off the screen, you could add another linearlayout below the one that you already had, and then add on to that. You could keep doing that and you would end up with no textviews goind off the screen. You could also try using a gridview.

Here is what this layout looks like: http://developer.android.com/guide/topics/ui/layout/gridview.html And here is the documentation: http://developer.android.com/reference/android/widget/GridView.html

superuser
  • 731
  • 10
  • 28