I would like to highlight a TextView and achieve the design shown below.
Does anyone know how to achieve this using a TextView?
I took this screenshot from an existing Android app.
By using this code I get results shown below, which is not what I want:
sp.setSpan(new BackgroundColorSpan(color), start, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
You need to use a Spannable String:
TextView textView = (TextView)findViewById(R.id.textView1);
String text = "Test";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
Try this. This will work.
If I understand you correctly, you want to highlight the text with a light color? If that's the case, then simply change the transparency of the color. For example: change the first two FF of 0xFFFFFF00 to something like 80(50 % transparency). So, it would look like 0x80FFFF00.
TextView textView = (TextView)findViewById(R.id.textView1);
String text = "Your String";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0x80FFFF00), 14, 19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
Here's a relevant thread regarding hex color transparency.
Related
I am working on a text editor . I want to make text bold and normal(unbold) on a button click . When bold button has been pressed every next typed text is bold, but i am unable to make every next typed text normal once bold button has been unchecked .
editText.getText().setSpan(boldSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
this line makes the every next typed text bold. i want to make text normal afterwards as soon as the bold button has been unchecked.
what i have tried:
I have tried setting Typeface.NORMAL on the coming text but this doesn't remove the boldSpan.
I have tried removing end part of the span , but this removes the whole span , so at the end no text is bold at all .
I have tried for days, trying different approaches , but none of them worked . Any approach to get my job done would be highly appreciated .
check below code if its helpful for you.instead of string you can use your editText here dude ;)
String normalBefore= "First Part Not Bold ";
String normalBOLD= "BOLD ";
String normalAfter= "rest not bold";
String finalString= normalBefore+normalBOLD+normalAfter;
Spannable sb = new SpannableString( finalString );
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
sb.setSpan(new AbsoluteSizeSpan(intSize), finalString.indexOf(normalBOLD)+ normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
I have a string array which contains couple of paragraphs .one word contains a italic font. How can i apply italic font for a particular word-textview . I know we can apply italic using spannable. but any other way?
<string-array name="string_collections" formatted="false">
<item>nutrients and weakens the <![CDATA[<i>Agni</i>]]>(digestive fire) within the stomach. Ayurveda recommends sipping minimal amounts during meals, with larger volumes spaced throughout the day, always away from food.
\n</item>
It is showing as text. no effect using Html.fromHtml method.
Try this one :-
Try Html.fromHtml(), and mark up your text with bold and italic HTML tags e.g:
Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");
textView.setText(text);
I found a better approch my self.just use Matcher and get index of it.
java.util.regex.Pattern p =
java.util.regex.Pattern.compile("(^|\\s)word\\b",
java.util.regex.Pattern.CASE_INSENSITIVE);
final Matcher matcher = p.matcher(whole_text);
final SpannableStringBuilder spannable = new SpannableStringBuilder(whole_text);
while (matcher.find()) {
Log.e("Spannable", "Spannable" +"Matchfound");
//apply span which you want.
spannable.setSpan(new StyleSpan(Typeface.ITALIC), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tvtextmsg.setText(spannable);
Note:just found some another issue i have faced while using
if you are trying to do as below
tvtextmsg.setText(spannable+"\n \n"+another_text);
spannable will not be effected .you need do as
tvtextmsg.setText(spannable);
tvtextmsg.append("\n \n"+another_text)
so this image basicly shows what I want to do:
I want to add static text (that orange A) on a button which already has text on it. But I don't have any idea how.
You could try Html.fromHTML(htmlString). Something like:
myTextButton.setText(Html.fromHtml("<font color='#FFA500'>A: </font> whatever text you need here"));
With resp to #blackbelt
Spannable wordtoSpan = new SpannableString("A: Text");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.ORANGE), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(wordtoSpan);
where button is your Button object..
where
.setSpan(what,start,end,flag);
where
what has ForegroundColorSpan or StyleSpan or UnderlineSpan object. ForegroundColorSpan take one integer argument for color and StyleSpan also take one argument for style but UnderlineSpan don’t take any argument.
start has starting point of string where would you start string index for color of style.
end has ending point of string where would you end string index for color of style.
flag can have 1 or 0.
You can also change Fontsize to part of String.. refer this link for ref
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.
I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:
PageText = PageText.replaceAll("\\*([a-zA-Z0-9]+)\\*", "<b>$1</b>");
PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");
PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);
So, to bold a word, surround it with *'s, for italics, surround with _.
But, for strikethrough, Html.fromHtml does not support the "strike" tag, so it can't be done this same way. I've seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for - (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I'm wondering if there is a more elegant solution out there.
If it is just TextView you can strike through using paint flags
TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
#Suresh solution works if you want to strikethrough the entire TextView but if you want to strikethrough only some portions of the text then use the code below.
tvMRP.setText(text, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tvMRP.getText();
spannable.setSpan(new StrikethroughSpan(), 3, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Here text is the text which we want out TextView to display, 3 is the no. of characters (starting from 0) from where the strikethrough will start.
You can do it with a custom TagHandler such as the one on this SO question:
Spanned parsed = Html.fromHtml(PageText, null, new MyHtmlTagHandler());
And the TagHandler implements the methods:
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
....
Are you sure Html.fromHtml doesn't support <strike>? It's listed in this Commonsware blog post
It looks like is not really supported, at least it does not work on Android 3.1.
#RMS2 if text is small you can split it into two or three separate text views and apply flag only to the one which you want, not perfect for long texts ;(
Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we essentially have some functions available for the SpannableStringBuilder class.
For example, after instantiating a Builder instance we can use the build methods to append some bold text:
textView.text =buildSpannedString {
strikeThrough {
append(
value ?: ""
)
}
}