Android TextView ellipsize error with Html Text - android

I have some problem with ellipsize in TextView.
Here is html string:
final String str = "His followed carriage proposal entrance <br/>directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. <br/>Power dried her taken place day ought the. Four and our ham west miss."
I create 2 textview(tv_test_html, tv_test_normal).
<TextView
android:id="#+id/tv_test_html"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Hello"
/>
<TextView
android:layout_marginTop="20dp"
android:id="#+id/tv_test_normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Hello"
/>
In code I set:
final TextView tvTestHtml = (TextView) findViewById(R.id.tv_test_html);
final TextView tvTestNormal = (TextView) findViewById(R.id.tv_test_normal);
tvTestHtml.setText(Html.fromHtml(str));
tvTestNormal.setText(str);
When I run application:
You can see "..." is error when I use Html.fromHtml(...);
So how do I fix it ?
Thanks in advance !
[I just update image]

replace string with it : His followed carriage proposal entrance <br>directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. <br>Power dried her taken place day ought the. Four and our ham west miss.

Related

Urdu language special character parentheses wrong displaying: right to left

I am trying to display an Urdu text (Right to Left) in my app android HtmlTextView of the following dependency org.sufficientlysecure.htmltextview, but I get parentheses wrong displayed left to right in lines.
In the above image, parentheses display wrong formatting even though I checked the text is proper.
and below the image is the correct version of the text
Here is my XML code
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="#+id/detail_d"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_8sdp"
android:layout_marginRight="#dimen/_8sdp"
android:gravity="right"
android:paddingLeft="#dimen/_1sdp"
android:paddingRight="#dimen/_1sdp"
android:text="#string/urdu_detail"
android:textColor="#000"
android:textIsSelectable="true"
android:textSize="#dimen/_18ssp" />
Here is my Java code
private String htmlText = "سنبل کی لہر سے نہ رہے پھر ہمیں مطلب یکدست جو تم کا کل خمدار دکھاؤ (١٨٤٠ء، کلیات ظفر، ١٩٣:١)<br>";
private HtmlTextView htmlTextView = htmlTextView.setHtml(htmlText);

How can i add a description in a ScrollView on separate lines?

How can i create a description within a scrollview?
i would like to place a date on none line and a title on a separate line, but even with spaces and pressing enter it creates all on one line.
The code works well i just need help on being able to control what i write on what line. For example
date
location
title
expiry
instead of writing them on separate lines it all goes onto the same line. Even using enter, tab, spaces.
MAIN ACTIVITY
TextView mTitleWindow = (TextView) findViewById(R.id.titleWindow);
TextView mMessageWindow = (TextView) findViewById(R.id.messageWindow);
mTitleWindow.setText("BATMAN IS PART OF THE DC TRINITY");
StringBuilder stringBuilder = new StringBuilder();
String someMessage = "hello hi" ;
stringBuilder.append(someMessage);
mMessageWindow.setText(stringBuilder.toString());
layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/titleWindow"
android:layout_width="352dp"
android:layout_height="58dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp" />
<TextView
android:id="#+id/messageWindow"
android:layout_width="350dp"
android:layout_height="455dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="17dp"
android:layout_below="#id/titleWindow" />
</RelativeLayout>
</ScrollView>
Hello #D_TECK use "\n" in string variable value for the separate line in message.
String someMessage = "hello \n hi";

Android TextView Hyperlink

I'm implementing a TextView with a string containing two hyperlinks as below but the links are not opening a new browser window:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="#color/colorPrimary"
android:autoLink="web"
android:text="#string/agree_terms_privacy"/>
In string.xml
<string name="agree_terms_privacy">By continuing, you agree to our Terms of Use and read the Privacy Policy</string>
Here is the solution that worked for me, after looking through multiple Stack Overflow posts. I've tailored it to your implementation:
1. Remove autolink in favor of using LinkMovementMethod, and set linksClickable to true
<TextView
android:id="#+id/termsOfUse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="#color/colorPrimary"
android:text="#string/agree_terms_privacy"/>
If you use the android:autoLink="web" property then you'll have to override it with textView.setAutoLinkMask(0); before calling setText() on your TextView. You can also set the link to be clickable in your activity instead like in Harshal's answer if you prefer, but I left it since you already had it in the layout. I also added an id to your TextView called termsOfUse which we'll use later.
2. Replace < with < in strings.xml and remove double quotes around the url
This is because when you retrieve the string resource, it won't parse the embedded html correctly, and for some reason it doesn't escape the double quotes. So instead of:
<string name="agree_terms_privacy">By continuing, you agree to our Terms of Use and read the Privacy Policy</string>
you'll want to do:
<string name="agree_terms_privacy">By continuing, you agree to our <a href=http://link1/terms>Terms of Use</a> and read the <a href=http://link1/privacy>Privacy Policy</a></string>
3. Parsing the string resource and binding it to our TextView
Spanned policy = Html.fromHtml(getString(R.string.agree_terms_privacy));
TextView termsOfUse = (TextView)findViewById(R.id.termsOfUse);
termsOfUse.setText(policy);
termsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
Note: Html.fromHtml has been deprecated in API 24 (see this post for more information on how to handle this if needed). We use this method to get the expected HTML formatting from our string.
Have a look on below code snippet, hope it helps,
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
This works for me
Add this in your textview:
android:autoLink="all"
android:clickable="true"
https://www.youtube.com/watch?v=UnJxyfyDyHU
I hope this help you.
I would recommend you to have two TextViews since ou want two different actions:
TextView yourTermsOfUseTextView = (TextView) findViewById(R.id.your_id);
yourTermsOfUseTextView.setOnclickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(your_download_link));
startActivity(myIntent);
}
});
Repeat to the privacy policy.
Just add below code in your Textview
android:autoLink="email"
You can use like bellow..
textview xml
<TextView
android:id="#+id/tv_welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
In activity
TextView tv_welcome_message=(TextView) findViewById(R.id.tv_welcome_message);
tv_welcome_message.setMovementMethod(LinkMovementMethod.getInstance());
String text = " <a href='http://www.google.com'> Google </a>";
tv_welcome_message.setText(Html.fromHtml(text));
MainActivity.java
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
I removed most of the attributes on my TextView to match what was in the demo
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtCredits"/>
Don't forget to remove autoLink="web" if you are calling setMovementMethod()
Just use Linkify
textView.text = text
Linkify.addLinks(textView, Linkify.WEB_URLS)
This worked for me
Add this in your textview
android:linksClickable="true"
android:autoLink="all"
AutoLink Values: all, email, map, none, phone, web Controls whether links such as urls and email addresses are automatically found and converted to clickable links. The default value is "none", disabling this feature.

Android ellipsize does not work properly

I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))

Android setting with TextView for Hebrew text?

I am setting text in TextView from string resource. Normally, Hebrew works in Right-To-Left format. When I set a text, it sets the text Right-To-Left format in LG, Samsung, Sony Phone but in HTC it does not work. It works in Left-To-Right format in HTC. Even I set Gravity to the TextView in Java file.
Text in TextView should be span according to the screen size. For example if it is 320 x 480 then it display in 4 lines but if it is Galaxy Tab then there may be 2 lines.
Here is my Code snippet:
In Java:
private TextView mVersionInfo, mVersionDescriptionOne, mVersionDescriptionTwo, mVersionDescriptionThree;
mVersionInfo = (TextView)findViewById(R.id.VersionInfo);
mVersionDescriptionOne = (TextView)findViewById(R.id.VersionDesc1);
mVersionDescriptionTwo = (TextView)findViewById(R.id.VersionDesc2);
mVersionDescriptionThree = (TextView)findViewById(R.id.VersionDesc3);
mVersionDescriptionOne.setGravity(Gravity.RIGHT);
mVersionDescriptionTwo.setGravity(Gravity.RIGHT);
mVersionDescriptionThree.setGravity(Gravity.RIGHT);
in XML:
<TextView android:id="#+id/VersionDesc1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc1" android:textColor="#000000"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/Share" android:textSize="13sp"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc2" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc1" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc3" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc2" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
In String Resource:
<string name="versiondesc1">האפליקציה מתחברת לאתר הספק הסלולרי כדי להציג את מצב החשבון. לעיתים, כשאתר הספק איננו עובד תקין לא יהיה ניתן לקבל מידע. באם אתר הספק ישתנה האפליקציה עלולה להפסיק לעבוד. במצב כזה האפליקציה תחזור לעבודה תקינה מיד לאחר שאנו נתאים את שרת התוכנה שלנו לשינויים.</string>
<string name="versiondesc2">הערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string>
<string name="versiondesc3">אם נתקלת בבעיה, השתמש/י בכפתור יצירת קשר על מנת שנוכל לפתור אותה. נשמח לקבל כל משוב על האפליקציה.</string>
What is wrong with my code?
Anybody who has worked with another language, Please guide me here.
Thanks.
Gravity will only affect alignment and will not set base direction for the text. That it works on some devices and not others may be a font issue, or perhaps an OS version issue. Try adding a RIGHT-TO-LEFT MARK character (\u200F) at the start of your text. This might help the display on an HTC and will not hurt anything on devices where it is already working.
here is an example from my hebrew string xml, Thanks to Ted Hopp's answer:
you need to add '\u200e' before the char that causes you the problem:
<string name="basic_text1">המר על תוצאת המשחק\u200e:</string>
and the result will be:
:המר על תוצאת המשחק

Categories

Resources