Search code examples
androidunicodeencodingletters

German unicode letters don't show up in app


I have a problem with language encoding... what i try is to incorporate a random motivational string into my app which contains German unicode letters... as far as I know, Java uses Unicode-16, but the respective letters dont show up at all when I start the app. I'm using Android Studio and the app is tested on a real device.

public class Start extends ActionBarActivity {

//This is a string array containing quotes
String[] motivational = {"Unser größter Ruhm ist nicht, niemals zu fallen, sondern jedes Mal wieder aufzustehen.\nRalph Waldo Emerson"};

public int randInt() {
    Random rand = new Random();
    return rand.nextInt((motivational.length) + 1);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    //Takes a random quote from the list and sets it as a TextViews content
    TextView motivator = (TextView) findViewById(R.id.motivator);
    motivator.setText(motivational[randInt()]);
}

//rest of the class

Solution

  • Add your text to strings.xml and then use Android's getResources.getString() method to get the text

    <resources>
    
      <string name="motivational">Unser größter Ruhm ist nicht, niemals zu fallen, sondern jedes Mal wieder aufzustehen.\nRalph Waldo Emerson</string>
    
    </resources>
    

    Then in your java file, use

        String motivational = getResources().getString(R.string.motivational);
        TextView motivator = (TextView) findViewById(R.id.motivator);
        motivator.setText(motivational);
    

    Retrieving the value from strings.xml is done using

    getResources().getString(R.string.motivational) 
    

    where R.string.motivational is the unique string identifier.

    Isn't this the output you are looking for?

    enter image description here