user2403534
user2403534

Reputation: 1

Android: EditText in TableRow does not wrap when added by code

I'm stuck with the following:

Trying to place a left-aligned multiline EditText to the left of a ToggleButton for each row of a table.

Layout XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:ems="10"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:inputType="none"
            android:text="A very long line that should be wrapped into two or more lines as it doesn't fit on one line" />

        <ToggleButton
            android:id="@+id/toggleButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />

    </TableRow>

</TableLayout>

</LinearLayout>

This produces exactly what I'm looking for (Example: http://s8.postimg.org/6ldgsovr9/before.png).

But then I try to add a new row with this code:

    TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);

    TableRow row = new TableRow(this);
    row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    row.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

    EditText et = new EditText(this);
    et.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

    TableRow.LayoutParams params = (TableRow.LayoutParams) et.getLayoutParams();
    params.height = TableRow.LayoutParams.WRAP_CONTENT;
    et.setLayoutParams(params);

    et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    et.setBackgroundColor(Color.TRANSPARENT);
    et.setText("A very long line that should be wrapped into two ore more lines as it doesn't fit on one line");

    row.addView(et);

    ToggleButton tb = new ToggleButton (this);
    row.addView(tb);

    tl.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

Which messes up the existing line and the new line does not wrap at all (Example: http://s24.postimg.org/3ty1ne71x/after.png)

What am I missing here?

Thanks!

Upvotes: 0

Views: 649

Answers (4)

praveenb
praveenb

Reputation: 10659

I face similar issue... Though its late reply, Just posting so it may help somebody...

I solved issue by adding these attributes to TableLayout

android:stretchColumns="0" 
android:shrinkColumns="0"

and also set inputType for EditText

android:inputType="textMultiLine"

Upvotes: 1

user2403534
user2403534

Reputation: 1

It looks like the following line solves the display issue:

et.setEms(10);

I don't know why this makes a difference (the font size stays the same) but now the text is aligned correctly.

Upvotes: 0

Jeff Lee
Jeff Lee

Reputation: 781

Hope I can help you :D

If you want to set Multline on XML. Add this betwween TextView Tag:

android:maxLines="4"
android:lines="4"

If you want to set it on run time. Use this.

mTextView.setMaxLines(maxlines)
mTextView.setLines(lines)

Upvotes: 0

user740797
user740797

Reputation: 131

Try commenting the following line. //et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

Upvotes: 0

Related Questions