Different font width in #string of TextView - android

I have the string <string name="getStarted">Get Started!</string>.
I have set it's textview to android:fontFamily="sans-serif-light".
Can I have the Started! as a regular fontFamily where the rest of the string (Get) having a light weight?
I tried:
<string name="getStarted">Get <n>Started!</n></string> like <b></b> but well... n is not a tag (n for normal).

final SpannableStringBuilder sb = new SpannableStringBuilder("Get Started!");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
sb.setSpan(bss, 4, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make them also bold
yourTextView.setText(sb);

For consistency, based on #Gavin2058 's answer.
SpannableString sb = new SpannableString("Get Started!");
sb.setSpan(new TypefaceSpan("sans-serif"), 4, 11, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
SPAN_EXCLUSIVE_INCLUSIVE helps you add more setSpan of the same style if you have a big string with multiple width words.

Related

How to make part of string bold and colored in android?

I have a string with some points which i get from storage model , I want to bold and color the point string only which i get from data storage model. How do i do that i have tried below mentioned code.
//Making a part of text from terms and condition spannable
SpannableStringBuilder sb = new SpannableStringBuilder("App " + CWalletDataModel.getInstance().getS_szWalletBalance() + getString(R.string.points_text));
// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(RewardUtil.getColor(mContext,R.color.colorAccent));
// create a bold StyleSpan to be used on the SpannableStringBuilder
StyleSpan b = new StyleSpan(Typeface.BOLD); // Span to make text bold
// Set the text color for first 4 characters
sb.setSpan(fcs, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// set only the name part of the SpannableStringBuilder to be bold --> 16, 16 + name.length()
sb.setSpan(b, 8, 8 + CWalletDataModel.getInstance().getS_szWalletBalance().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
HomeScreenActivity.m_toolbarTitle.setText(sb);
Here i want only to bold "CWalletDataModel.getInstance().getS_szWalletBalance()".
That's because App + blank space is 4 characters("App "), not 8. Set the start for those 5000 at 4th position like you wrote in the comment and then spannable options would start after "App "
sb.setSpan(fcs, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(b, 4, 4 + test.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

Seeking the Simplest way to have a TextView's text colored in diffrent colors

I want to have some of the string that is shown by a text view to be red, and some black, how can i do that?
is there a way to mention it simply on the XML file? (strings.xml)
I think you can use span. Text Style
For Example,
final SpannableStringBuilder sb = new SpannableStringBuilder(" text must be here ");
final ForegroundColorSpan fc1 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
final ForegroundColorSpan fc2 = new ForegroundColorSpan(Color.rgb(255, 255, 255));
sb.setSpan(fc1 , 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(fc2, 5, 8, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
(or)
In styles XML, declare the color values as shown below
<color name="redColor">#ff0000</color>
and use it in the layout xml, for the textColor attribute,
android:textColor="#color/redColor"
you can try this,
String.replaceAll(textToHighlight,<font color="red">textToHighlight</font>);
Textview.setText(Html.fromHtml(String));
I believe the only way of doing it is to use a spanned type as below.
TextView textview = (TextView)findViewById(R.id.myTextview);
String firstName = "FirstName";
String lastName = "LastName";
Spanned details = Html.fromHtml(firstName + "<br />" + "<small><font color=\"#767676\">" + lastName + "</b></small>");
textview.setText(details);
Hope this helps

TextView with different sizes

In my layout, I have defined a TextView and have given it the id - textName.
Programmatically, I have set its text to My name is Blah. I was wondering if it is possible to programmatically increase the textsize of just Blah and not the whole TextView?
Or simply using the Spannable class:
String text = textView.getText();
Spannable span = new SpannableString(text);
span.setSpan(new RelativeSizeSpan(1.5f), text.indexOf("Blah"), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
Yes!! you can do by formatting the string with HTML, refer https://stackoverflow.com/a/1533512/603233
like eg
String text = "<h5>My name is </h5><h1>Blah</h1>";
yourtextview.setText(Html.fromHtml(text));

How to make multi spannable text?

This is example.
String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//someTextView.setText(out);
Expected result: This is example text ("hi" & "xam" are bold)
Actual result:      This is example text (works only last setSpan method and bold is only "xam")
How to make multi spannable text? It's possible?
Maybe problem is in Spannable.SPAN_EXCLUSIVE_EXCLUSIVE flag? Thanks.
I think you need a new StyleSpan for each.
String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Haven't tried it out though.
The reason is that each bold spannable text must be represented by a StyleSpan. The reason for yours is just the same as reassignment of a value.

combining multi strings and text in android

I'm pretty new to android, Java and sqlite. For my first program I'm creating a program that will take user input and place in predefined text.
Example: "text" string1 "more text" string2 "even more text" etc
My text will be one color and strings will display in another color.
I'm using sqlite to seperate my data and code and this is where I hit a wall. Trying to find help on how I will be able to combine my above text into one row/column in my database table.
Using only one above example i could get this up and running. But there will be 50+ of above example for release making a database a must especially when I want to add more after release.
Most probably you've read up on SpannableStringBuilder, which allows you to add color to the text in your TextView's content. Check out the code below:
SpannableStringBuilder ssb = new SpannableStringBuilder(<your text>);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, ssb.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
The code above will work in most cases, however what you want is to have different alternating colors on a single TextView. Then you should do the following:
String text = your_text + text_from_database;
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255 0));
ssb.setSpan(fcs, 0, your_text, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(fcs2, your_text.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
The above code will now work, but you'll notice that if you add another text your_another_text and want to use the previous fcs instance for a second time, the previously colored your_text will now lose its formatting (color). This time you'll need to create another ForegroundColorSpan fcs3 to format the third part of the SpannableStringBuilder. The key here is to use a character style in a setSpan method only once. See below:
String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";
SpannableStringBuilder ssb = new SpannableStringBuilder(testTest+testTest2+testTest3);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, testTest.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs2, testTest.length(), (testTest+testTest2).length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcs3, (testTest+testTest2).length(), ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
test.setText(ssb);
This method is good if you know you have a fixed number of String elements in your SpannableStringBuilder. If you have wish to have a TextView of dynamic length and number of elements, you need to do this in a different approach. What worked for me was to convert each string element into a SpannableString, use setSpan, and append it to the TextView. This is useful if you're using a loop to build your TextView.
TextView test = (TextView)findViewById(R.id.test);
String testTest = "abcdefgh";
String testTest2 = "ijklmnop";
String testTest3 = "qrstuvwxyz";
SpannableString ssb = new SpannableString(testTest);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ForegroundColorSpan fcs2 = new ForegroundColorSpan(Color.rgb(0, 255, 0));
ForegroundColorSpan fcs3 = new ForegroundColorSpan(Color.rgb(255, 0, 0));
ssb.setSpan(fcs, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.setText(ssb);
SpannableString ssb2 = new SpannableString(testTest2);
ssb2.setSpan(fcs2, 0, ssb2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb2);
SpannableString ssb3 = new SpannableString(testTest3);
ssb3.setSpan(fcs3, 0, ssb3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
test.append(ssb3);

Categories

Resources