Atto Allas
Atto Allas

Reputation: 610

TextView not ellipsizing in LinearLayout inside TableRow

I have tried to make my TextViews ellipsize (they are located in a LinearLayout inside a TableRow), however, they do not ellipsize correctly unless I manually set the maxEms (which I do not want to do). Note: the entire xml (apart from the TableLayout) is generated programmatically.

I have tried most fixes: messing about (a bit) with the layout parameters, changing setMaxLines to setSingleLine, among other fixes but I still do apologise if the answer is staring me in the face!

Code for generation:

TableLayout tableLayout = (TableLayout) view.findViewById(R.id.table);

    // Add text
    for (int iter=0;iter<DisplayJson.summaryData.tasks.length;iter++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(16,16,16,16);

        LinearLayout container = new LinearLayout(this);
        container.setOrientation(LinearLayout.VERTICAL);

        TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);

        TableRow.LayoutParams layoutLayoutParams = new TableRow.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f);

        TextView titleView = new TextView(this);
        titleView.setText(DisplayJson.summaryData.tasks[iter].get("title"));
        titleView.setEllipsize(TextUtils.TruncateAt.END);
        titleView.setMaxLines(1);
        //titleView.setMaxEms(22); I do NOT want to set this (but setting it DOES work as expected)
        titleView.setLayoutParams(layoutParams);

        TextView classView = new TextView(this);
        classView.setText(DisplayJson.summaryData.tasks[iter].get("class"));
        classView.setLayoutParams(layoutParams);

        container.addView(titleView);
        container.addView(classView);
        container.setLayoutParams(layoutLayoutParams);

        tableRow.addView(container);

        setOnClick(tableRow, iter); //Custom function which does not affect the problem
        tableRow.setLayoutParams(rowParams);

        tableLayout.addView(tableRow);
    }

    setContentView(view);

TableLayout xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="1"></TableLayout>

I should note that before I added the LinearLayout and the 'classView' TextView, the ellipsizing was working correctly (may have been slightly different code). Also, only the titleView TextView needs to ellipsize, the classView one should not.

Any help would be greatly appreciated, and I should note that none of this code HAS to be like this, and can be changed as long as it has the same output.

Upvotes: 0

Views: 117

Answers (1)

Cheticamp
Cheticamp

Reputation: 62841

If you look at the output of the layout inspector in Android Studio, you will see that your titleView is blowing out the width of the table row. Change the layout parameters for linear layout to give it a weight as follows and you will see the ellipsis appear:

TableRow.LayoutParams layoutLayoutParams = new TableRow.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT,
        1.0f);

I hope this helps.

Upvotes: 1

Related Questions