So my requirement is as follows:
I've multiple strings stored in different string variables, lot of them are hardcore string, some of them are user inputs, while some are values from database(online as well as offline database.)
I want to combine all the strings in single String variable but I want each letter formatted differently as shown in following image.
where as my normal string would be like this:
MAIN TITLE SUBTITLE: Detail 1 Detail 2 Heading 1: User Input Data Heading 2: User Input Data.
Thank you!
Spannable String is your solution.
Refer this https://developer.android.com/reference/android/text/SpannableString.html
Also refer this
http://androidcocktail.blogspot.in/2014/03/android-spannablestring-example.html
Here is an example:
final SpannableString text = new SpannableString("Hello World");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "World".length(),text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.BLACK), 4, text.length() - 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
https://developer.android.com/reference/java/util/Formatter.html
Please check above link for string formatting.
You can use Html.fromHtml() and format the string with HTML like;
String my_string = "<h1>MAIN TITLE</h1><br /><h2>SUBTITLE</h2><br />"
+"Detail 1 <br /> Detail 2 <br /><br/>"
+"<h4>Heading 1:</h4> User input data <br/>"
+"<h4>Heading 2:</h4> User input data"
Then
TextView myTextView = (TextView) findViewById(R.id.the_id_of_your_tv);
myTextView.setText(Html.fromHtml(my_string);
Related
I have this code
TextView text1 = (TextView) view.findViewById(R.layout.myLayout);
Spanned myBold = (Html.fromHtml("<b>Test<b>", Html.FROM_HTML_MODE_LEGACY));
If I do
text1.setText(myBold);
Then myBold is in bold,which is ok. But when I want to add a string more, like
text1.setText(myBold+"bla");
Then the whole TextView is not bold anymore. Why does the new String "bla" affect this?
Thanks.
Why does the new String "bla" affect this?
Because what you are really doing is:
text1.setText(myBold.toString() + "bla");
A String has no style information. A Spanned object does.
Use TextUtils.concat() instead:
text1.setText(TextUtils.concat(myBold, "bla"));
A better choice would be to use a Bold StyleSpan. In the next sample only the "hello" world will be set to bold by using such technique:
Java:
final SpannableString caption = new SpannableString("hello world");
// Set to bold from index 0 to the length of 'hello'
caption.setSpan(new StyleSpan(Typeface.BOLD), 0, "hello".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(caption);
Kotlin:
yourTextView.text = SpannableString("hello world").apply {
// Set to bold from index 0 to the length of 'hello'
setSpan(StyleSpan(Typeface.BOLD), 0, "hello".length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE))
}
This would be a more optimal solution rather than using the Html.fromHtml technicque, as it doesn't have to go through the overhead of parsing/interpreting the HTML tags.
In addition, it allows you to combine more styles, sizes, etc, in the same SpannableString.
I have implemented a search list
It outputs the matched strings from pattern in a given list view.
Now I want to color only that part of my string which matches the pattern.
Please help me.
Thanks in advance.
You're looking for spannable:
http://developer.android.com/reference/android/text/Spannable.html
Example:
SpannableString demotext = new SpannableString("All your base are belong to me");
// Make 'All' (chars 0 to 3) red
demotext.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, 0);
((TextView) getView().findViewById(R.id.fragment_disclaimer_title)).setText(demotext);
http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring
http://eazyprogramming.blogspot.co.uk/2013/06/spannable-string-in-android-url-span.html
for color a part of your string you can do like this :
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
just after modify this.
This is the string which i am getting from web using json api in my code:
String content = "<strong><em>India is the world’s hub for child sex trafficking</em>
</strong></p>\n<p style=\"text-align: center;\"><strong><em>Nearly 40,000 children are
abducted every year… </em></strong></p>\n<p style=\"text-align: center;\"><strong<em>
Every8 minutes a girl child goes missing in India!</em></strong></p>\n<p><strong>Priti
Pathak</strong></p>\n<p>MEET Shivani Shivaji Roy, Senior Inspector, Crime Branch, Mumbai
Police, as she sets out to confront the mastermind behind the child trafficking mafia,"
In this string whatever the text is in
"<strong><em> TEXT </em></strong>"
I want to display it BOLD. So for the above string
<strong><em>India is the world’s hub for child sex trafficking</em></strong>
will be displayed as
India is the world’s hub for child sex trafficking
. This should happen for entire string. This is the code which i am using:
int startIndex = content.indexOf("<strong><em>")+13; // 13 because <strong><em> has 13 characters
String substring = content.substring(startIndex, startIndex+200);
int subendIndex = substring.indexOf("</em></strong>");
int endIndex = startIndex + subendIndex;
SpannableString s = new SpannableString(content);
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
textview.setText(s, BufferType.SPANNABLE );
This code is working but it is setting bold text only to the text whichever is first within strong tag and not to the rest. Because I am setting bold text only to the first tag.
How should i get the startIndex, endIndex of all the "strong tags" and so i can set
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
to all the texts and also the number of times i would have to set the span.
I though of using regex also but i dont know how to get indexes using it. Any help would be nice. Thankyou !
The easier way is to use the Html class:
s = Html.fromHtml(content);
See the documentation for the details.
This is the simple way
TextView TV = (TextView)findViewById(R.id.Tv);
String Title="I N <big>D</big> I A";
TV.setText(Html.fromHtml(Title));
If you want to only bold some strings then I recommend Html.fromHtml. A short sample:
String str = "<b> bold text</b> unbold one";
textView.setText(Html.fromHtml(str));
Here's a list of html tags supported by textview.
Use regular HTML tags in Strings, This text uses bold and italics by using inline tags such as within the string file.. Refer this link Hope this Link would be helpful for you!
I am trying to create a function that will take a string "word" as an argument, apply span styling to the string and return it as a spannable string that I can ""+insert+"" into a string in a TextView. Thank you in advance for any help you can offer with this.
Here is the function I have so far.
public Spannable fr(String word){
Spannable WordtoSpan = new SpannableString(word);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return WordtoSpan;
}
and the code that should display the spannable inline with my text.
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<strong>Text String</strong> "+fr("style_this"));
The application will either load the text without styling or the app will crash. Not sure what I'm doing wrong but I'm guessing it has something to do with mixing fromHtml text with spannables. Any work arounds?
If I remove the Html.fromHtml() and wrap the entire text with my fr() function it works. I get bold blue text. But I need to be able to make only certain words styled... It would also be nice to be able to reference vars from my string.xml file instead of hard coding them in, for multilingual support.
I think you may find this helpful,
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<font color='blue'>Text</font> "
+ "<font color='red'>String</font>"));
This may be a blunder,But I need to know... :)
Iam develeoping an android application,In in I want to display two type face in a single textview and found this One very useful,A custom typeface span that extends typeface span,
As per my understanding , we are creating a spannable as follows using two words
String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
then sets two type face to that words using our custom span like this
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.
WHAT I NEED IS, DIRECTLY APPLY THE CHANGE TO TEXT WHAT THE SET SPAN DO TO THE TEXT
Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?
Thank you for all
Very late answer but what you asked for is pretty easy.
For instance, lets say we want to change everything between quotation marks into italic font.
String s = "\"Hello my name is Edward\" Hola, my nombre es Edward";
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(text);
Spannable spannable = new SpannableString(s);
while (m.find()) {
int textLocation = text.indexOf(m.group(1));
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);
}
textbox.setText( spannable );
Result of s would be
"Hello my name is Edward" Hola, my nombre es Edward
Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;
And you could also remove the tags afterwards with a simple .replace();