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.
publicclassLanguageLoader(){
HashMap<String,String> strLangMap = newHashMap<String,String>;
publicvoidLoadLanguageData (String pathtolanguagepathxml){
//do you xml parsing here and load values in the strLangMap
}
publicStringgetString(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.
LanguageLoaderll=newLanguageLoader();
TextViewtv= (TextView)findViewById(R.id.tv);
Buttonbtn= (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 :
classProduct {
public String name;
}
publicclassMainActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParserparser= pullParserFactory.newPullParser();
InputStreamin_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();
}
}
privatevoidparseXML(XmlPullParser parser)throws XmlPullParserException,
IOException {
ArrayList<Product> products = null;
inteventType= parser.getEventType();
ProductcurrentProduct=null;
while (eventType != XmlPullParser.END_DOCUMENT) {
Stringname=null;
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
products = newArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("string")) {
Stringxyz= parser.getAttributeValue(null, "name");
System.out.println("1234567890***" + xyz);
currentProduct = newProduct();
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);
}
privatevoidprintProducts(ArrayList<Product> products) {
Stringcontent="";
Iterator<Product> it = products.iterator();
while (it.hasNext()) {
ProductcurrProduct= it.next();
content = content + "nnnProduct :" + currProduct.name + "n";
}
TextViewdisplay= (TextView) findViewById(R.id.info);
display.setText(content);
}
}
2. Keep File Observer :
3. Refresh Views :
privatevoidrestartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Post a Comment for "Android Pull Language Xml File From Sd Card, How?"