Set text-size for part of the string - android

I had two strings:
<string name="bigtext">Big text</string>
<string name="anothertext">Small text</string>
I need them both in EditText's android:hint.
I tried to combine them & put "Big text" into <b>Big text</b>:
<string name="test">"<b>Big text</b> \nSmall text"</string>
"Big text" is bold, yep. But how to specify that "Big text" should have bigger font than "Small text"?

Add <big> tags around the big text. Or, if you prefer, add <small> tags around the small text. However, those will not work in a string resource AFAIK -- you would need to have this string in Java code or something, using Html.fromHtml() to get the SpannedString you would feed to setHint() on EditText.

Related

How to set extra bold to text in string resources?

I have a question is there a way to set text the extra bold weight in a string resource?
this is what I have now:
<string name="home_text2>
<![CDATA[
some text<br/>
more text<br/>
<b>even more text</b>\u1433
]]>
</string>
The designer says all text should be in bold not just as i have it now in example string. which is easy setting android:textStyle="bold" in the text view. But how do I set part of the text in the example string as extra bold?
Been more specific I want to set the extra bold weight to the part of the text that now is just bold.
Thanks!
Nothing involving string resources or TextView support "extra bold" out of the box. You are welcome to try creating a custom CharacterStyle that implements "extra bold" by some means. Or, use a WebView. Or, draw text directly to a Canvas, with painting rules that implement "extra bold".

Android: How to use bold text inside normal text

I have the following TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This must be bold (but not this)" />
I want only a part of the text to be bold. android:textStyle="bold" will therefore not work.
If the text is coming from a string resource, use <b> tags as you would in HTML:
<string name="foo"><b>This must be bold</b> (but not this)</string>
If the text is coming from somewhere else, use a SpannableStringBuilder to wrap the bold portion in a StyleSpan set for boldface. Or, generate an HTML edition of the string with <b> tags and use Html.fromHtml() to create the Spanned. In either case, the Spanned/SpannableStringBuilder output then just gets passed to setText(), which will rendered the desired portion in bold.
You can do it but I think it is very handy with xml therefore you can create a string in html and do it like this:
String sourceString = "<b>" + id + "</b> " + name;
mytextview.setText(Html.fromHtml(sourceString));
Where id and name will be two different text in your class

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>"));

Formatting string in xml resource file

I want to display help dialog box in which it has one textview and it loads the content from the String.xml file. Instead of making it one boring paragraphs, I would like to add some formatting to that String.xml For example coloring some sentences, bold..etc. Is there a way I can do that in the xml file within the string?
My xml looks like that
<string name="help_summary">Clicking on button (Summary) will result in ((report))</string>
So I want (Summary) to be red color and ((report)) to be bold.
How can I achieve that?
You can use Html. When you load the string in the textview use:
yourTextView.setText(Html.fromHtml(context.getResources().getString(R.string.help_summary)));
And use html in the string, for example:
<string name="help_summary"><![CDATA[Clicking on button <span style="color:red">Summary</span> will result in <b>report</b>]]></string>
There are some attributes that can be used in string resources without implementing HTML into and or altering your java code. None needed, you CAN do most of what you seem to need in strings.xml
Example:
<string name ="my_string"><i>italics</i><b>bold</b><u>underline</u><font fgcolor="#FFFFFFFF">color</font><small>small text</small></string>
I believe there is a <large/> or <big/> tag as well, amongst a few more.
Edit also, there is \n for a new line. Just include that in your string like so:
<string name="paragraph">Text is one first line \n now text is on next line. \n\n this will appear as an indented paragraph now.</string>
Hope this helps, happy coding!

Dynamically setting links to text in strings.xml

I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance).
So, I have my links in res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="link1">http://some.link.com</string>
<string name="link2">http://some.link2.com</string>
</resources>
and my localised text in res/values-en-rGB/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
</resources>
I've not tested this bit, but from the localization section of developer.android.com it says that this approach to reducing content duplication should work, although I'm not sure what folder I should put Italian, for example. Would it be in 'res/values-it-rIT/strings.xml'? Lets assume that I have various other languages too.
I'm looking for a way of taking the base localised 'sampleText' and inserting my html links in, and getting them to work when clicked on. I've tried two approaches so far:
1,
Putting some formatting in the 'sampleText' (%s):
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
and then processing the text like this:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(getResources().getString(R.string.sampleText, getResources().getString(R.string.link1), getResources().getString(R.string.link2)));
But this didn't work when I click on the link, even though the link text is being put in to the correct places.
2, I tried to use Linkify but the regular expression route may be difficult as I'm looking at supporting non-Latin based languages. I tried to put a custom xml tag around the link text and then do something like this:
Pattern wordMatcher = Pattern.compile("<span1>.*</span1>");
String viewURL = "content://" + getResources().getString(R.string.someLink);
Linkify.addLinks(tv, wordMatcher , viewURL );
But this didn't work either.
So, I'd like to know if there's a way of dynamically adding multiple URLs to different sections of the same text which will link to web content?
The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:
<string name="sampleText">Sample text <![CDATA[link1]]></string>
And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
In your layout set android:autoLink to web
<TextView android:text="#string/text_with_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
And in strings.xml just add the URL(s).
<string name="text_with_url">http://stackoverflow.com/ FTW!</string>
Try using Html.fromHtml() to convert the HTML into a Spannable that you put into the TextView. With what you have in #1, I would expect the TextView to show the HTML source, not rendered HTML.
You have to implement
setMovementMethod(LinkMovementMethod.getInstance());
on your Textview
Here is a better example:
clickable-urls-in-android-textviews

Categories

Resources