Home > database >  Is it possible to create string.xml file programmatically in android?
Is it possible to create string.xml file programmatically in android?

Time:10-18

How to create string.xml file programmatically in resource file in android studio project

for example

"languageKeys": [
        {
            "key": "welcome_screen_title",
            "value": "ZervBee"
        },
        {
            "key": "skip_button",
            "value": "தவிர்"
        },
        {
            "key": "walkthrough_title_1",
            "value": "மேசையிலிருந்து ஆர்டர்"
        },
        {
            "key": "walkthrough_title_2",
            "value": "ஆன்லைனில் பணம் செலுத்துங்கள்"
        },
        {
            "key": "walkthrough_title_3",
            "value": "வரிசைகளை தவிர்க்கவும்"
        },
        {
            "key": "walkthrough_desc_1",
            "value": "இப்போது நீங்கள் உங்கள் மேஜையில் இருந்து உணவை ஆர்டர் செய்யலாம்"
        },
        {
            "key": "walkthrough_desc_2",
            "value": "மாற்றத்தைப் பற்றி ஒருபோதும் கவலைப்பட வேண்டாம், ஆர்டர் செய்யும் போது சரியான தொகையை செலுத்துங்கள்"
        },
        {
            "key": "walkthrough_desc_3",
            "value": "வரிசையில் நிற்பதைத் தவிர்க்கவும், உங்கள் ஆர்டர் தயாராக இருக்கும்போது நாங்கள் அறிவிப்போம்"
        },
        {
            "key": "choose_langauge_title",
            "value": "மொழியை தேர்வு செய்யவும்"
        },
        {
            "key": "my_cart",
            "value": "என் வண்டி"
        }
    ] 

This was the response from API which is in JSON Format. I convert the response into xml format. How to save that xml file as string file.xml in resource. Is their is any way to convert it.

Thanks in advance

CodePudding user response:

A string.xml file is a resource file. Like any other resource file these files cannot be modified at runtime. To be more precise, any file in the res folder cannot be modified at runtime, eg. string.xml, drawables, values.xml etc.

Ideally, you can parse and store your response in a SharedPrefernce or perhaps set up a local db using Rooom.

CodePudding user response:

Resource files are used at build time (one-way / read-only usage). You misunderstand their purpose.

You may just load (deserialize) the JSON response into Java object using GSON, etc.

Prepare LanguageKey class for deserializing:

public class LanguageKey {
    public final String key;
    public final String value;

    public LanguageKey(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

A sample MainActivity:

public class MainActivity extends AppCompatActivity {
    private static final String JSON = "["  
            "{"  
            "\"key\": \"welcome_screen_title\","  
            "\"value\": \"ZervBee\""  
            "},"  
            "{"  
            "\"key\": \"skip_button\","  
            "\"value\": \"தவிர்\""  
            "}"  
            "]";

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

        // deserialize using GSON
        List<LanguageKey> languageKeys = new Gson().fromJson(
            JSON, new TypeToken<List<LanguageKey>>(){}.getType()
        );

        // Now, you can use languageKeys object directly

        for (LanguageKey languageKey: languageKeys) {
            Log.d("LanguageKey", String.format(
                "key: %s; value: %s",
                languageKey.key, languageKey.value
            ));
        }
    }
}

Note: your JSON is invalid. You may need to delete "languageKeys": before processing.

  • Related