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
Related
In my android app with Kotlin, I created a layout in which there's a TextView that shows some text. For the text I have an item in strings.xml where I want to change the color of part of this Text, I tried the following code :
<string name="description">the product is <font fgcolor="green"> free </font></string>
But, The color didn't change.
I just want to change the color of "free" to green, can someone explain how I can achieve this?
Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.
Ben P.'s awesome answer should satisfy your use case. However, I want to present to you another way you can achieve this.
You can use SpannableString to achieve the same effect. With SpannableString, you can set several behaviours (color, font-weight, font-size, click-behaviour, etc) to any part of your String.
For the string in your question, you can do something like this:
// the textview you want to set your coloured text to
TextView textView = (TextView) findViewById(R.id.myTextView);
// declare the string you want to span as a Spannable
Spannable wordtoSpan = new SpannableString("the product is free");
// set the colour span
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 15, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the text to your TextView
textView.setText(wordtoSpan);
I have implemented a custom typeface throughout my app using the uk.co.chrisjenx:calligraphy:2.1.0 library.
I now need to change the color of part of a textview. I have tried to use
String username = "<font color='#FC195A'>" + post.getUsername() + "</font>";
but the font color remains the same.
Is there a workaround for this?
You can use Spannable String to get your thing done.
Just follow below link :
Set color of TextView span in Android
To provide color to the username, first get the length of it and do as below :
Spannable wordtoSpan = new SpannableString(username);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, username.length();, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
usernameTextView.setText(wordtoSpan);
After declaring the typeface as shown below
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf"));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(sBuilder, TextView.BufferType.SPANNABLE);
add the set text color option
setTextColor(Color.BLACK);
I want to have different typefaces for different parts of text in the same Edit Text. I have tried doing this to change Typeface :
Typeface myFont = Typeface.createFromAsset(getAssets(), "droid-sans.ttf");
myEditText.setTypeface(myFont, Typeface.BOLD);
I am using a button to make text BOLD.
But this changes Typeface of the entire text that is already present in the EditText ! I want to keep existing text formatting and change Typeface for the text that will be entered after I click "Bold" button.
You are looking for Spannable interface. You can use this to change the font of different part of an EditText or TextView.
This is an example code for you to turn a selected text into ITALIC.
Spannable str = mBodyText.getText();
if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart())
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
else
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
mBodyText.getSelectionEnd(),
mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
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
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.