3

I have tableview, where create new rows and set them layout manually. In every row there are 3 main elements: image, textview for title and textview for description. To move them where I want, I use two linear layouts. I want textview for description (named as descView) show text in two lines, but instead - it shows only one. What did I do wrong or what method to call, so view shows text in two lines. Would be perfect if it wraps text by word (not char).

Screenshot of what I have now:

enter image description here

Code of creating new row ('New' is Object where I save information (text, image, etc.)):

private TableRow formRow(New item) {

    //prepare table row parameters
    TableRow tr = new TableRow(this);
    tr.setBackgroundResource(R.drawable.results_table_row);
    tr.setVerticalGravity(Gravity.CENTER);
    tr.setTag(item.getTitle());
    tr.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_UP) {
                v.setBackgroundResource(R.drawable.results_table_row);

            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setBackgroundResource(R.drawable.results_table_row_touched);
            }
            return true;
        }
    });


    //create main layout for content
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.HORIZONTAL);

    //add image
    int imageID = Integer.parseInt(item.getPictureSrc());
    ImageView image = new ImageView(this);
    image.setImageResource(imageID);
    image.setPadding(5, 5, 5, 5);
    layout.addView(image);

    //add another layout for title and description
    LinearLayout descLayout = new LinearLayout(this);
    descLayout.setOrientation(LinearLayout.VERTICAL);
    descLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    //add title
    TextView titleView = new TextView(this);
    titleView.setText(item.getTitle());
    titleView.setPadding(5, 0, 0, 0);
    titleView.setTextAppearance(this, R.style.TableRowStyle_ChildTitle);
    titleView.setLines(1);
    descLayout.addView(titleView);

    //add description
    TextView descView = new TextView(this);
    descView.setText(item.getDescription());
    descView.setPadding(5, 0, 0, 0);
    descView.setTextAppearance(this, R.style.TableRowStyle_ChildText);
    descView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    descView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    //descView.setEllipsize(TextUtils.TruncateAt.END);
    descView.setLines(2);
    descLayout.addView(descView);

    //add description layout to main layout
    layout.addView(descLayout);

    //finally, add layout to row
    tr.addView(layout);

    return tr;
}
Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
  • According to this http://stackoverflow.com/questions/2197744/android-textview-text-not-getting-wrapped you need to set android:width="0dip" – amb Apr 26 '12 at 12:58
  • How to achieve that by code? I tried `descView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT)); `, but this time it doesn't even show any text – Paulius Vindzigelskis Apr 26 '12 at 13:24
  • It seems that if I set actual width (>0), then it shows text and wraps, but to width I set. How to set width which can get? – Paulius Vindzigelskis Apr 26 '12 at 13:37
  • I added this line `int widthForText = GlobalPrefs.getScreenWidth() - MAX_IMAGE_WIDTH - 10;` to compute width(screenWidth - imageWidth - padingAndOtherLoses). So now it works with your help – Paulius Vindzigelskis Apr 26 '12 at 13:54

3 Answers3

1

I have a similar setup like this for some of my applications, but personally, with something like that, I would scrap the 2 LinearLayouts and use a single RelativeLayout for this, and I would try getting rid of:

descView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
descView.setLines(2);

Then I would position the items relatively to each other and the sides with a Width of that TextView set to FILL_PARENT and its height to WRAP_CONTENT.

You could get a much more uniform appearance for that with a RelativeLayout:

Set the ImageView with ALIGN_PARENT_LEFT, give it a margin of x-dip on the right and left.

Then add your Title TextView and align it to the RIGHT of the ImageView.

Then add your Description TextView and align it to the RIGHT of the ImageView and BELOW the Title.

Since you're doing it through code though, I would go even one step further since it looks like they're both the same font style (title is just bolded) and make it a single TextView for both title/description, then just use:

 textView.setText(Html.fromHtml("<b>" + item.getTitle() + "</b><br>" + item.getDescription())); 

Just make sure you give everything a unique ID to link to, and don't align to something that hasn't been displayed yet or you'll throw a NPE or it just won't display right at all.

But yeah, I'll use a RelativeLayout for listing items with multiple components over a LinearLayout any day.

Hope it helps~

Cruceo
  • 6,763
  • 2
  • 31
  • 52
0

If your TableRow only has 1 column then I think setting android:shrinkColumns="0" in your TableLayout will help you to fix this.


Add Layout parameters to your TableRow :

tr.setLayoutParams(new LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
       TableLayout.LayoutParams.WRAP_CONTENT));
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • In code, i guess setting `setShrinkAllColumns(true)` to your TableLayout may help – waqaslam Apr 26 '12 at 12:59
  • it actually doesn't work, as I suspected, because rows show normally, but texview refuses to show all text. It shows only one line and cuts everything after that – Paulius Vindzigelskis Apr 26 '12 at 13:03
  • I think, you are looking wrong way. I tried that also. Also, I tried to setLines to textview, it creates those lines, but fills only first line, other is left blank. Now in screenshot is set two lines and as you see, there is only one filled – Paulius Vindzigelskis Apr 26 '12 at 13:14
0

I set max lines to some number and set characters length by calculating character to first dot. So now application know how much information to show and no such bug appear

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41