String size and font change in string.xml file - android

In my android application, i have to write a string into a file. Before that I want to format the string to make the font bold and have a new text size. So I modify the string.xml by
"<string name="file_title" ><b>#title*</b></string>",
for bold the text and wrote to text file. But it is not showing as bold in the file.
Is that right method i used to implement the string bold?
Also i want to know how to change the text size of a string direct
from string.xml
how to add that boldness in file... i tried
"<string name="note"><![CDATA[<b>title</b>]]></string>"
in java code get your string
yourtextview.setText(Html.fromHtml(getString(R.string.note)));
by using texview it"s coming bold , exaclty i want to write to file, using the below code
"bufferWritter.write(String.valueOf(Html.fromHtml(getString(R.string.note))));"
but i can"t see the boldness in my print.txt file...
Thanks & Regards
Anoop

You Should try to use dimens.xml for dimension values
then reference it #dimen/yourDimensionName
Refer this link for
Or other way is using HTML from java code
I tried this is working.
In your string.xml file try this
<string name="test"><![CDATA[<b>test</b>]]></string>
In your java code settext this string value from string resource like this
textview.setText(Html.fromHtml(getString(R.string.test)));
Its working fine....

Try this
in string.xml write like this
<string name="note"><![CDATA[<b>title</b>]]></string>
in java code get your string
yourtextview.setText(Html.fromHtml(getString(R.string.note)));

This is not the right way. Just change its size from xml directly. use.
android:text="Date"
android:textStyle="bold"
android:textSize="xdp"

In order to do that, you should have your xml resource as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="file_title"><b>title*</b>!</string>
</resources>
And then use:
Resources res = getResources();
String text = res.getString(R.string.file_title);
CharSequence styledText = Html.fromHtml(text);
In order to change the text or other parameters, you can add a <span></span> with specific css style around your text.
For more information please check here: http://developer.android.com/guide/topics/resources/string-resource.html
Hope this helps!

Related

Android Charsequence - replacing text

Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);

android: show font icon as a sequence of number string

I want to put a Font-Awesome icon inside textview text. After I set the text, android shows me a string sequence instead of font-icon.
My code is:
String formatedSection = formatedSection + sections.get(i).getContent() +getResources().getString(R.string.icon_ref);
I define icon_ref in string.xml as below:
<string name="icon_ref"></string>
I followed these instructions to add font-icon. What am I doing wrong?
If you did step 5 in that short guid your problem seem to forget that there is a diff between java and XML.
In XML you use XML escape (&...;), in Java you'll probably have to use Java escape (\u...). – Biffen May 20 at 10:10
Try to hard code the string into "\uf075" and it would work like a charm.

Set text colour in strings started min SDK version 7

I have this line in my code.
String credit= "<font color='#0166b9'>Credit: </font>";
I need to get "Credit: " string from my resources, because It can change depended of device default language. So how I can use value of some variable in this line ?
EDIT:
I must have only one word or some text coloured. It means that I must have different coloured TextView.
There is some way to set colour in your R.strings , here is the example
<string name="clients_credit"> <font fgcolor="#0166b9">Credit: </font> </string>
But it works only for latest versions. So it'll be great to find some way to setting colour in string.xml.
Use can use Html.fromHtml();
You can define your string in string.xml as
<string name="title"><![CDATA[<font color='#0166b9'>Credit: </font>]]></string>
And in java file you need to use spannable text
textView.setText(Html.fromHtml(getResources.getString(R.string.title)), TextView.BufferType.SPANNABLE);
You can do this :
Textview tV;
// Initialize textview
tV.setText(R.string.credit);
tV.setBackgroundResource(R.color.background_color);
tV.setTextColor(getResources.getColor(R.color.text_color));
Add Credit: to your strings file and then retrieve it later on like getResources().getString(R.string.credit) and add colour like getResources().getColor(R.color.textColour). Add
<color name="textColor">#0166b9</color>
to your color XML in res folder. If you don't have one create one.
Put file named colors.xml under res/values of your Android project.
Declare the color in that file
<color name="myColor">#ff0166b9<color>
Declare the string in strings.xml under res/values
<string name="credit">"Credit: "</string>
In your Activity, get reference to TextView defined in activity layout using the
textView = (TextView) findViewById(R.id.idOfTextView);
and then use the following to set the string and text color.
textView.setText(R.string.credit);
textView.setTextColor(getResources().getColor(R.color.myColor));
Hope this helps.
I found the solution. Here is it.
TextView debitTxtView = (TextView) view.findViewById(R.id.txtViewclientDebit);
final Spannable clientsDebit= new SpannableString(mContext.getString(R.string.clients_debit));
clientsDebit.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue)), 0, clientsDebit.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
debitTxtView.setText(clientsDebit);
debitTxtView.append(" " + file.getDebit());

Android: text from strings.xml displayed as #numbers

I have updated some string values in strings.xml and my application now shows not the new text but something like #234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?
To obtain a string from your strings.xml file, you can do a few things.
If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.
If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.
In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:
TextView.setText(12345680);
Are you trying to read it directly as R.string.my_string_resource?
Try passing it to getString() as getString(R.string.my_string_resource).
You can put your stings.xml file in the follwing format.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>
and use the name as reference of your textbox ids like.
android:text="#string/app_name"

How to use strings in android

I know this is a simple question, but I can't figure out how to reference strings in android.
for example
on this EditText, I don't want to hardcode the string hi, I want it to reference a string in the res folder. How do I do this?
Suppose you have following string in xml.
<string name="string_one">My string</string>
You have to access this in code :
String str = resources.getString(R.string.string_one);
You can also used in xml where you have take EditText :
android:text="#string/string_one"
Create a file strings.xml in res/values folder, and add a text like
<string name="card">Card</string>
then reference the text in layout as android:text="#string/card"
Figured it out. All you need to do is create a string in the res value folder and then reference it
Create a file strings.xml in res/values/ folder, and add a text in it like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="mystring">MyStringFromXML</string>
</resources>
then reference the text in layout as :
android:text="#string/mystring"
And in Java file do it like:
String string = getString(R.string.mystring);

Categories

Resources