My problem is that a multiline text works properly in my xml template. If the text is wider than the screen it wraps into the next line. But if I try the same in Java, the text won’t be wrapped:
I have the following xml template for testing my prospective layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<TableLayout
android:id="@+id/messageTable"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:stretchColumns="*"
android:clickable="false">
<TableRow
android:layout_width="fill_parent"
android:background="#777777"
android:textColor="#FFFFFF"
android:clickable="false">
<TextView
android:id="@+id/topic"
android:text="This is the topic"
android:textColor="#FFFFFF"
android:layout_width="fill_parent"
android:textSize="14sp"
android:paddingLeft="5sp"
android:paddingRight="5sp"
android:paddingTop="2sp"
android:paddingBottom="2sp"
android:textStyle="bold"
android:clickable="false"
/>
</TableRow>
<TableRow>
<TextView
android:text="Mo., 26.12.2011, 10:00 - This is some example text. It will be wrapped in view. This works."
android:textSize="14sp"
android:paddingLeft="5sp"
android:paddingRight="5sp"
android:paddingTop="2sp"
android:paddingBottom="2sp"
android:scrollHorizontally="false"
android:inputType="textMultiLine"
android:longClickable="false"
android:clickable="false"
android:layout_width="0sp"
/>
</TableRow>
.
.
.
Now I’d like to use parts of these xml in my Java code. The table rows should be generated dynamically, depending on the data I will receive in the real application. Now the xml of the real application looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<TableLayout
android:id="@+id/messageTable"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:stretchColumns="*"
android:clickable="false">
</TableLayout>
.
.
.
In code I use own types for my text rows, text topics and text bodies:
TopicTableRow:
public class TopicTableRow extends TableRow {
public TopicTableRow(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setBackgroundColor(Color.parseColor("#777777"));
setClickable(false);
}
}
TopicTextView:
public class TopicTextView extends TextView {
public final String TAG = "TopicTextView";
public TopicTextView(Context context) {
super(context);
PixelCalc pixelCalc = new PixelCalc(context, TAG);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setPadding(pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2), pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2));
setTextColor(Color.parseColor("#FFFFFF"));
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
setTypeface(Typeface.DEFAULT_BOLD);
setClickable(false);
}
}
and BodyTextView (which doesn’t wrap the text) looks like this:
public class BodyTextView extends TextView {
public final String TAG = "BodyTextView";
public BodyTextView(Context context) {
super(context);
PixelCalc pixelCalc = new PixelCalc(context, TAG);
setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));
setPadding(pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2), pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2));
setClickable(false);
setLongClickable(false);
setHorizontallyScrolling(false);
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
}
}
(As you can see, the width settings are exactly the same as in the template XML).
In Java code I add a topic followed by a “body”. These are the methods. (Everything works fine but the text is not wrapped.):
private void addTopic(String text) {
TopicTableRow topicTableRow = new TopicTableRow(this);
TopicTextView topicTextView = new TopicTextView(this);
topicTextView.setText(text);
topicTableRow.addView(topicTextView);
messageTable.addView(topicTableRow);
}
private void addStandardText() {
TableRow tableRow = new TableRow(this);
BodyTextView bodyTextView = new BodyTextView(this);
bodyTextView.setText(getString(R.string.standardText));
tableRow.addView(bodyTextView);
messageTable.addView(tableRow);
}
I have the suspicion that the line setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT)); of BodyTextView doesn’t work properly because when I set layoutWidth to 100 or 200, the width won’t be minimized and protrudes out of the screen. Why is this so? If I try shortening the topic to 200 (TopicTextView), I get a smaller bar, so the Java method should work properly, but in BodyTextView it somehow ignores the width. Does anybody have any idea why this is so? I already thought about this bug but I’m not sure.
Solved:
I don’t know why but…
setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
instead of
setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
does the job.