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);
Related
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
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?
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);
My Application require is share text with facebook.I'm able to share text using this code.
Here I'm giving parameters like this:
Bundle myParames = new Bundle();
myParames.putString("message", message);asyncFacebook.request("me/feed", myParames,"POST", new FacebookPostListener(), null);
But I want share the text every letter with corresponding font like,
if myString = "stack"; then,
String newString = null;
newString.replace("s", "Ⓢ");
newString.replace("t", "Ⓣ");
newString.replace("a", "Ⓐ");
newString.replace("c", "Ⓒ");
newString.replace("k", "Ⓚ");
Then I'm able to share my text with replaced fonts.
But I want apply font from .ttf file and then share.
I tried SpannableString, But it can't convert into .string().
So I need to change library?
Those characters you are doing your replacements with are no font, but characters. Unicode characters, to be precise.
Enclosed C or circled Latin C (Ⓒ or ⓒ) is a typographical symbol. As one of many enclosed alphanumerics, the symbol is a "C" within a circle.
The capitalized symbol (Ⓒ) can be generated with the unicode encoding "U+24B8" and the UTF-8 (hex.) encoding "e2 92 b8".
Source: http://en.wikipedia.org/wiki/%E2%92%B8
So, you are replacing one character with another, and that is all you can do.
You can't pass a custom font embedded in your text to another application through something like an intent. This is not possible.
I want to show registered or trademark symbol in android string.I tried using Unicode characters like
® and #reg; also.But it's not working.Can somebody please tell me how to display these symbols.Thanks in advance
Add this line in your TextView code
android:text="\u00AE"
Also try this Ⓡ symbol use ® and trademark ™ symbol ™
Add this line in your strings.xml
<string name="registered_symbol">®</string>
Variant 1 -- just paste the appropriate character
make sure you have the strings.xml in correct encoding: <?xml version="1.0" encoding="utf-8"?>
get the character from e.g. here:
REGISTERED SIGN: http://www.fileformat.info/info/unicode/char/ae/index.htm
TRADE MARK SIGN: http://www.fileformat.info/info/unicode/char/2122/index.htm
Variant 2 -- use html entities
In the linked pages you can also find the corresponding HTML entities ® and ™.
you have to set the text as HTML in your code, like this:
yourTextView.setText(Html.fromHtml(getString(R.string.your_text_entry)));
I hope this helped.
Declare a String in string.xml file like below,
<string name="registered">®</string>
Now you can access it by calling this String variable.
If you want to do it by html then try this, way
String htmlRegistered = "<html><body>My Trade mark is ®</body></html>";
Update from your Comment:
private String[] m_listImmunisations = new String[]
{ "<html><body>Hepatitis B (H-B-Vax® II)</body></html>",
"<html><body>HPV (Cervarix®)</body></html>",
"<html><body>HPV (Gardasil®)</body></html>"
}
Use "\u00AE" to replace that symbol.
To make it on top right corner, you can use SuperscriptSpan like following, replace the startIndex and endIndex with the position of your trademark character. Be careful with the size of the text, you may need some density independent size here probably to make it work on all screens.
Spannable span = new SpannableString(title);
span.setSpan(new TextAppearanceSpan(null, 0, 60, null, null), (int)startIndex, (int)endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new SuperscriptSpan(), (int)startIndex, (int)endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);