Skip to content Skip to sidebar Skip to footer

Android Pull Language Xml File From SD Card, How?

Is here any way in android to use the strings.xml file from the SD_CARD? So basically I have a lot of string file on the SD, strings_en.xml, strings_de.xml, strings_fr.xml etc. H

Solution 1:

You can try making a LanguageClass in which you do parsing and loading of strings in hashmap. In the same class create one more function to which you can pass "key" i.e. the string name and get the value.

public class LanguageLoader(){
    HashMap<String,String> strLangMap = new HashMap<String,String>;

    public void LoadLanguageData (String pathtolanguagepathxml){
         //do you xml parsing here and load values in the strLangMap
    }

    public String getString(String key){
        //get value of the corresponding key from strLangMap
    }
}

You need to call this LanguageLoader class one from application load and another from where user changes the application language.

And in each activity class set labels using this LanguageLoader class.

e.g. for your controls do all initialization in oncreate and do label setup in onResume.

LanguageLoader ll = new LanguageLoader();

TextView tv = (TextView)findViewById(R.id.tv);
Button btn = (Button)findViewById(R.id.btn);

tv.setText(ll.getString(tvlabel));
btn.setText(ll.getString(btnlabel));

Solution 2:

Bsically I idea is to parse String from SDcard --> Keep a file observer on assert so that if files change i get notification --> and refresh view

Below are Three Steps :

   1. Parse String.xml :

    class Product {

    public String name;

    }
    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        XmlPullParserFactory pullParserFactory;
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream in_s = getApplicationContext().getAssets().open(
                    "temp.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_s, null);

            parseXML(parser);

        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void parseXML(XmlPullParser parser) throws XmlPullParserException,
            IOException {
        ArrayList<Product> products = null;
        int eventType = parser.getEventType();
        Product currentProduct = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name = null;
            switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                products = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();

                if (name.equals("string")) {
                    String xyz = parser.getAttributeValue(null, "name");
                    System.out.println("1234567890***" + xyz);
                    currentProduct = new Product();
                    currentProduct.name = parser.nextText();
                    System.out.println("1234567890" + currentProduct.name);
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("resources")
                        && currentProduct != null) {
                    products.add(currentProduct);

                }
            }
            eventType = parser.next();
        }

        printProducts(products);
    }

    private void printProducts(ArrayList<Product> products) {
        String content = "";
        Iterator<Product> it = products.iterator();
        while (it.hasNext()) {
            Product currProduct = it.next();
            content = content + "nnnProduct :" + currProduct.name + "n";

        }

        TextView display = (TextView) findViewById(R.id.info);
        display.setText(content);
    }

}

2. Keep File Observer :

FileObserver

3.  Refresh Views :

private void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

Post a Comment for "Android Pull Language Xml File From SD Card, How?"