23

Simple question: I want to set a TextView and in that I want to have three dots (Ellipsis). Like

Read more... <-- Now I'm sure I shouldn't just write ... into the String. How should I write these three dots?

Musterknabe
  • 5,763
  • 14
  • 61
  • 117

4 Answers4

35

Write "\u2026" in your String literal. See http://www.fileformat.info/info/unicode/char/2026/index.htm

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
14

You can use the UTF-8 character "Horizonal ellipsis" (U+2026), "\u2026":

http://www.fileformat.info/info/unicode/char/2026/index.htm

Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
  • 1
    UTF-8 is not the default encoding for java on macs. It uses MacRoman instead. – Sri Harsha Chilakapati Nov 15 '13 at 09:44
  • 2
    It doesn't matter in GUI. Java uses UTF-16 to represent strings internally. The encoding matters only if you want to input or output text from file etc: http://stackoverflow.com/questions/9699071/what-is-the-javas-internal-represention-for-string-modified-utf-8-utf-16. Authorative source is here: http://docs.oracle.com/javase/7/docs/technotes/guides/intl/overview.html, see "Text Representation" – Erik A. Brandstadmoen Nov 15 '13 at 09:48
  • I didn't know that they don't effect the GUI. I've only said that because I've experienced this when reading XML files made with a native OS X app. – Sri Harsha Chilakapati Nov 15 '13 at 09:51
  • 4
    Reading XML files is completely different from using a `\u` in a String literal. – Dawood ibn Kareem Nov 15 '13 at 09:52
4

I suggest to add this to your code:

interface CommonConstants {
    String ELLIPSIS = "\u2026";
}

You can now import that anywhere you need it. The name will make it easy to know what this odd Unicode string might mean.

Note that this is safe to use with any editor since the source only uses ASCII character to encode the information.

If you're sure that all parts of your build process (editor, compiler, ...) are UTF-8 safe and configured to use UTF-8 and you have the font, you can enter the Unicode using any of the usual methods of your OS (maybe Cut&Paste would be most simple).

note: Eclipse can handle UTF-8 but many people have configured it to use the default = platform encoding so they don't get what they expect.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

Now I'm sure I shouldn't just write ... into the String.

Why? Are you having some sort of issue with how text is handled by the application or operating system? What benefit is served by using a special Unicode symbol instead of a character built into every keyboard out there? You save a byte of data?

I can't imagine how I could be the first one to put this into an answer, but just use three dots.

James
  • 1,239
  • 1
  • 11
  • 18