I have a textView
inside with a number (variable) and a string
, how can I give the number one size larger than the string
?
the code:
TextView size = (TextView)convertView.findViewById(R.id.privarea_list_size);
if (ls.numProducts != null) {
size.setText(ls.numProducts + " " + mContext.getString(R.string.products));
}
I want ls.numproducts has a size different from the rest of the text. How to do?
In case you want to avoid too much confusion for your translators, I've come up with a way to have just a placeholder in the strings, which will be handled in code.
So, supposed you have this in the strings:
<string name="test">
<![CDATA[
We found %1$s items]]>
</string>
And you want the placeholder text to have a different size and color, you can use this:
val textToPutAsPlaceHolder = "123"
val formattedStr = getString(R.string.test, "$textToPutAsPlaceHolder<bc/>")
val placeHolderTextSize = resources.getDimensionPixelSize(R.dimen.some_text_size)
val placeHolderTextColor = ContextCompat.getColor(this, R.color.design_default_color_primary_dark)
val textToShow = HtmlCompat.fromHtml(formattedStr, HtmlCompat.FROM_HTML_MODE_LEGACY, null, object : Html.TagHandler {
var start = 0
override fun handleTag(opening: Boolean, tag: String, output: Editable, xmlReader: XMLReader) {
when (tag) {
"bc" -> if (!opening) start = output.length - textToPutAsPlaceHolder.length
"html" -> if (!opening) {
output.setSpan(AbsoluteSizeSpan(placeHolderTextSize), start, start + textToPutAsPlaceHolder.length, 0)
output.setSpan(ForegroundColorSpan(placeHolderTextColor), start, start + textToPutAsPlaceHolder.length, 0)
}
}
}
})
textView.text = textToShow
And the result:
I have written my own function which takes 2 strings and 1 int (text size)
The full text and the part of the text you want to change the size of it.
It returns a SpannableStringBuilder which you can use it in text view.
public static SpannableStringBuilder setSectionOfTextSize(String text, String textToChangeSize, int size){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToChangeSize.length() > 0 && !textToChangeSize.trim().equals("")){
//for counting start/end indexes
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToChangeSize.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
//for counting start/end indexes
if(startingIndex < 0 || endingIndex <0){
return builder.append(text);
}
else if(startingIndex >= 0 && endingIndex >=0){
builder.append(text);
builder.setSpan(new AbsoluteSizeSpan(size, true), startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}else{
return builder.append(text);
}
return builder;
}
in kotlin do it as below by using html
HtmlCompat.fromHtml("<html><body><h1>This is Large Heading :-</h1><br>This is normal size<body></html>",HtmlCompat.FROM_HTML_MODE_LEGACY)
Try spannableStringbuilder. Using this we can create string with multiple font sizes.
You can get this done using html string and setting the html to Textview using
txtView.setText(Html.fromHtml("Your html string here"));
For example :
txtView.setText(Html.fromHtml("<html><body><font size=5 color=red>Hello </font> World </body><html>"));`
Method 1
public static void increaseFontSizeForPath(Spannable spannable, String path, float increaseTime) {
int startIndexOfPath = spannable.toString().indexOf(path);
spannable.setSpan(new RelativeSizeSpan(increaseTime), startIndexOfPath,
startIndexOfPath + path.length(), 0);
}
using
Utils.increaseFontSizeForPath(spannable, "big", 3); // make "big" text bigger 3 time than normal text
Method 2
public static void setFontSizeForPath(Spannable spannable, String path, int fontSizeInPixel) {
int startIndexOfPath = spannable.toString().indexOf(path);
spannable.setSpan(new AbsoluteSizeSpan(fontSizeInPixel), startIndexOfPath,
startIndexOfPath + path.length(), 0);
}
using
Utils.setFontSizeForPath(spannable, "big", (int) textView.getTextSize() + 20); // make "big" text bigger 20px than normal text
Just in case you're wondering how you can set multiple different sizes in the same textview, but using an absolute size and not a relative one, you can achieve that using AbsoluteSizeSpan
instead of a RelativeSizeSpan
.
Just get the dimension in pixels of the desired text size
int textSize1 = getResources().getDimensionPixelSize(R.dimen.text_size_1);
int textSize2 = getResources().getDimensionPixelSize(R.dimen.text_size_2);
and then create a new AbsoluteSpan
based on the text
String text1 = "Hi";
String text2 = "there";
SpannableString span1 = new SpannableString(text1);
span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE);
SpannableString span2 = new SpannableString(text2);
span2.setSpan(new AbsoluteSizeSpan(textSize2), 0, text2.length(), SPAN_INCLUSIVE_INCLUSIVE);
// let's put both spans together with a separator and all
CharSequence finalText = TextUtils.concat(span1, " ", span2);
private SpannableStringBuilder SpannableStringBuilder(final String text, final char afterChar, final float reduceBy) {
RelativeSizeSpan smallSizeText = new RelativeSizeSpan(reduceBy);
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text);
ssBuilder.setSpan(
smallSizeText,
text.indexOf(afterChar),
text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
return ssBuilder;
}
------------------------
TextView textView =view.findViewById(R.id.textview);
String s= "123456.24";
textView.setText(SpannableStringBuilder(s, '.', 0.7f));
---------------- Result ---------------
12345.24
The best way to do that is Html without substring your text and fully dynamique For example :
public static String getTextSize(String text,int size) {
return "<span style=\"size:"+size+"\" >"+text+"</span>";
}
and you can use color attribut etc... if the other hand :
size.setText(Html.fromHtml(getTextSize(ls.numProducts,100) + " " + mContext.getString(R.string.products));
Source: Stackoverflow.com