Search code examples
androidspecial-characters

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


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


Solution

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