• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Set TextView Width To WRAP_CONTENT in code

crazyfool_1

Newbie
Jan 25, 2011
12
0
hey guys, I am trying to dev my first android app and its going well except for one minor niggle. I have a table which contains a check box and textview per row however the text within the textview goes off the screen to the right. I know this is an issue which can be solved by wrapping the width but I cant work out how to do it in code and not xml.
This is what I have so far.
Code:
for(int count = 0;count < 15; count++)
        {
            tr = new TableRow(this);
            tr.setId(count);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));   
            tr.setBackgroundResource(R.color.redColor);
            
            check = new CheckBox(this);
            check.setPadding(5, 5, 5, 5);
            check.setId(count);
            
            text = new TextView(this);
            text.setId(count);
            text.setText("This is example text which goes off the screen!!!");
            text.setPadding(5, 0, 5, 0);
            text.setHorizontallyScrolling(false);
            //text.setBackgroundResource(R.color.redColor);
            
            tr.addView(check);
            tr.addView(text);
            
            table.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
    }
        scroll.addView(table);
        setContentView(scroll);
So can anyone help me work out how to wrap the textview to keep the text on the screen? Thanks.
Btw if this is a noobish question be kind as I am new at this :)
 
I haven't tried this, but a short look around the API suggests that you could use the sam WRAP_CONTENT value as in the XML, and in your example when you set the LayoutParams of the Table.

So my guess is that you use:

Code:
text.setWidth(LayoutParams.WRAP_CONTENT);

TextView.setWidth(int)
LayoutParams.WRAP_CONTENT

As it says in the API, LayoutParams.WRAP_CONTENT is the int -2, so you should also be able to just set the width to -2, but using the static makes it more readable, so you don't have to remember what -2 is.

Try that, and let us know if it works out ;-)
 
  • Like
Reactions: crazyfool_1
Upvote 0
basically this is what I have atm:

X Some text which is getting cut o
X Some text which is getting cut o

The 'X' is the check box and those are two rows in the table layout, as you can see the text gets cuts off, what I want is how it looks below:

X Some text which isn't getting
cut off here
X Some text which isn't getting
cut off here

So instead of having the text cut off it move down to the next line. I was under the impression to get this to happen I need to wrap the text? Or am I mistaken? :S
 
Upvote 0
ok this is what i have
tr = new TableRow(this);
tr.setId(count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setBackgroundResource(R.color.redColor);

check = new CheckBox(this);
check.setPadding(5, 5, 5, 5);
check.setId(count);

text = new TextView(this);
text.setId(count);
text.setText("random");
text.setPadding(5, 0, 5, 0);
text.setHorizontallyScrolling(false);
text.setOnClickListener(homeListener());
text.setWidth(LayoutParams.WRAP_CONTENT);

tr.addView(check);
tr.addView(text);

table.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
scroll.addView(table);
setContentView(scroll);
this give me very tall textview boxes (about 5 lines high) with no text in them

if i change
text.setWidth(LayoutParams.WRAP_CONTENT);
to
text.setHeight(LayoutParams.WRAP_CONTENT);
I get normal height (1 line) boxes with no text in them... :(
 
Upvote 0
The part causing your problem is, I believe the TableLayout.

I would look into trying to set the shrinkColumns property.

I'm not sure how to do this in Java. In XML it would be
Code:
<TableLayout android:shrinkColumns="0">
For the first column or
Code:
<TableLayout android:shrinkColumns="*">
for all columns.

Without this setting the table columns expand off the screen to fit the content.
Adding the setting should stop this from happening allowing your text wrap inside your TextView.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones