9

Any idea how I can force the textview to go to a new line once it runs out of space inside the view. The behaviour I want to happen is that without programatically findout out the size and forcing the newline I want it to happen by iteself.

In this code it forces the buttons off the screen.

<TableRow>
                <TextView android:layout_width="fill_parent"
                    android:paddingLeft="5dp" android:paddingRight="5dp"
                    android:singleLine="false" android:layout_height="wrap_content"
                    style="@style/heading1" android:gravity="left" android:id="@+id/store_address"></TextView>

                <TableLayout android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:gravity="right">
                    <TableRow>
                        <ImageView android:id="@+id/img_user" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/qrcode"
                            android:paddingBottom="2dp" />
                    </TableRow>
                    <TableRow>
                        <ImageView android:id="@+id/nfc" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/nfc" />
                    </TableRow>
                </TableLayout>
            </TableRow>
kgibbon
  • 726
  • 1
  • 15
  • 37
  • I cant see any buttons. But i guess your problem is that you set the width of the TextView to fill_parent. Breaking a new line is the default behaviour in android. But if your view fills the whole screen it will not break until it reaches the end of the screen. Make your textView small enough that there is still room for the "buttons". – pumpkee May 15 '11 at 06:51
  • I hardcoded the width to and it seems to word. I would rather not do that. – kgibbon May 15 '11 at 06:56
  • 1
    Take a look at layout weights. It lets you set how much percent of the parent view gets used by the child. – pumpkee May 15 '11 at 07:05

2 Answers2

10

add the following attribute to the TextView control:

android:maxWidth = "100dip" Here i have used 100dip, you can give whatever dimensions you want. Once the text exceeds the specified width, it will automatically be shifted to a new line.

user590849
  • 11,655
  • 27
  • 84
  • 125
1

This is not the most dynamic way to handle the problem, but I increased the Height of the TextView depending on how many lines were being drawn.

        // Create TextViews with Question
        TextView question = new TextView(this);

        if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 70)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 75, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 110)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 100, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 150)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 125, .9f));
        }

        question.setTextColor(Color.BLUE);
        question.setSingleLine(false);
        question.setEllipsize(TextUtils.TruncateAt.END);
        question.setMaxLines(5);
user3666197
  • 1
  • 6
  • 50
  • 92