applying italic for item in string-array in a string xml - android

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)

Related

How can I change some of statements' text style in Firebase?

I'm trying to show some statements to users from Firebase. There are titles and contents in these statements. Trying to write titles with a boldly styled post. However, I cannot change the code from the android studio because I've assigned the whole description child to a single TextView. instead, I used expressions such as "\ n", which is also used in string expressions. Is it possible to write bold headlines? if so how?
For example ; here is my
Can I write "ilk olarak bulunduğunuz federal devletteki bir..." here by a bold style?
yes you can display, the approach i use is setting html from firebase
<p><b>This text is bold</b></p>
and in android side
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tvDesc.setText(Html.fromHtml(strfromfirebase, Html.FROM_HTML_MODE_COMPACT));
} else {
tvDesc.setText(Html.fromHtml(strfromfirebase));
}
You need to use a SpannableString in this case.
For example if I want to bold the word "Hello" in "Hello World", I would do something like:
String word = "Hello World";
SpannableString spannableString = new SpannableString(word);
spannableString.setSpan(new StyleSpan(Typeface.BOLD),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
0 is the starting index and 5 is the ending index

How to use Spannable String Builder to enclose string within h1 tag

How can i set a particular string as a title in Edittext. I want to output to look like something like this:
Title
Some other text.
I have tried to make the text bold and give it a larger style. Something like this:
s.setSpan(new StyleSpan(Typeface.BOLD), 0, length ,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.2f), 0, length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
That does the trick but i wish if i could instead create a h1 tag around the string so the formatted html code i extract from Edittext can be displayed in textview or used elsewhere.
Update
It seems like a problem with Html class. Try something like this.
Log.d("Test", Html.toHtml(Html.fromHtml("<h1>Hello</h1>")));
You would get something like this in your logs:
<p dir="ltr"><b>Hello</b></p>
It seems like Html class is capable of converting h1 tags to spannable but not capable of being able to convert it back to h1 tags.
The fromHtml method converts the header tags to Spannable using a similar trick i already stated above. It's not implemented in the toHtml method and hence it don't create header tags from Spannable. Can someone try to fix this.
You can take a look at HTML.fromHtml(String text)
This will parse HTML and return a spanned string. The only thing notable is, that it's a bit tricky modifying the styles used for the spans.
Try something like this. Which worked for me
SpannableString text = new SpannableString(Html.fromHtml(<h1>Your content</h1>));

How to get this special space character ␣ into an Android Button?

I want to symbolize a space in an Android Button so:
in the strings.xml file I define <string name="SpaceSymbol">␣</string>
in the layout XML file I set android:text to "#string/SpaceSymbol"
When I run the application the button shows nothing.
Any ideas?
If any other character that symbolizes a space works other than ␣ I'll be glad to use it
Use the Html code corresponding to ␣ character, HTML Codes Table or HTML Special Characters
the define into the strings.xml
<string name="SpaceSymbol">␣</string>
This is an example with & character:
How can I write character & in android strings.xml
Update:
Answering your question, ␣ is non-printable character, it is just a "Space", if you want show this symbol into the button, try loading an image of that symbol into an ImageButton.
You can wrap the space char in
![CDATA[ ... ]] in the string resource..that may help you bring it in. You should check that the character is in the roboto font also
Given that the character is not represented in the Roboto font, you can use a SpannableString to replace the special character with your own image:
you should create a Drawable (or Bitmap) with the space image in it that you want to use. in this example it would be assigned to the variable 'drawable':
SpannableString textspan = new SpannableString(sb);
Pattern pattern = Pattern.compile("#_", Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(textspan);
while(matcher.find()) {
textspan.setSpan(new ImageSpan(this, drawable, ImageSpan.ALIGN_BASELINE), matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
In your string (in this code example) you would have replaced your space symbol with the two characters "#_". The pattern will find each occurence of "#_" and replace it with the drawable.
Then you can use the SpannableString as a CharSequence argument anywhere it's defined (Toast.makeText, TextView.setText, etc)
like this :
TextView tv = (TextView) findViewById(..);
tv.setText(textspan);

Rich Text Box in android

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.

Specifying "strikethrough" on a section of TextView text

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 ?: ""
)
}
}

Categories

Resources