0

I want to symbolize a space in an Android Button so:

  1. in the strings.xml file I define <string name="SpaceSymbol">␣</string>
  2. 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

Pavitx
  • 81
  • 2
  • 17

2 Answers2

3

Use the Html code corresponding to character, HTML Codes Table or HTML Special Characters

the define into the strings.xml

 <string name="SpaceSymbol">&#9251;</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.

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Does not work. Even if use Html.fromHtml to load it, like this: mEspaiButton.setText(Html.fromHtml(getResources().getString(R.string.SpaceSymbol))); – Pavitx Apr 11 '14 at 12:47
  • Hello Pavtix, ␣ is non-printable character, is just a "Space", I see you want show the `␣` symbol, i think you must use an image into an ImageButton :(. – Jorgesys Apr 11 '14 at 14:39
  • I will try this. In iOS the same character does appear in a Button, although iOS and Android are different worlds. – Pavitx Apr 11 '14 at 15:35
  • Oh didn't know it was just a space. Good to know. – Ahmad Apr 13 '14 at 22:43
  • I would say it's not a space, but a printable character. The fact that it does not appear may be linked to the fact that the character does not exist in the Roboto (default) font. See the answer by dangVarmit. – Pavitx Apr 14 '14 at 16:18
1

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);
dangVarmit
  • 5,641
  • 2
  • 22
  • 24
  • Wrapping the space char in ![CDATA[ ... ]] in the string resource makes the character appear in the layout preview in my IDE (Intellij IDEA Community Edition), however in the application it does not work. Looking at the Roboto documentation, it appears this character is not included in the font but haven't seen anything fit to symbolize a space. – Pavitx Apr 14 '14 at 16:19