0

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
AdHominem
  • 1,204
  • 3
  • 13
  • 32
  • Use Unicode UTF-8 ``. – Phantômaxx Jun 19 '15 at 14:12
  • Already have all the XML files encoding in UTF-8... – AdHominem Jun 19 '15 at 14:17
  • Weird... it works for me. Where are the strings stored? In a text file? If so, this text file must be saved with the Unicode UTF-8 encoding, not Windows standard CodePage 1252. – Phantômaxx Jun 19 '15 at 14:21
  • In the regular res/values directory, but those strings work fine even though they include German letters. The problem seems to originate in the fact that the string I use here is hardcoded in the Java file... I guess the only workaround would be by making each quote an XML reference, just think that would be a lot more work than simply including them in the code... – AdHominem Jun 19 '15 at 14:25
  • 1
    Maybe the Java file itself is not saved as UTF-8. Or your workspace isn't set in the preference to use UTF-8. However, best practice wants you to set the strings into a resource file. – Phantômaxx Jun 19 '15 at 14:27
  • I just wonder because it works fine with English strings... It just seems very inconvenient to do this on a large amount of strings and then putting them into an array after referencing each one of them... – AdHominem Jun 19 '15 at 14:49
  • English strings don't contain special (umlauted vowels and beta) characters. – Phantômaxx Jun 19 '15 at 14:50
  • And this is why I think there has to be some way to do this without using resources... already set all possible encoding options accordingly. Well, if it doesn't work, I probably need to put them all into the strings file. – AdHominem Jun 19 '15 at 14:53
  • Or better, into the arrays file (or, why not? into a database table), so you can pick a random one. – Phantômaxx Jun 19 '15 at 14:59
  • 1
    This sounds like a good solution... what would be the best way to access a string item inside an XML string array? Simply making a reference to the array like String[] some_array = getResources().getStringArray(R.array.your_string_array) and then using the some_array to fetch random strings? – AdHominem Jun 19 '15 at 15:08
  • Yes, more or less like that. Use a Random object to get the element at a random index (0 ... elements - 1). – Phantômaxx Jun 19 '15 at 15:10

1 Answers1

1

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

capt.swag
  • 10,335
  • 2
  • 41
  • 41
  • If this solved your query, you should mark this answer as correct @OP – capt.swag Jun 19 '15 at 14:33
  • 1
    Well, this is a solution as stated above, but it would be rather inconvenient to put a great number of strings into the res file and then reference them in an array instead of using them directly... I know that best practise is to keep strings in the res file anyways. I just wonder if it could be possible, since it would work perfectly with English strings. – AdHominem Jun 19 '15 at 14:40