i have a textview whose text i am setting as a big string.
this string has several parts like telphone no, mail , browsing address.
i have found how to use the browsing address in a span to open up the link in a browser?
here is the code
TextView tv = (TextView) findViewById(R.id.infotest);
SpannableString ss = new SpannableString(getResources().getString(R.string.clientaddress));
ss.setSpan(new URLSpan("http://"+getResources().getString(R.string.clientaddress)), 0, 23,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
how do i do the same for telephone text.
i mean how do i span the telephone text to call a the number when the user presses that specific part of the textview string.
You have to set it from the xml file.
For example:
android:id="#+id/mobile_number"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:autoLink="phone"
Available values for the autoLink field is: web, email, phone, map, all
Can you use Linkify?
Linkify.addLinks(tv,Linkify.PHONE_NUMBERS);
ETA or try using tel:// instead of http?
Related
A number of discussions on here going back years related to getting hyperlinks to work in a TextView. The conclusion is that autoLink works for parsing out URLs that are simply embedded in the text, e.g., "go to www.google.com". Then there is
setMovememtMethod(LinkMovementMethod.getInstance());
that will cause actual HTML tags to work, e.g. go to Google. However, using the latter causes autoLink to not work in the same view.
My issue is that I am displaying text that is supplied by a user database, so I have no control over the formatting. In some cases, this text has plaintext links while in others it is entered as HTML tags. Is there any way to get both types of links to work at the same time?
Both plain text links & links with HTML tags will work with the below code
TexView in xml
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textColorLink="#06b" />
Here in activity.java
String text = "this is the link with anchor tag Google. here is the plain text link http://www.google.com";
TextView textView = (TextView) findViewById(R.id.txt_view);
textView.setText(getParsedLinks(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
instead of using android:autoLink="all" in xml or Linkify.addLinks(textView, Linkify.ALL) use Linkify.addLinks to SpannableString as in below method
SpannableString getParsedLinks(String txt){
Spanned span = Html.fromHtml(txt);
URLSpan[] urlSpans = span.getSpans(0, span.length(), URLSpan.class);
SpannableString s = new SpannableString(span);
Linkify.addLinks(s, Linkify.ALL);
for (URLSpan urlSpan : urlSpans) {
s.setSpan(urlSpan, span.getSpanStart(urlSpan), span.getSpanEnd(urlSpan), 0);
}
return s;
}
i'm writing an application which consists some code snippets.
as the usual manner it can be displayed in TextView but i want to highlight some keywords say "main" , "int" , "class", "return" , etc.
after lots of time i figured out it can be done using html and css that loads to webview up , but it has some disadvantages like slow rendering so it makes bad user Experience .
is there a better way to approach the solution ?
it would be great to give me some code example...
thanks
Use SpannableString:
SpannableString string = new SpannableString("Your text that has to be highlighted");
string.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 10, 0);
textView.setText(string);
BackgroundColorSpan changes the background color. Also check out ForegroundColorSpan.
Or use Html.fromHtml which accepts Html tags:
String text1 = "Start your text ";
String text2 = "<font color='#EE0000'>and it is now red</font>";
textView.setText(Html.fromHtml(text1 + text2));
Html way might be a bit slower than SpannableString as it involves parsing the string.
I have a large body of text that includes web urls and emails. I created a SpannableString object and used Linkify class to set up hyperlinks for the web urls and emails. However, perceived phone numbers also get linked. So even though I have instances of 5-digit numbers such as "12345", this numeric sequence also are being linked. When clicked, a prompt comes up asking to call the number. Linkify thnks it is a phone number. How can I go back through and un-link specific text that I know should not be linked?
Here is how I have linked everything
final SpannableString s = new SpannableString(context.getText(R.string.message));
Linkify.addLinks(s, Linkify.WEB_URLS);
textView.setText(s);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Thank you in advance.
The image is from an app called kakao story.
Suppose there's a post with a list of comments like any sns apps.
When you click a comment, it inserts the user name of the commenter in the edit-text to indicate my new comment is a reply to the user.
(You can't add the same name more than once.)
When you hit backspace to delete the name, the entire characters that make up the name(e.g., chabeau in the example) will be deleted by 1-backspace.
I'm trying to mimic the behavior and want some pointers how to implement it or what to search for.
If you are in search of bubble view. You can achieve it by creating a subclass of android.text.style.DynamicDrawableSpan.ImageSpan which will convert a portion of EditText string into formatted span.
This SO Question will give you some basic idea about creating formatted span.
This is a good tutorial for customizing editext with spans.
And for deleting whole word at once, you can use SPAN_EXCLUSIVE_EXCLUSIVE property.
Below code will format the first four character of the string, Hope this will give you some hint.
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
final ForegroundColorSpan fcs
= new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(sb);
EditText et = (EditText) findViewById(R.id.edit1);
et.setTextColor(Color.parseColor("yourColorCodeHere"));
Is there any control in android which we can use as a rich text box which can handle formatting of characters such as bold, italic etc. and use fonts and color ?
If anyone having any link or source related to the above information please share it with me.
SpannableString class or HTML.fromHtml() allows you to manipulate different styles in actual string. See the links below:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)
Sample SpannableString and TextView implementation:
TextView tv = new TextView;
String text = "String String String String body";
SpannableString spannableStr = new SpannableString(text);
spannableStr.setSpan(new StyleSpan(Typeface.BOLD), 0 , 10, 0);
tv.setText(spannableStr);
WebView is the closest that I can think of, but it is super duper heavy unless you want the entire screen to be a webview.