Applying dimensions from dimens.xml to text formatted with Html.fromHtml - android

I have this code for setting the text of a TextView:
TextView txt = new TextView(this);
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />" + "<small>" + m.getText() + "</small>" + "<br />");
The <small> mark is working, but I'd like to set the text size according to my dimensions defined in the dimens.xml file, which I use for all other text in my application. Adding the TextView through an xml layout is not an option since I don't know how many TextViews I'll be adding.
Dimensions in the dimens.xml file are set up like <dimen name="text_size_320dp_small">16sp</dimen>.
How can I apply these dimensions to my text formatted with Html.fromHtml?
Thanks a lot.

I have tested following code myself. You can do it like this.
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />"
+ "<font textsize="
+ getResources().getDimension(R.dimen.text_size_320dp_small) + ">" + m.getText()
+ "</font>" + "<br />"));

[Updated]:
Just came up with some references and updates :
You can store this in strings.xml
<string name="mystring"><font size = "%s"></string>
In code you can write as:
int sptopx = getResources().getDimensionPixelSize(R.dimen.text_size_320dp_small);
Spanned modified = Html.fromHtml( context.getString(R.string.mystring, sptopx) );
myTextView.setText(spanned);
TextView txt = new TextView(this);
txt.setText(
Html.fromHtml(
"<b>" + m.getTitle() + "</b>" +
"<br />" +
modified +
">" + m.getText() + "</font>" +
"<br />"
)
);
for details about html tags support in TextViews you can check this link.

You can't directly, the small tag creates a RelativeSizeSpan with a proportion of .8f, which is hardcoded into the implementation of Html.fromHtml.
Leaves two options that I can see, set the text size to 20sp (which would make small work out to 16sp). Probably not ideal.
The other option is to use a custom tag <mySmall> by replacing all occurrences of <small> and </small> with <mySmall>& </mySmall>. And then call fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) with a TagHandler that integrates a AbsoluteSizeSpan into the output Editable.

Why don't you use txt.setSizeText(yoursize)? However you can retrieve your dimensions using this:
float yourDimen = getResources().getDimension(R.dimen.your_dimen_name);

Related

Setting part of summary with different colour while using preferences

I have a set of listpreferences which I am displaying in a PreferenceActivity screen. I would like to display a part of the text in the summary field with some formatting, say italics, or different colour.
What would be a good way to go about this?
I am using the following code to set the summary of a listpreference:
lp.setSummary(myText);
//E.g myText = "My name is SoAndSo";
//Required output : "My name is *SoAndSo* (in italics)
// or
// "My name is *SoAndSo* **(in green)**
You can format the text output in html, using html tags.
Here's one way:
mBox = new TextView(context);
mBox.setText(Html.fromHtml("My name is <i>SoAndSo</i>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));
For an unofficial list of tags supported by this method, refer to this link

Change font size in the same textview

Is it possible change size of temperature?
remoteViews.setTextColor(R.id.battery, Color.WHITE);
remoteViews.setTextViewText(R.id.battery, String.valueOf((int)batteryLevel + "%" + "|" + temperatura + "°C"));
Batterylevel and temperature are in the same textview. I want change size only of temperature. Actually is 50dp. I want 20dp.Hopw can i do it?
You can change the size using HTML code, but I don't think it is possible to specify detailed sizes in dp with it.
In your case I would use the tag <small> for the temperature:
remoteViews.setTextColor(R.id.battery, Color.WHITE);
String styledText = String.valueOf((int)batteryLevel) + "%" + "|" + "<small>" + temperatura + "°C</small>";
remoteViews.setTextViewText(R.id.battery, Html.fromHtml(styledText));
The list of supported tags is described here:
http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/

Changing text within textview

So is it possible to change text size of part of the text in textview dynamically
Let's say we have a method that does that...what am I missing...I want to make x variable twice as big as y...they are string btw...
private void Data_transfer() {
test3.setText( x + " " + y);
}
You can use the Spannable type with a TextView to set multiple styles in one field.
In your case, you can do this like so, using the fromHtml() method which generates a Spannable string:
test3.setText( Html.fromHtml( "<font size='10'>" + x + "</font> <font size='5'>" + y + "</font>" ) );
And you can, of course, change 10 and 5 to whatever values you like.
private void Data_transfer() {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("Welcome to android");
}

Not display bold content in webview android

I have set custom font in Webview using
String html = "<html><head><title></title> "
+ "<style type=\"text/css\"> "
+ "#font-face { font-family:'GEORGIA'; src: url('file:///android_asset/GEORGIA.TTF'); }"
+ "body { font-family: 'GEORGIA'; text-align: justify;}"
+ "</style> "
+ "</head><body >"
+ "<div>"
+ "<p>"
+ data + "</p></div></body></html>";
webview.loadDataWithBaseURL("",html,"text/html", "utf-8", "");
It is set font but problem is that in data i have text like heading contents when i run it then heading is not display bold.
Maybe your font does not contain bold characters.
True Type Fonts (TTF) can contain a subset of all available characters.
Did you copy the ttf-file from your systems fontpath?
If not, you can open the font with a tool like font-explorer,

Using size HTML attribute in TextView

I have the following:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
The string 'Hello' does turn red but the size does not change.
It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something wrong?
Size attribute seems not working.
You can use <small> or <big> (multiple times to increase the effect)
You can also use <h1> to <h6> (Header only, i.e. add a new line)
Its old-fashion, but it works well !
Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.
From Html class sources:
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
}
...
else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
}
...
}
private static void startFont(SpannableStringBuilder text,
Attributes attributes) {
String color = attributes.getValue("", "color");
String face = attributes.getValue("", "face");
int len = text.length();
text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
Try this one,Its working for me,use small,big key words
TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
+ "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
+ "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
Sergey Gotov is right. The only way to change text size it to use h1 - h6 tags.
EDIT: You can also implement TagHandler and use your own tags.
Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?

Categories

Resources