Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple TextViews into LinearLayout programmatically

There is long string. I am dividing long string into words and setting TextView for each word. (You can ask why I need this function? When user clicks on textview(word), application shows the meaning of the word)

...
    TextView tv = new TextView(this);
                tv.setTextSize(24);
                tv.setText(word);
                ll.addView(tv);
...

My LinearLayout:

<LinearLayout
        android:id="@+id/llReader"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical" >
    </LinearLayout>

If I put LinearLayout's orientation vertical, it is putting each TextView in new line. For example:

word1

word2

word3

If I put it in horizontal orientation, it is putting all words only in one line:

word 1, word 2, word 3

In result word4,word5, word6 is not visible.

How to add TextViews programmatically in this way?

enter image description here

like image 526
user3388473 Avatar asked Sep 17 '25 00:09

user3388473


1 Answers

You should add Horizontal LinearLayout in your Vertical LinearLayout:

<LinearLayout
    android:id="@+id/verticalLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/horizontalLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>
like image 66
The Badak Avatar answered Sep 19 '25 16:09

The Badak