user2182071
user2182071

Reputation: 35

Textview wrapping in Android

I have to display the Textview in Android getting from xml feed.

Let us take this is my Textview:

Mallika Sherawat's upcoming movie Dirty Politics

Here I have created the layout dynamically for these Textview:

LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 50);
textLayoutParams.gravity = Gravity.CENTER;

TextView tv = new TextView(this);
tv.setSingleLine(false);
tv.setEllipsize(null);
tv.setHorizontallyScrolling(false);
tv.setTextColor(Color.parseColor("#a7a9ac"));
tv.setText(Appscontent.Sub_arraylisttwo.get(j));
tv.setLayoutParams(textLayoutParams);
tv.setBackgroundColor(Color.parseColor("#414042"));

I have mentioned the width 50 here.

So the text is displayed well on small content Textview.some time I have more content Textview which means how can I display the Textview like below:

Mallika Sherawat's 
upcoming movie Dirty...

I mean the text is have to display maximum 2 lines only after the text is extended means simply add the ... content like above example. How can I do this?

Upvotes: 2

Views: 2082

Answers (4)

LomE999
LomE999

Reputation: 119

Try out be setting android:ems="10" it will make your edittext that much wide and wraps the text

Upvotes: 1

Nezam
Nezam

Reputation: 4132

You can try defining your text for the TextView in strings.xml.Then if you set from html then it may wrap according to screen size:

So you can try this out:

<string name="nice_html">
<![CDATA[Don&apos;t worry we will never share this with anyone<br/>
By Clicking register you are indicating that you have read and agreed to the <br/>
 <u>Terms of services</u>  and <u>Privacy Policy</u>
]]></string>

Then, in your code:

TextView foo = (TextView)findViewById(R.id.foo); foo.setText(Html.fromHtml(getString(R.string.nice_html)));

Upvotes: 0

Furqi
Furqi

Reputation: 2403

Try setting android:ellipsize to none for each TextView. For more details see documentation on this attribute.

From xml:

<TextView ... android:ellipsize="none" />

From code:

TextView textView = new TextView(context);
 ...
 textView.setEllipsize(null);

Upvotes: 0

liarspocker
liarspocker

Reputation: 2454

Try with:

tv.setMaxLines(2);
tv.setEllipsize(TextUtils.TruncateAt.END);

Upvotes: 5

Related Questions