special characters in layout xml of Android project - android

I again have a problem with special characters.
In this case it is in a res\layout\mylayout.xml
When using characters like 'ó' and 'í' my project doesn't compile.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RadioGroup
android:id="#+id/radiosearchoptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/searchtitleonly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="buscar sólo en el título"
any hints ? thanks.

To use those characters you have to define a new string in your strings.xml file like this:
<string name="titulo">buscar sólo en el título</string>
And then, you add it to the text property of your RadioButton like this:
android:text="#string/titulo"

Save the file in the same encoding (utf-8) you declare in the ?xml header. If you save in some other encoding, there may be octet sequences that are not valid utf-8.

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.

some ideas of how export a hymn to XML without losing the position of chords

I created a program to move from PDF to XML, but for some reason the chords change places unlike the original. I wanted to ask to see if you give me any idea to help me. I'm a bit stuck
Actualy this is my xml code:
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:orientation='vertical'
android:layout_width='match_parent'
android:layout_height='match_parent' >
<TextView
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:fontFamily='sans-serif'
android:typeface='serif'
android:textAppearance='?android:attr/textAppearanceLarge'
android:text=' G C G G C Em D C G ' android:textSize='15sp' />
<TextView
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:fontFamily='sans-serif'
android:typeface='serif'
android:textAppearance='?android:attr/textAppearanceLarge'
android:text='¡Aleluya, aleluya! Te alabamos, Señor, nuestro Rey. ' android:textSize='18sp' />
</LinearLayout>
This is the original
and my final result in android
I tried changing the font size, change of position but always wrong.
I want to use text no images.
Thanks for your 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: html in strings.xml

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>

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

Categories

Resources