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>
we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget.
Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With