-1

I'm trying to programmatically add many TextView to a RelativeLayout but I am unable to do that when TextView reach the end of the display right next TextView inflate in a new line.

RelativeLayout:

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:id="@+id/tag_cloud"
     android:padding="10dp">
 </RelativeLayout>

Code:

if (categoriesCursor.moveToFirst()){
            do {
                TextView tagElement = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
                tagElement.setText(categoriesCursor.getString(2));
                LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                llp.setMargins(0, 0, pixels, pixels); // llp.setMargins(left, top, right, bottom);
                tagElement.setLayoutParams(llp);
                tagCloudLayout.addView(tagElement);
            } while (categoriesCursor.moveToNext());
        }

tag.xml

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:textColor="#ff000000"
    android:textStyle="bold"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"/>

Thanks

PauAr
  • 1
  • 2
  • If you use `RelativeLayout` your views overlap with each other while adding. So I suggest use `LinearLayout` orientation vertical. – Bharatesh May 12 '15 at 12:35

3 Answers3

0

It´s not really clear what You are asking, but If I understand You the right way, You want to have only one line with textViews, or? Also, You are using wrong Layout Params, if You want to add some views to a relativeLayout side by side, I think You will get it with:

     RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f);

Where the last parameter stands for the layout weight attribute. With LayoutWeight and MATCH_PARENT, all views will be drawn with equal size.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

Consider using a ListView which contains TextView, and that way you'll be able to take advantage of cell re-use etc. Here's a good tutorial

Zain
  • 2,336
  • 1
  • 16
  • 25
0

Finally I put one LinearLayout vertical oriented and inside LinearLayouts horizontal oriented with three TextView inside. Isn't the best solution but it works for me.

PauAr
  • 1
  • 2