5

I would like to know if it always best practice to store String values in the strings.xml file, even when the String(s) are really large. To be more specific, I have a game, where I display the rules of the game. The sum of all characters is bigger then 700 characters. Currently I break those long Strings into smaller Strings (into paragraphs). So I was wondering, would it be considered as good practice to have those long Strings, containing more then 700 characters? I know the HEAP size has to be considered how much characters can be addressed, but I doubt you can easily hit the limit. From what I was reading Java has the limit set to (2^31 - 1) characters and in Android to somewhere 4-64 million characters.

Community
  • 1
  • 1
Jernej K
  • 1,602
  • 2
  • 25
  • 38

2 Answers2

4

I don't think there is an upper limit to storing strings in xml, so it would be most convenient to store the strings there.

If really want to go for different mechanism, you can either go for database, or file. But, reading from database and file would take time as compared to reading from the xml file, and it would require more code to achieve the same thing.

Edit :

I just noticed that the string you are referring to are the rules of the game. So I'd strongly recommend to go with strings.xml because android uses that XML to enable translating your app into different languages

From the official guidelines on localization:

Move all strings into strings.xml. As you build your apps, remember not to hard code any string. Instead declare all of your strings as resources in a default strings.xml file which makes it easy to update and localize. Strings in strings.xml file can be extracted, translated and integrated back into your app (with appropriate qualifiers) without any changes to compiled code.

Yash
  • 5,225
  • 4
  • 32
  • 65
-1

Instead of saving all the long strings in strings.xml, you can use a text file and read that file when you want to show the rules text.

Your layout file will be something like this:

<ScrollView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_weight="1.0">
<TextView 
    android:id="@+id/subtitletv"
    android:textSize="18dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
/>

And code will be something like this:

subtitletv = (TextView)findViewById("R.id.helptext");
try {
        FileReader fr=new FileReader(selectedfile);
        BufferedReader br=new BufferedReader(fr);
        String line = null;
        try {
            while((line = br.readLine()) != null)
                 {
                      subtitletv.append(line);
                      subtitletv.append("/n");
                 }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

This will be easy to maintain and access. Make necessary modifications if required..

DsD
  • 1,081
  • 8
  • 11
  • But is this a best practice? Would you always recommend using a file instead of the resource file? – Jernej K Apr 14 '16 at 03:02
  • According to the comment by "Yashasvi", it will be more time consuming..Was not aware of it..thanks anyway..!! – DsD Apr 14 '16 at 03:05
  • Note that the time consumption is absolutely trivial. – Fattie Apr 01 '21 at 11:47
  • I realise that this is a bit old but I think the translation part is i important to note. Strings.xml is geared for translations, using a file is not. There is no benefit to using an individual file which in fact adds an overhead, it just ‘feels’ like the right thing to do as you’ve got a lot of text. In the end, strings.xml is just a file same as writing to an individual file, but strings.xml can be read and translated much easier. :) – greysqrl Jul 11 '23 at 05:36