Mohamed Eltarkawe Mohamed Eltarkawe - 1 month ago 19
Java Question

Translate Menu in Java Class

I am translating my app to Spanish and Arabic. I converted all the English Strings in the values file to the required language and that worked out very well.

My problem is that the activity names are still in the default language! As you see below, I used
String activity1 = getResources().getString(R.string.activity1);
I defined the 3 activity names in the values file (res/values-es/string.xml). For example my first activity has the id (activity1). For some reason this still does not work! Any suggestion or/and other reading materials!

package com.reportodor.mohamed;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{

String displayNames[] = { "Report Odor","Info","Contact Us",};
Class activities[] = {Report.class, Study.class, Contact.class};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, displayNames));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = displayNames[position];
Class ourClass = activities[position];
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
//String activity1 = getResources().getString(R.string.activity1);
setTitle(getResources().getText(R.string.activity1));
setTitle("activity1 title"); //


}
}


I read the following and they were quite helpful:
Accessing Resources
Localizing with Resources

Answer

It finally worked out. In res/values/arrays: I created

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array
        name="listMenu1">
        <item>@string/activity1</item>
        <item>@string/activity2</item>
        <item>@string/activity3</item>
    </string-array>

</resources>

And in res/values-es/strings called the spanish translation...

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="activity1"> Reportar Un Olor </string>
     <string name="activity2"> Informaci├│n </string>
     <string name="activity3"> Contáctenos </string>                             
</resources> 

Then the array was retrieved in the OnCreate method as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    String[] listM = getResources().getStringArray(R.array.listMenu1);
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, listM));
}

I greatly appreciate the guidance and directions from SuigetsuSake and sidgate.