Superscript and Subscript in Android App XML - android

I am parsing data from an XML file which has subscript and superscript characters in them which I got from the character map. Like so:
<value column="Back" null="false">H₂</value>
but when I display it on a textview in Android it gives me this 'Hâ,,'
How can I fix this and display it properly in my app?

I found out how
In the XML file the code should be like this :
<value column="Back" null="false">H<sub>2</sub></value>
So that the parced string value is "H<sub>2</sub>"
then in the java code :
TextView textview.setText(Html.fromHtml(YourString);
and for superscript use "sup" instead of "sub"

You can use <small> tag along with <sub> or <sup> tags to decrease the size of the text.
For example:<sup><small>your-string</small></sup>
If you want to still decrease the size of text,you add <small> tag once more like this<sup><small><small>your-string</small></small></sup>
I'm using android studio and it is working perfectly for me !

i found this method useful for me.
strings.xml
<string name="your_string">H<sub>2</sub></string>

You can change it easier than that, just write in #strings or where you want<sub>your-string-here</sub> for subscript or <sup>your-string-here</sup>.
The subscripted letters(of your word) still have the same size as your text. You need to decrease it's size.
For that, use , where number means the size of the text you want.

Related

How to write subscript under superscript in xml?

I am working with android textview. I need print subscript under superscript.
This is my code:
<string name="abc">Text<sup>supertext</sup><sub>subtext</sub></string>
And this is what i get:
Textsuertextsubtext
But i need something like:
Is it possible to do that only in XML without using java?
You could use 2 text views. One with "Text" and next to it a multi line text view with "Supertext...".
However only do that if it's not strings that may ever be translated.

how to superscript in a title in androidplot?

I'm trying to superscript a number in a title in androidplot, like so:
strings file:
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
xml file:
androidPlot.title="#string/plot_title_3m"
but it doesn't superscript at all, the number is normal styling. I've also tried using
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
but no dice, it actually shows the sup tags in the title
also tried this
<string name="plot_title_3m">stuff per m<small><sup>3</sup></small></string>
and actually found out that the 'small' tags don't work either...
You can use HTML tags to achieve this. To be more exact, you need to use Spans on your input text to achieve this look on TextView, but we have a friendly neighborhood helper class that converts some common HTML tags into spans.
E.g.:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

Change font of superscript text

I am writing an view where I need to display superscript data. I am following this to achieve this.
Along this is there any way to change font size(in number) of superscript text.
EDIT
Commanware suggested link work great, except one thing. I need superscript bit above of base text. I'm updating image for same, please refer. I'm using same code with mention in reference code.
Here either can go for separate text view could be second priority solution. Any suggestion !
try this one this is working code:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("04:30<sup><small>pm</small></sup>"));
you cant pass text size in numbers, yo have to change the size to string :
<font size="3" color="red">This is some text!</font>
Use this Code :
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("your Text <sup style="font-size:5(yourfontsize);">subscript</sup>"));
Try this and Let me know :)

How to make XML strings bold, underlined etc?

http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext

How to create multiple lines of text in android?

In my project I need to display the instructions page. The page consists of 8 instructions. So do we need to create 8 textviews to display those instructions? Is it possible to display all those instructions using one single textview?
I read about multi-line textviews.
But for my case can I do so to display all 8 instructions in one textview?
As far as I recall, the newline character \n is recognised when used in strings.xml, so you could declare your instructions there and reference it in the layout. I never found a way to do it within the layout XML itself, but there may be something.
Edit: for clarity - I mean using the escaped character, e.g.:
<string name="instructions">Line 1\nLine 2</string>
You can use \n new line character from string resource or thru code:textView.setText("1st line\n 2nd line");
You can use html to format your string: textView.setText(Html.fromHtml("item 1<br>item 2<br>"));
You can use: StringBuilder to it.

Categories

Resources