Mohamed Eltarkawe Mohamed Eltarkawe - 1 year ago 117
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.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};

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

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);
//String activity1 = getResources().getString(R.string.activity1);
setTitle("activity1 title"); //


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

Answer Source

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

<?xml version="1.0" encoding="utf-8"?>


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

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

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

protected void onCreate(Bundle savedInstanceState) {
    String[] listM = getResources().getStringArray(R.array.listMenu1);
    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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download