29

How can I change application label to change app name shown from java code in android? I'm refering to:

<application android:icon="@drawable/icon" android:label="@string/app_name">

in the Android Manifest

Is there any way to update values in strings.xml file?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Soul
  • 590
  • 1
  • 7
  • 17

9 Answers9

53

in the activity, i tried

this.setTitle("your text");

and it worked. I hope it's a common solution

Mark
  • 1,035
  • 1
  • 11
  • 24
16

It's not possible by the moment. It is a fixed string in the AndroidManifest.xml file which cannot be changed at runtime.

Michael Alan Huff
  • 3,462
  • 3
  • 28
  • 44
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • 3
    Even tho old and not the question i was looking for, this i hope is never allowed as it would allow programs to pose as others or addons – Angry 84 Feb 10 '15 at 12:13
  • 1
    Actually it *is* possible to change the app title - but not the way the OP was desperately groping at. I like the answer user "Mark" gives below. – Ed Poor Jun 24 '18 at 15:27
  • Old thread but it is possible to change app title based on if the locale of the phone is changed? – pingOfDoom Apr 01 '19 at 14:32
  • @pingOfDoom Yes, just define it as a string resource and provide different values in each localized `strings.xml` file. This probably is already setup for you when you create a new project. – Mister Smith Apr 04 '19 at 07:49
12

Using <activity-alias> you can change App icon and name to few predefined by you.

Create such config in Mannifest.xml

<activity android:name="package.name.MainActivity"
 android:screenOrientation="portrait"
 android:label="@string/app_name"
 android:theme="@style/CustomTheme"
 android:launchMode="singleTask">
  <intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

<activity-alias android:label="@string/app_name_default" 
 android:icon="@drawable/icon_default" 
 android:name=".MainActivity-Default"
 android:enabled="true"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

<activity-alias android:label="@string/app_name_flavor_one" 
 android:icon="@drawable/icon_flavor_one" 
 android:name=".MainActivity-Flavor-One"
 android:enabled="false"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

Now you can switch between those two aliases, therefore we will change app icon or/and name. To switch from Default to Flavor-One use this code.

 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Flavor-One"), 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Default"), 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Keep in mind that you have to track that only one alias will be enabled at a time

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • Its taking time to show the another icon in app menu. Refresh time is too long. is there way for sudden change ?/ – Jayesh M May 31 '18 at 10:41
  • 1
    Looks like an almost exact copy of https://stackoverflow.com/questions/1103027/how-to-change-an-application-icon-programmatically-in-android/15249542#15249542 Maybe refer to the answer and question instead of copying it. – JacksOnF1re Oct 28 '19 at 10:15
5

In Launcher Activity,Before setContentView() write this,

setTitle("Your Title");

I don't know how it's possible, But it's surely works.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Vivek Modi
  • 117
  • 1
  • 4
2

Yes, Its possible, In this question everyone mentioned like

this.setTitle("your text");

this will change only your activity name not app logo here I show you how to change the app logo and appname dynamicly

First add your dynamic app icons in the mipmap folder , after that add the below code in your AndroidManifest.xml file

Add <activity-alias> for your app icon

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!-- Disable the original activity app icon in launcher -->
                <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>


        <activity-alias android:label="Anand"
            android:icon="@mipmap/ic_launcher"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:name=".MainActivityAlias1"
            android:enabled="true"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="Anand 1"
            android:icon="@mipmap/ic_launcher2"
            android:roundIcon="@mipmap/ic_launcher2_round"
            android:name=".MainActivityAlias2"
            android:enabled="false"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

    </application>

After doing these methods on your activity, just do below things on button click

import android.content.ComponentName
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button1.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )
        }

        button2.setOnClickListener{
            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias1"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
            )

            packageManager?.setComponentEnabledSetting(
                    ComponentName(applicationContext.packageName, applicationContext.packageName + ".MainActivityAlias2"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
            )
        }
    }
}

for more details refer this github project

Anand
  • 4,355
  • 2
  • 35
  • 45
1

Application's android:label is a fixed resource referrer.

But the string under this referrer could have multiple values, depending on configuration qualifier names (values-en, -large, -land, etc.), according to Providing Alternative Resources.

Luten
  • 5,420
  • 4
  • 26
  • 24
0

If you are extending the firmware, you can actually accomplish this by changing IconCache.java file and make it show a string with some internal value of the phone.

For example if you want the SIM Toolkit to show the name of the carrier, you can do that this way.

But for regular apps, as it's been said, it's currently not posible.

Olaia
  • 2,172
  • 25
  • 27
0

As Mister Smith said, it is not possible,

but you could use multiple ActivityAlias, which can be enabled/disabled dynamically and point to the same targetActivity. Therefore create your chooser for the app name - let the user select one and enable the ActivityAlias via the packageManager:

ComponentName componentName = new ComponentName(context, context.getPackageName() + "." + aliasName);
        context.getPackageManager().setComponentEnabledSetting(componentName,
                                                               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                                               PackageManager.DONT_KILL_APP);

To hide the old alias, use the same code with the flag : COMPONENT_ENABLED_STATE_DISABLED

You can also add the possibility to directly add a shortcut to the home launcher, after you enabled the alias. There are plenty ways described here on sow.

JacksOnF1re
  • 3,336
  • 24
  • 55
0

To anyone interested: Android How to change the application title But it's not clear if it changes the "application label" (that is, the name of the icon in the application list) or only the window title.

Community
  • 1
  • 1
think01
  • 681
  • 1
  • 10
  • 22