I have created an application in English language. Now I am changing that in arabic language with the help of of res/values-cn And its working fine. Now I want to implement the menu in my application. I have followed this link to make custom menu. http://www.codeproject.com/Articles/173121/Android-Menus-My-Way# .
Its working fine. And also the caption of the MenuItem also changes to Arabic language in emulator. But can't find the solution in the mobile.
My question is, How to change the Caption of the MenuItem in android device.
This is my code which i edited in Demo.java
package com.menu;
import java.util.ArrayList;
import java.util.Locale;
import com.menu.CustomMenu.OnMenuItemSelectedListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;
/**
* Demo class
*
* This is class demonstrates how to use the custom menu helper classes.
*
* @category Demos
* @author William J Francis (w.j.francis@tx.rr.com)
* @copyright Enjoy!
* @version 1.0
*/
public class Demo extends Activity implements OnMenuItemSelectedListener {
/**
* Some global variables.
*/
Typeface typeface;
private CustomMenu mMenu;
public static final int MENU_ITEM_1 = 1;
public static final int MENU_ITEM_2 = 2;
public static final int MENU_ITEM_3 = 3;
public static final int MENU_ITEM_4 = 4;
/**
* Always called when an Android activity launches.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
//create the view
super.onCreate(savedInstanceState);
Locale locale = new Locale("cn");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
typeface = Typeface.createFromAsset(getAssets(),"arabtype.ttf");
setContentView(R.layout.main);
//initialize the menu
mMenu = new CustomMenu(this, this, getLayoutInflater());
mMenu.setHideOnSelect(true);
mMenu.setItemsPerLineInPortraitOrientation(4);
mMenu.setItemsPerLineInLandscapeOrientation(8);
//load the menu items
loadMenuItems();
}
/**
* Snarf the menu key.
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
doMenu();
return true; //always eat it!
}
return super.onKeyDown(keyCode, event);
}
/**
* Load up our menu.
*/
private void loadMenuItems() {
//This is kind of a tedious way to load up the menu items.
//Am sure there is room for improvement.
ArrayList<CustomMenuItem> menuItems = new ArrayList<CustomMenuItem>();
CustomMenuItem cmi = new CustomMenuItem();
//String str = "hello";
cmi.setTypeface(typeface);
cmi.setCaption(getString(R.string.first));
cmi.setImageResourceId(R.drawable.icon1);
cmi.setId(MENU_ITEM_1);
menuItems.add(cmi);
cmi = new CustomMenuItem();
cmi.setCaption(getString(R.string.second));
cmi.setImageResourceId(R.drawable.icon2);
cmi.setId(MENU_ITEM_2);
menuItems.add(cmi);
cmi = new CustomMenuItem();
cmi.setCaption("Third");
cmi.setImageResourceId(R.drawable.icon3);
cmi.setId(MENU_ITEM_3);
menuItems.add(cmi);
cmi = new CustomMenuItem();
cmi.setCaption("Fourth");
cmi.setImageResourceId(R.drawable.icon4);
cmi.setId(MENU_ITEM_4);
menuItems.add(cmi);
if (!mMenu.isShowing())
try {
mMenu.setMenuItems(menuItems);
} catch (Exception e) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Egads!");
alert.setMessage(e.getMessage());
alert.show();
}
}
/**
* Toggle our menu on user pressing the menu key.
*/
private void doMenu() {
if (mMenu.isShowing()) {
mMenu.hide();
} else {
//Note it doesn't matter what widget you send the menu as long as it gets view.
mMenu.show(findViewById(R.id.any_old_widget));
}
}
/**
* For the demo just toast the item selected.
*/
@Override
public void MenuItemSelectedEvent(CustomMenuItem selection) {
Toast t = Toast.makeText(this, "You selected item #"+Integer.toString(selection.getId()), Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
}
And strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Demo!</string>
<string name="app_name">CustomMenu</string>
<string name="first">الأول</string>
<string name="second">ثان</string>
</resources>