TextView with different fonts and styles? - android

Is it possible to have texts with different sizes, font-types or styles in the same TextView ?
Something like this:
| myLogin logout |

You can do this using:
textView.setText(Html.fromHtml("<b>myLogin</b> <i>logout</i>"));
For more options, look into SpannableString: Link
With SpannableString, you can apply multiple formatting to a single string.
This article will be very helpful to you: Rich-Style Formatting of an Android TextView

For anyone who wants to do this without the HTML formatting , use a SpannableString.
As in :
SpannableString styledString = new SpannableString("myLogin logout");
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 8, 14, 0);
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(styledString);
More examples can be found here

Unless you make a custom TextView, no. Consider using two different TextViews if you do not want to create a custom TextView.

Related

edittext with multi style text

I need to add multiple styles for text in my edittext, after searching I have found two ways
1- using this and it is working for me
str.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.correct_email_color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
2- using HTML format, and I need to be able to use this to add padding and margin to my text :
str.setSpan(new SpannableString(Html.fromHtml(String.format(EMAIL_HTML_FORMAT, "ffA9F5BC" , splittedEmail[i]))), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
but the second way is not working, can any one help here ??
Not sure but I think you have to use
android.text.style.LeadingMarginSpan
For e.g:-
str.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, s.length(), 0);
Look into the following..http://developer.android.com/reference/android/text/style/LeadingMarginSpan.html
Hope this will helps you.
String text = "<body>By clicking Sign Up i agree with Facebook<b>Terms and Conditions</b> and <b>Privacy Policy</b></body>";
Spanned spanned1 = Html.fromHtml(text);
tvTerms.setText(spanned1);
above code is just snippest to use style through html tag hope it wll help you to achieve your requirement that how to give style through html tag in android

how to set upper small text in android TextView?

I want to add small upper text in android textView
how would you implement this?
two textViews both layout_below the same element and then
make the second smaller?
or is there a way in one textView to put style to one word
(can be longer or shorter in other languages)?
You can achieve that by following code
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("2580<sup>TH</sup>"));
Solution from Android Layout framework, would put two textViews in side a Linear layout and that is it.
Another way, is making the small text as an image. Than you can add it to text view directly.
you can do it like this:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("YourRank:250<sup>TH</sup>"));
also you can do it like this:
String str=getString(R.string.rank); //YourRank:250
String str2=getString(R.string.superScript); //TH
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("+str+"<sup>"+str2+"</sup>"));
it worked like charm!
You can use SpannableStringBuilder as fallows:
String text = "YourRank:250TH";
SpannableStringBuilder spannableString = new SpannableStringBuilder(text);
spannableString.setSpan(new SuperscriptSpan(), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
YourTextView.setText(spannableString);

Set text of a textview with different sizes

I have TextView and I want each word of the text to be with different size and font.
I know that you can set part of the text to be bold or with underline with the and tags.
But what if I need part of the text to be the size of 18sp and have a custom typeface(from assets) and another part to have a different custom typeface and a size of 14sp.
I can split the TextView to individual TextViews, but I want to avoid that.
You should use RelativeSizeSpan ,for Reference RelativeSizeSpan for different font sizes.
TextView tv;
final SpannableString text = new SpannableString("Hello World");
text.setSpan(new RelativeSizeSpan(2.0f), 0, 5,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new RelativeSizeSpan(1.5f), 5, 11,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
For FontStyle use StyleSpan for reference StyleSpan
So use can use custom typeface with the help of StyleSpan.
text.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start,
end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Hope this will help you.
You can write your code in HTML with all the styling you like and then use Html.fromHtml() in the setText() method:
E.g.:
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
Or:
Refer to this question. it might help you:
Custom Textview with Html text and Custom Font whole Android application

Dynamically change Textview size

Is it possible to change the single textView size dynamically?
In the below link the text size is dynamically changed. ? can anyone guide me on how to do this. thanks in advance
Thanks for all, at-last i used three different textview to design whatever i need. Thanks for all.
Yes. You can use SpannableStringBuilder to styling the text.
Same question is registered at here
Try following code
span.setSpan(new RelativeSizeSpan(0.8f), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Here, you need to set start and end as per your requirement. 0.8f is size of text here. You need to pass float value here.
Code
textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(textView.getText());
span.setSpan(new RelativeSizeSpan(0.8f), 0, 7,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new RelativeSizeSpan(1.8f), 7,
txt.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(span);
Output

Color is lost after adding string format

I need to add some text in a textview with some text with red color and some are black color with a specific formatting
Spannable wordtoSpan = new SpannableString(temp.substring(start, i));
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//failedToSolve.append(wordtoSpan); // was working when I used no formatting
failedToSolve.append(String.format("%7s", wordtoSpan)); // coloring is not working after I apply String.format on it
I know I am formatting Spannable object with String class method. Is there any alternatives?
How can I do the formatting and coloring together? I used HTML tag but no effects. Thanks in advance.
Is there any alternatives?
Call String.format() first, then create the SpannableString from the result and apply the ForegroundColorSpan.

Categories

Resources