1

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>
dilix
  • 3,761
  • 3
  • 31
  • 55
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
  • do you mean caption of the "first", "second" etc on the second screenshot ? Do you want to see them on aravic language ? Use strings.xml and provide captions on what languages do you want – dilix Aug 09 '12 at 12:38
  • i tried it. its working fine in emulator. but in the device the arabic language is not showing, instead of that small boxes are displayed like unknown language. – Narendra Pal Aug 09 '12 at 12:40
  • Maybe smth wrong with your custom layout - can you show it ? – dilix Aug 09 '12 at 12:42
  • Are other elements of the interface fine with arabic language ? Maybe smth wrong with charset of strings.xml ? – dilix Aug 09 '12 at 12:43

1 Answers1

0

Use cmi.setCaption(getString(R.string.youString));

I've taken sample that you've provided, import it to my eclipse, open from eclipse strings.xml that was in the folder values, make one more field test with "ثان" and when adding item add getString code.

What i've get:

enter image description here

I think there are some troubles with charset when you've created your new string.xml - check if this file get utf8 (because of it it can work in windows (cp1251 and don't work in android (linux == utf8))).

dilix
  • 3,761
  • 3
  • 31
  • 55