Android: html in strings.xml - android

I would like display for example this html code:
<body>
<p><b>Hello World</b></p>
<p>This is a test of the URL Example</p>
<p><b>This text is bold</b></p>
<p><em>This text is emphasized</em></p>
<p><code>This is computer output</code></p>
<p>This is<sub> subscript</sub> and <sup>superscript</sup></p>
</body>
I want to display it on a Dialog by declaring html in resources strings.xml. How can I do it?

The best way to add html source code in strings.xml is to use <![CDATA[html source code]]>. Here is an example:
<string name="html"><![CDATA[<p>Text</p>]]></string>
Then you can display this html in TextView using:
myTextView.setText(Html.fromHtml(getString(R.string.html)));
If you have links in your html and you want them to be clickable, use this method:
myTextView.setMovementMethod(LinkMovementMethod.getInstance());

Here's most of the examples. I don't think the pre tag is supported.
This is the strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Formatting</string>
<string name="link"><b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a></string>
<string name="bold"><b>This text is bold</b></string>
<string name="emphasis"><em>This text is emphasized</em></string>
<string name="sup">This is <sub>subscript</sub> and <sup>superscript</sup></string>
</resources>
Here's the layout. Note for the link to actually be clickable, there's a bit of extra work needed:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/test1"
android:linksClickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/test3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/test4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</ScrollView>
Finally, the code:
TextView test1 = (TextView)findViewById(R.id.test1);
Spanned spanned = Html.fromHtml(getString(R.string.link));
test1.setMovementMethod(LinkMovementMethod.getInstance());
test1.setText(spanned);
TextView test2 = (TextView)findViewById(R.id.test2);
test2.setText(Html.fromHtml(getString(R.string.bold)));
TextView test3 = (TextView)findViewById(R.id.test3);
test3.setText(Html.fromHtml(getString(R.string.emphasis)));
TextView test4 = (TextView)findViewById(R.id.test4);
test4.setText(Html.fromHtml(getString(R.string.sup)));

String.xml can contains HTML entities, like so:
<resources>
<string name="hello_world"><span></string>
</resources>
In your code: getResources().getString(R.string.hello_world); will evaluate to "<span>". You can use this HTML formatted text like this:
TextView helloWorld = (TextView)findViewById(R.id.hello_world);
helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));

All the styling supported by the XML resources system is explained in the Android documentation.
String Resources: Formatting and Styling
Anything included there can be used and set directly on TextView. If you need to use further HTML markup, you will need to place raw HTML (with escaped characters for <, > and such) into the resource and load the entire thing in a WebView.

This Worked for me :
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">Sangamner College</string>
<string name="about_desc"><![CDATA[In order to make higher education available in the rural environment such as of Sangamner, Shikshan Prasarak Sanstha was established in 1960. Sangamner College was established by Shikshan Prasarak Sanstha, Sangamner on 23rd January 1961 on the auspicious occasion of Birth Anniversary of Netaji Subhashchandra Bose.The Arts and Commerce courses were commenced in June 1961 and in June 1965 Science courses were introduced. When Sangamner College was founded forty years ago, in 1961, there was no college available to the rural youth of this region. <br><br></>The college was founded with the aim of upliftment of the disadvantageous rural youth in all respects. On one hand, we are aware of the social circumstances prevailing in the rural area where we are working. So, we offer the elective option to students, which are favourable to the local atmosphere. On the other hand, we want to academically empower the aspiring youth by offering vocational course in Computer Applications to students of Arts & Commerce. B.B.A., B.C.A. and M.C.A. courses were started with the same purpose. “Think globally, act locally” is our guiding Principle.]]></string>

Related

Cannot format partial TextView or Button text in Android Studio

I'm using Android Studio to create an English language learning app and I want to make a few letters on a button bold but I cannot do it. For example I want the button to read "I like basketball" as the first syllable in basketball is emphasized when we pronounce the word.
However I cannot get this to work. Here is my strings.xml.
<string name="test">I like <b>bas</b>ketball</string>
And here is my activity.xml.
<Button
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/test"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/test"
android:textAllCaps="false" />
</LinearLayout>
Neither the Button nor the TextView are recognizing the formatting.
I will have over 400 buttons that will require a different set of characters to be bold so I don't want to use spannable strings to code this.
Is there an easy solution?
1st Approach-
you string need to be updated to <string name="test">I like <b> bas</b>ketball.</string> if you are using string from String.xml
but you need to add below code in Activity class to work -
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(getString(R.string.test), Html.FROM_HTML_MODE_LEGACY));
}else{
textView.setText(Html.fromHtml(getString(R.string.test));
}
2nd Approach-
Add below code in your String.xml file at top
<?xml version="1.0" encoding="utf-8"?>
Then add your string in resources -
<resources>
<string name="test"><![CDATA[I like <b> bas</b>ketball]]></string>
</resources>
and then in activity class set the HTML text like below -
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(getString(R.string.test), Html.FROM_HTML_MODE_LEGACY));
}else{
textView.setText(Html.fromHtml(getString(R.string.test));
}
Hope this will help.

Error in matchin #string value in .xlm layout

I created a text box, associated to him his string value and all goes well. Later I added a new text box and associated him another string. The problem is that even the first text box took the second string.
Here the xml code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/istruzioni"
android:id="#+id/istruzioni" />
</TableRow>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/inserimentoCodice"
android:id="#+id/inserimentoCodice"
android:layout_gravity="left" />[...]
The two string are istruzioni and inserimentoCodice .
Here my strings.xml file
<string name="app_name">Controllore</string>
<string name="controlla">Controlla corsa</string>
<string name="nuova">Nuova corsa</string>
<string name="quit">Quit</string>
<string name="istruzioni">Dopo aver inserito il codice del documento di identità dell'utente, verrà visualizzata l'ultima convalida, e confermata la sua eventuale validità</string>
<string name="inserimentoCodice">Inserire codice documento:</string>
If in the first file I put the mouse over the inserimentoCodice line, I get this:
Why Android studio decides to overwrite another string?
I'm working on ubuntu 13.10 with Android studio

Android;Body of ProgressDialog?

I saw this source as layout of AlertDialog in res\layout folder of Android SDK:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/body"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:paddingLeft="8dip"
android:paddingTop="10dip"
android:paddingRight="8dip"
android:paddingBottom="10dip">
<ProgressBar android:id="#android:id/progress"
style="#android:style/Widget.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="10000"
android:layout_marginRight="12dip" />
<TextView android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</FrameLayout>
I tried to find TextView that it's id is "message" like this:
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
It works fine.Then I tried to find LinearLayout that it's id is "body" like this:
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
But eclipse show this error:
body cannot be resolved or is not a field
This is snippet code:
ProgressDialog dialog = new ProgressDialog(WriteOpinionActivity.this);
dialog.setMessage("some text");
dialog.show();
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
Why this error occurs and how I can reach "body"?
Basically, not all resources are accessible to You from platforms res folder.
E.g. I'm able to find #+id/message in global_actions_item.xml, usb_storage_activity.xml, alert_dialog_holo.xml, js_prompt.xml, progress_dialog_holo.xml, progress_dialog.xml, alert_dialog.xml (for API 17), and so, there's no warranty that You get id which defined in progress_dialog.xml (it can be defined in another file and just because of the same name You is able to find it in progress dialog).
Actually, public resources are defined here, however, it looks quite outdated, so some resources might be missed. Checkout these questions about lists of all accessible resources: 1 and 2
Use this instead.
TextView tvMessage = (TextView)dialog.findViewById(R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(R.id.body);
You weren't actually using android.R.id.message in the first place. #+id/message means you are creating a new id resource named message.
To use an id that exists in the Android framework, you can see here that you use #android:id/progress. And in source, you use it like you had it: android.R.id.progress.
Otherwise, use android:id="#=id/myIdentifier" in XML and
findViewById(R.id.myIdentifier) in code.
http://developer.android.com/guide/topics/resources/overview.html

HTML won't be interpreted in my Android project

I'm facing a strange issue, that I could not fix with the solutions I found on related topics. (through StackOverflow or other sites)
It is summarized on that StackOverflow topic :
Display HTML Formatted String
So I am asking you. Why the heck is none of those solutions working in my project ?
I even tried to use WebView instead of my TextView ! Nada ! (html tags seem to be simply ignored)
This is my TextView :
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#+id/signupLinkText"
android:text="" android:gravity="center_horizontal"
android:clickable="true" android:layout_margin="5px"
android:layout_marginTop="10px" android:layout_marginBottom="10px"></TextView>
And the code I use to put my html code
Spanned spannnedSignupLink = Html.fromHtml(getString(R.string.lbl_not_yet_subscribed));
signupLinkText.setText(spannnedSignupLink, BufferType.SPANNABLE);
Then The html that must be interpreted in strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- ... -->
<string name="lbl_not_yet_subscribed">To sign up to RESA Mobile, <strong>click here</strong> !</string>
</resources>
Thank you for the time you've spent in reading me,
Regards
Post the html your attempting,
This works for me:
<TextView
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:textColor="#FFFFFF" />
TextView textView = (TextView) findViewById(R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String message = "Blah blah blah Google Link blah blah blah.";
textView.setText(Html.fromHtml(message));
This should work:
TextView textView = findViewById(R.id.signupLinkText);
textView.setText(Html.fromHtml(...put the HTML here...));

Android setting with TextView for Hebrew text?

I am setting text in TextView from string resource. Normally, Hebrew works in Right-To-Left format. When I set a text, it sets the text Right-To-Left format in LG, Samsung, Sony Phone but in HTC it does not work. It works in Left-To-Right format in HTC. Even I set Gravity to the TextView in Java file.
Text in TextView should be span according to the screen size. For example if it is 320 x 480 then it display in 4 lines but if it is Galaxy Tab then there may be 2 lines.
Here is my Code snippet:
In Java:
private TextView mVersionInfo, mVersionDescriptionOne, mVersionDescriptionTwo, mVersionDescriptionThree;
mVersionInfo = (TextView)findViewById(R.id.VersionInfo);
mVersionDescriptionOne = (TextView)findViewById(R.id.VersionDesc1);
mVersionDescriptionTwo = (TextView)findViewById(R.id.VersionDesc2);
mVersionDescriptionThree = (TextView)findViewById(R.id.VersionDesc3);
mVersionDescriptionOne.setGravity(Gravity.RIGHT);
mVersionDescriptionTwo.setGravity(Gravity.RIGHT);
mVersionDescriptionThree.setGravity(Gravity.RIGHT);
in XML:
<TextView android:id="#+id/VersionDesc1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc1" android:textColor="#000000"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/Share" android:textSize="13sp"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc2" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc1" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
<TextView android:id="#+id/VersionDesc3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/versiondesc3" android:textColor="#000000"
android:layout_alignParentRight="true" android:layout_marginRight="10dip"
android:layout_below="#+id/VersionDesc2" android:textSize="13sp"
android:layout_marginTop="5dip" android:gravity="right"
android:layout_alignRight="#+id/Body" />
In String Resource:
<string name="versiondesc1">האפליקציה מתחברת לאתר הספק הסלולרי כדי להציג את מצב החשבון. לעיתים, כשאתר הספק איננו עובד תקין לא יהיה ניתן לקבל מידע. באם אתר הספק ישתנה האפליקציה עלולה להפסיק לעבוד. במצב כזה האפליקציה תחזור לעבודה תקינה מיד לאחר שאנו נתאים את שרת התוכנה שלנו לשינויים.</string>
<string name="versiondesc2">הערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string>
<string name="versiondesc3">אם נתקלת בבעיה, השתמש/י בכפתור יצירת קשר על מנת שנוכל לפתור אותה. נשמח לקבל כל משוב על האפליקציה.</string>
What is wrong with my code?
Anybody who has worked with another language, Please guide me here.
Thanks.
Gravity will only affect alignment and will not set base direction for the text. That it works on some devices and not others may be a font issue, or perhaps an OS version issue. Try adding a RIGHT-TO-LEFT MARK character (\u200F) at the start of your text. This might help the display on an HTC and will not hurt anything on devices where it is already working.
here is an example from my hebrew string xml, Thanks to Ted Hopp's answer:
you need to add '\u200e' before the char that causes you the problem:
<string name="basic_text1">המר על תוצאת המשחק\u200e:</string>
and the result will be:
:המר על תוצאת המשחק

Categories

Resources