Soul
Soul

Reputation: 610

Android : Change App Label programmatically

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?

Upvotes: 30

Views: 45977

Answers (9)

Anandh Krishnan
Anandh Krishnan

Reputation: 6032

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

Upvotes: 2

Vivek Modi
Vivek Modi

Reputation: 117

In Launcher Activity,Before setContentView() write this,

setTitle("Your Title");

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

Upvotes: 5

Roman Nazarevych
Roman Nazarevych

Reputation: 7703

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

Upvotes: 13

JacksOnF1re
JacksOnF1re

Reputation: 3547

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.

Upvotes: 0

Olaia
Olaia

Reputation: 2210

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.

Upvotes: 0

Mister Smith
Mister Smith

Reputation: 28199

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

Upvotes: 16

Luten
Luten

Reputation: 5670

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.

Upvotes: 1

Mark
Mark

Reputation: 1029

in the activity, i tried

this.setTitle("your text");

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

Upvotes: 50

cognitio
cognitio

Reputation: 691

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.

Upvotes: 0

Related Questions