4

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

4 Answers 4

3

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>
Sign up to request clarification or add additional context in comments.

Comments

0

In your activity,

private LinearLayout mReaderLayout;

protected void onCreate(Bundle savedInstanceState) {
    mReaderLayout = (LinearLayout) findViewById(R.layout.llReader);
}

private void addText(String text) {
    TextView textView = new TextView(this);
    textView.setText(text);
    mReaderLayout.addView(textView);
}

1 Comment

First of all, thank you for your answer. But I asked how to add textviews into linearlayout in the way shown in picture: paragraph of TextViews
0

You can use gridview with adapter.

  <GridView
                    android:id="@+id/gridview1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:columnWidth="50dp"
                    android:gravity="center"
                    android:numColumns="auto_fit"
                    android:stretchMode="columnWidth" >

                </GridView>

and in onCreate:

    String[] strings= "Long string".split(" ");
       gridView = (GridView) findViewById(R.id.gridview1);  

              // Create adapter to set value for grid view
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, strings);

                gridView.setAdapter(adapter);

                gridView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                       Toast.makeText(getApplicationContext(),
                        ((TextView) v).getText()  , Toast.LENGTH_SHORT).show();

                    }
                });

Comments

0

You can add use a RelativeLayout right away or you can add a two buttons in a linear, vertical Layout and then add that into a horizontal layout. Then add the next vertical linear layout with the other two buttons into the horizontal layout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.