• 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

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 ;-)
 
Thanks for the reply. I tried your suggestion however it didn't work. What happens when I try it is all the text disappears and the textview becomes very tall. Very strange. Any ideas?
 
Kinda makes sense, even though using the LayoutParams should work, I guess...

I'll look more into it tonight (Danish time), and see what I can find out
 
Actually I'm trying to create a list of items with a checkbox next to each within a table layout which I have done, I just can't get the textview to wrap to the screen and not cut off my text
 
So, if I read that the right way, you are actually not trying to wrap the text but rather to scale it to fit inside a certain area?
 
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
 
Ok...

So, the WRAP_CONTENT makes the TextView big enough for the text inside it, even if this means letting the TextView get bigger than it's parent, resulting in text cut-offs. In your case it is the height of the TextView that needs to be set up to wrap the content inside, so it will make extra lines as more text is added.
 
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... :(
 
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.
 
Back
Top Bottom