Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListView with multiple TextViews with different width.

Assuming I can add a TextView with a longer text anytime.

How can I get the max width from all the TextViews and set it to all the rest so that my text will be displayed in one line?

Example:

C++
Column1 | Column2 
text    | texttext 
text    | texttexttext


I want all the items from column2 to have the same width as texttexttext.

What I have tried:

I've tried to set a width to the TextView and also set android:singleLine="true", but if the text I enter, the input is longer than the width I set as an example if I have

texttexttext as an input
on the mobile screen I'll have texttex...
Posted
Comments
David Crow 12-Dec-17 21:46pm    
If the width of the first column is static, why not just have the width of the second column take up the remaining space?
grrigore 13-Dec-17 5:38am    
That has nothing to do with the first column. I'm sorry I didn't make it clear. I'll have an n number of columns with different text with different lengths. So, in my opinion I can't really set a width.
David Crow 13-Dec-17 10:25am    
"So, in my opinion I can't really set a width."

So then the "Setting textview's width..." part of this thread means what exactly?

What does the layout's XML file look like? What does the code look like that is adding items to the ListView?
grrigore 13-Dec-17 12:56pm    
Sorry for the confusion I created.
I mean I have like three columns let's say column1, column2, column3.

In the xml code I'll have a LinearLayout with a horizontal orientation in which I define something like a header. In this LinearLayout I'll have 3 textViews each containing a text with column1, column2 and so on..

So the layout will look like:

Column1 | Column2 | Column3
ListView with item composed by three textView.

After this LinearLayout I'll have a ListView with the same amount of columns as I said below.

I updated two xml files here https://github.com/grrigore/ListView

Now, I don't know how long the text stored in the ListView's items will be and I want each text having different lengths to be displayed fully.

Hope it's clear!


David Crow 13-Dec-17 13:23pm    
"Now, I don't know how long the text stored in the ListView's items will be and I want each text having different lengths to be displayed fully."

You do realize this will only work up until a certain point, right? At which point, long text will be truncated (with ellipses).

One solution, which may or may not go against your design, would be to change the three TextViews in your list_item.xml to have a layout_width of 0dp and a layout_weight of 1. This will make all three "columns" equal width (i.e., 0.33).

If that does not work, you can play around with the layout_weight values, something like:

<TextView android:id="@+id/text_column1"
          android:layout_width="0dp"
          android:layout_weight="0.25"
          android:layout_height="match_parent"
          android:text="Column1" />

<TextView android:id="@+id/text_column2"
          android:layout_width="0dp"
          android:layout_weight="0.37"
          android:layout_height="match_parent"
          android:text="Column2" />

<TextView android:id="@+id/text_column3"
          android:layout_width="0dp"
          android:layout_weight="0.37"
          android:layout_height="match_parent"
          android:text="Column3" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900