Set text colour in strings started min SDK version 7 - android

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

Related

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.

Use string from MainActivity.java in activity_main.xml

I have a string defined in MainActivity.java:
public String counter1 = String.valueOf(e.getCount());
I would like to use this string in activity_main.xml as:
android:text="#string/counter1"
As you can tell I am very new to this so basic steps would be appreciated.
Thanks
Short answer: you cannot. The resources you set in your xml layouts must be statically defined in resources files, like.-
<string name="counter1">COUNTER VALUE</string>
To dynamically define new strings, you must set them programmatically.-
TextView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(counter1);
What do you want to do if you could get this to work? If you want to set text dynamically, you can use setText:
yourTextView.setText(counter1);
If you want to set something programmatically which you can... you should use
txtview.setText(yourString);
if you want to set a String in XML then you either set it in the XML like you did
android:text="Exercise Name"
or using strings which then you can use programmatically as well
in strings:
<string name="Delete">Delete</string>
in code you can call it with R.string.Delete or getString(R.string.app_name);
of course you can also set from strings in XML with #string...
by the sound of what you are trying to do, you want the TextView to change while program is running so the first option would suit you well
you can not,if you want to acheive this kind of solution:
create string in strings.xml
<string name="counter1">value of counter</string>
and if you want to set the text of counter dynamically then in you activity:
TextView tView = (TextView) findViewById(R.id.textViewId);
tView.setText(counter1);
you should follow this,because all static strings shoul always be defined in strings.xml

Android TextView in BOLD and Normal text

I thoroughly searched on SO but didnt get answer of my question.
I want to set a paragraph, I will set it in XML using
The text contains Title and Steps and regular text. I want to make the Title and Steps in bold and rest in normal text.
I can do this by using different 's but how can I do it in the same TextView.
I mean using the same TextView how can I set different attributes for different sentences?
Use a Spannable String
TextView tv = (TextView) findViewById(R.id.tv);
String steps = "Hello Everyone";
String title="Bold Please!";
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(steps);
For more styling check the link # http://blog.stylingandroid.com/archives/177
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
And output
You can format it like you would in HTML: let's call this custom_text
<b>Your title here</b>
This is the non-bolded stuff.
And then load the text using the Html class:
mTextView.setText(Html.fromHtml(getString(R.string.custom_text)));
That will create a spannable string and set it on the TextView.
please put this string in to res->string.xml
<string name="your_html">
<![CDATA[p><b>This is bold text</b> rementing is simple text
]]>
</string>
Now you can used whenever you have to require this thing.
tv.setText(Html.fromHtml(getString(R.string.your_html)));
It is bestway and work charm.
TextViews support SpannableStrings. You can either make your custom String or format your string in html and then set it with tv.setText(Html.fromHtml(yourString));
kotlin Solution :
For me, the issue was I had a text set in XML through data binding. I had to remove that. then this started taking effect
val spannableStringBuilder = SpannableStringBuilder(headerText)
spannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), 0, headerText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableStringBuilder.append(" ")
spannableStringBuilder.append(subText)
textView.text = spannableStringBuilder
The best way to do this is in the Strings resources.
<string name="sample_string"><![CDATA[<b>Title</b>StackOverflow]]></string>
Notice, the text between bold tag (<b> </b>) will appear in bold. Similarly, you can set other styles. For eg. <i> for italics and <u> for underline.
Hope this helps, good luck!
Instead, use this :
First, declare textView
TextView tv1 = (TextView) getActivity().findViewById(R.id.t1);
Then set it's text to the string you want
tv1.setText(Html.fromHtml(getString(R.string.my_text)));
Lastly, use set the typeface for your textView
tv1.setTypeface(null, Typeface.BOLD);
And, you are done.

String size and font change in string.xml file

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!

android: string format specify bold

I have a string defined in string.xml like
<string name="eventtitle">Title: %1$s </string>
which is formatted using string.format . How to define the string to get only the Titel: as Bold.
Thanks in advance for the help
You can do it like,
textView.setText(Html.fromHtml("<b>Title</b>: Text"));
If you have text in dynamic way..
And to define formatings in Strings.xml you can do like,
<string name="text1">This text uses <b>bold</b> and <i>italics</i>
by using inline tags such as <b> within the string file.</string>
see This Link
Now, it is reasonably supported by Android.
you can define the string in xml as <string name="text_to_show">Hey <b>This is in bold</b>
Then in code, use this to convert to CharSequence and then use it for e.g in TextView
String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);
Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by #Wahib. Here's the improved version:
Define the string resource as a:
<string name="styled_text">Hey, <b>this is bold</b> text</string>
Use the resource like this:
String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);
Here's the result:
You can use HTML markup like "<b>BOLD</b> other text ...".
See this Google resource for more information.
You have to style your string in the TextView in which in is displayed. See this link
Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf");
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF");
String strt_title_desc=this.getResources().getString(R.string.eventtitle);
int upto=strt_title_desc.indexOf(":"); //of you can specify 5
if (strt_title_desc!=null)
{
aboutAuthTV.setTextColor(Color.BLACK);
aboutAuthTV.setLineSpacing(1.2f, 1.5f);
aboutAuthTV.setTextSize(23);
SpannableString SS = new SpannableString(strt_title_desc);
SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourtextView.setText(SS);
}
// this for changing the font size, style and colors
String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
<i>Then Italic</i> </font>" +
"<br/> <font color=\"green\" >this is simple sentence </font>" +
"<br/><br/><br/><br/><a>this is simple sentence</a>";
Spanned strHtml= Html.fromHtml(str);
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(strHtml);
Now, it is reasonably supported by Android.
I have already given answer to this how to add styling within a string

Categories

Resources