Android: force texview for content wrapping

I have a tableview where new rows are created and their layouts manually. Each line has three main elements: an image, a text image for the title, and text for the description. To move them to where I want, I use two linear layouts. I want the textview for the description (called descView) to display the text in two lines, but instead it displays only one. What I did wrong or which method to call, so the view shows the text in two lines. It would be ideal if he wrapped the text with a word (not char).

Screenshot of what I have now:

enter image description here

Code for creating a new line ("New" is an object in which 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;
}
+3
3

, . , , , .

0

, , - , 2 LinearLayouts RelativeLayout , :

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

, TextView FILL_PARENT WRAP_CONTENT.

RelativeLayout:

ImageView ALIGN_PARENT_LEFT, x-dip .

TitleView ImageView.

TextView ImageView .

, , , ( ) TextView , :

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

, , , , , NPE .

, RelativeLayout LinearLayout .

, ~

+1

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


Add Layout options to TableRow:

tr.setLayoutParams(new LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
       TableLayout.LayoutParams.WRAP_CONTENT));
0
source

All Articles