Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application title for homescreen shortcut does not change on phone language change

I have an application for which I have also created a home screen shortcut from code. My app supports multiple languages. When I change the language on phone, the app name changes properly in launcher screen. But the name does not change for the home screen shortcut. On other hand, if I first change the language and then install the app, the home screen shortcut contains proper name.

Can someone help me in fixing this.

My home screen shortcut code:

    HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
    HomeScreenShortCut.putExtra("duplicate", false);
    //shortcutIntent is added with addIntent
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    getApplicationContext().sendBroadcast(addIntent);
like image 747
Sushil Avatar asked Nov 01 '22 04:11

Sushil


2 Answers

Shortcut doesn't update itself after language changing. You should handle language changing (for example save langue in preferences or local DB, and compare it with system language each time when your activity starts) and then reinstall shortcut.

like image 110
Grimmy Avatar answered Nov 09 '22 16:11

Grimmy


As Grimmy mentioned that Shortcut doesn't update itlesf on Language change. So, as per his advice, I have written a function which solved my purpose. I would like to share with you all:

In this function, when the user launches the app for 1st time, it creates the shortcut. When the user changes phone language and launches app again, it first deletes the old short based on old name and then recreates the shortcut with the new name (note : app name also changes as language changes).

public void createOrUpdateShortcut() {

    appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);

    String currentLanguage = Locale.getDefault().getDisplayLanguage();
    String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());

    if (!previousSetLanguage.equals(currentLanguage)) {
        shortcutReinstall = true;
    }

     if(!isAppInstalled || shortcutReinstall){

        Intent HomeScreenShortCut= new Intent(getApplicationContext(),
                BrowserLauncherActivity.class);

        HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
        HomeScreenShortCut.putExtra("duplicate", false);

        if(shortcutReinstall) {
            Intent removeIntent = new Intent();
            removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
            String prevAppName = appPreferences.getString("appName", getString(R.string.app_name));
            removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, prevAppName);
            removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
            getApplicationContext().sendBroadcast(removeIntent);
        }

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
        getApplicationContext().sendBroadcast(addIntent);


        //Make preference true
        SharedPreferences.Editor editor = appPreferences.edit();
        editor.putBoolean("isAppInstalled", true);
        editor.putString("phoneLanguage", currentLanguage);
        editor.putString("appName", getString(R.string.app_name));
        editor.commit();
    }

Hope it helps someone. Would again like to thank grimmy for his advise.

like image 39
Sushil Avatar answered Nov 09 '22 18:11

Sushil