Refresh Options menu in API > 11 and API < 11

I let the user change the app locale whenever he feels like to do it. Menu strings have to meet the new language within this configuration change, in order to do that, the options menu has to be refreshed.

Keep in mind that there are two different ways to achieve this depending on whether you are using API > 11 or API < 11.

Code is really simple and goes as follows:

	@SuppressLint("NewApi")
	public static void refreshOptionsMenu(Activity activity, Menu menu)
	{
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		if (currentVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
		{
			activity.invalidateOptionsMenu();
		}
		else if (menu != null)
		{
			menu.clear();
			activity.onCreateOptionsMenu(menu);
		}
	}

I added that method as static to a “GlobalContext” class so I can use it along with any of my activities. Enjoy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s