pass value to string.xml value that have a link inside it - android

I have this value in string.xml file:
<string name="rights">Copyright © All Rights Reserved %1$d <b> company name</b></string>
And I have applied this code that pass to the above text & set that text to the TextView
private void setupAppInfoRights() {
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String rights = String.format(new Locale("en"), getString(R.string.rights), currentYear);
appInfoRights.setText(rights);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
}
When I remove the passed value everything goes fine & when the user click on the company name it takes him/her to the company website.
Please note that I have tried autoLink in xml when there is no value passed but it does not work as expected.
But, when I add the passed value & used the code above the company name has no underline & when the user clicks it , it will do nothing.
How to edit my above code to pass the current year & keep the link behavior normal?
Note: I have used String.format to display the current year as English number always despite the other locale numbers.

I think SpannableStringBuilder is what you are looking for.
TextView linkTv = (TextView) findViewById(R.id.link_tv);
linkTv.setMovementMethod(LinkMovementMethod.getInstance());
Spannable span = (Spannable) linkTv.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget){
//open the link
}
};
span.setSpan(clickableSpan, 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//for bold
span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
If you want to make only certain part clickable then toggle the values of 0 and span.length() in setSpan().

When you have a string resource with both format arguments (like %1$d) and html markup, you have to use a multi-step process to create the styled CharSequence. This extra work is necessary because both Resources.getString(int, Object...) and String.format(String, Object...) can only return String instances, and not other CharSequence subclasses that are capable of holding styling information.
First, change your string resource to use html entities to escape the html tags:
<string name="rights">Copyright © All Rights Reserved %1$d <b> <a href="http://www.example.com/">company name</a></b></string>
Next, obtain a String with the format arguments replaced with the actual values you desire:
String withHtmlMarkup = getString(R.string.rights, currentYear);
Finally, use Html.fromHtml() to parse the html markup:
CharSequence styled = Html.fromHtml(withHtmlMarkup);
Then you can set this styled text to your TextView as normal:
appInfoRights.setText(styled);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
Developer guide: https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
Normally, this doesn't work because the format(String, Object...) and getString(int, Object...) methods strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

Related

How can I change some of statements' text style in Firebase?

I'm trying to show some statements to users from Firebase. There are titles and contents in these statements. Trying to write titles with a boldly styled post. However, I cannot change the code from the android studio because I've assigned the whole description child to a single TextView. instead, I used expressions such as "\ n", which is also used in string expressions. Is it possible to write bold headlines? if so how?
For example ; here is my
Can I write "ilk olarak bulunduğunuz federal devletteki bir..." here by a bold style?
yes you can display, the approach i use is setting html from firebase
<p><b>This text is bold</b></p>
and in android side
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tvDesc.setText(Html.fromHtml(strfromfirebase, Html.FROM_HTML_MODE_COMPACT));
} else {
tvDesc.setText(Html.fromHtml(strfromfirebase));
}
You need to use a SpannableString in this case.
For example if I want to bold the word "Hello" in "Hello World", I would do something like:
String word = "Hello World";
SpannableString spannableString = new SpannableString(word);
spannableString.setSpan(new StyleSpan(Typeface.BOLD),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
0 is the starting index and 5 is the ending index

Setting spannable string in android with combination of non-spannable strings

I have several strings uploaded to a String[]. These strings are set on a textview. However, I set the previous strings on my textview, as well. I need some of the strings to be red colored. I've heard of the SpannableString class which does exactly what I want, but the following call defeats my purpose of setting all the previous strings.
if(string from String[] contains ("R2")){//pseudo code
//build spanstring with all its properties
//combinedstrings = combinedstrings + spanstring;
}
else{
//string = string from String[]
//combinedstrings = combinedstrings + string;
}
textview.setText(spanstring, BufferType.SPANNABLE);
As you can see, this call only allows to set the spanstring not the combination of the spanstring including the non-spannable strings. I need the combination of both to be set at the same time.
I need a call that would set the text on the textview of both types of strings
the non-spannable and spannable ones.
thanks for any suggestions
Build the CharSequence you pass to setText with a SpannableStringBuilder. See https://developer.android.com/reference/android/text/SpannableStringBuilder.html This class allows you to append either string or spans and can be built into a single CharSequence.

TextView: set Html and own SpannableStringBuilder

I have a string that contain html tags. So for set text into TextView I use:
textView.setText(Html.fromHtml(myString));
It's works good.
Also I have own method convertText() with type SpannableStringBuilder. In which method I convert text, where I have something like [text="information"]. I use next for set text:
textView.setText(convertText(myString), TextView.BufferType.SPANNABLE);
For example: myString = "<h1> This is [text="information"]. </h1>"
The result must be:
This is INFORMATION. (with h1 formatted text. caps lock of word information works in method convretText() good)
It's also works good. But how I can use these two setText approach in one time?

Android formatting xml string loses its html tags

I have a xml string below that contains html tags such as Bold and a hyperlink href.
<string name"example"><b>%1$s</b> has been added to your clipboard and is valid until <b>%2$s</b>Please see our +T\'s and C\'s+ </string>
when i format the string to dynamically add some values it removes the bolded text and href I defined.
code below:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy");
Date date = new Date(text.getValidTo());
String text = String.format(getString(R.string_dialog_text), text.getCode(), simpleDateFormat.format(date), "£30");
I then apply it to a alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setNeutralButton(R.string.ok, onClickListener);
builder.setTitle(title);
builder.setMessage(text);
this worked fine if i dont format the text and directly use the string resource
use Html.fromHtml
builder.setMessage(Html.fromHtml(text));
when you apply the formatting, the CharSequence is converted back to String, and you need the Spannable with the html information.
From the doc:
Sometimes you may want to create a styled text resource that is also
used as a format string. Normally, this won't work because the
String.format(String, Object...) method will strip all the style
information from the string. The work-around to this is to write the
HTML tags with escaped entities, which are then recovered with
fromHtml(String), after the formatting takes place.
try with <b> in place of <b> and with </b> in place </b>
Suppose that you want this
<resources>
<string name="our_string_key">
<B>Title</B> <I>Italic</I><BR/>
Content
</string>
</resources>
Then in that case you yse it as
textview.setText(Html.fromHtml(R.String.our_string_key));
If in case you are getting errror then you can use it as
textview.setText(Html.fromHtml(getResources().getString(R.String.our_string_key)));

How to get this special space character ␣ into an Android Button?

I want to symbolize a space in an Android Button so:
in the strings.xml file I define <string name="SpaceSymbol">␣</string>
in the layout XML file I set android:text to "#string/SpaceSymbol"
When I run the application the button shows nothing.
Any ideas?
If any other character that symbolizes a space works other than ␣ I'll be glad to use it
Use the Html code corresponding to ␣ character, HTML Codes Table or HTML Special Characters
the define into the strings.xml
<string name="SpaceSymbol">␣</string>
This is an example with & character:
How can I write character & in android strings.xml
Update:
Answering your question, ␣ is non-printable character, it is just a "Space", if you want show this symbol into the button, try loading an image of that symbol into an ImageButton.
You can wrap the space char in
![CDATA[ ... ]] in the string resource..that may help you bring it in. You should check that the character is in the roboto font also
Given that the character is not represented in the Roboto font, you can use a SpannableString to replace the special character with your own image:
you should create a Drawable (or Bitmap) with the space image in it that you want to use. in this example it would be assigned to the variable 'drawable':
SpannableString textspan = new SpannableString(sb);
Pattern pattern = Pattern.compile("#_", Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(textspan);
while(matcher.find()) {
textspan.setSpan(new ImageSpan(this, drawable, ImageSpan.ALIGN_BASELINE), matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
In your string (in this code example) you would have replaced your space symbol with the two characters "#_". The pattern will find each occurence of "#_" and replace it with the drawable.
Then you can use the SpannableString as a CharSequence argument anywhere it's defined (Toast.makeText, TextView.setText, etc)
like this :
TextView tv = (TextView) findViewById(..);
tv.setText(textspan);

Categories

Resources