Change android:label in activity - android

I'm working in the project that to be a template to many apps.
So far everything is configured via JSON.
But I had a doubt: is It possible to change android:label via JSON?
this should to happen at runtime.
<application
android:allowBackup="true"
android:icon="#drawable/icon"
-> android:label="#string/app_name"

That is not at all possible. It is not possible to modify the value of a string resource from code
But, if you want to change the Title Bar of an activity, you can use
setTitle()
try this
this.setTitle("your title");
This will not change the label of the app

Related

Android: how to update app's label dynamically

I want to change the name of the application which is defined in application tag like:
<application android:icon="#drawable/icon" android:label="#string/app_name">
Here the label is defined in resource xml, but what I want to achieve is that user input app_name of his choice and save it and then this new name should be used on Home or Launcher screen of device.
I know there is a way to do by using ActivityAlias but again the label is pre-defined and it's not a dynamic way to do it.
I read some articles that there are some apps like Magisk which changes it's app name & package name through user input.

Can't change the name of an activity

Tried everything i could find on internet but cant seem to find a way to change the name of an Activity. When i test it it just shows the application name.
AndroidManifest
You should write label tag in activity tag not between activity tag
like this
<activity android:name=".MainActivity"
android:label="label_name(eg>#string/~~">
In your manifest, try android:label attribute:
<activity android:name=".MainActivity"
android:label="your_activity_readable_name">
According to android:label, if the attribute is not set, the label set for the application as a whole is used instead.
Use
setTitle(int titleId)
or
setTitle(CharSequence title)
on your onCreate() in Activity that you want to change the label

What happens to app name if android:label is not specified in android manifest?

I have the following in my android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nis.history_card_game" >
<application
android:allowBackup="true"
android:icon="#mipmap/history"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
<activity
android:name=".Battlefield">
</activity>
<activity
android:name=".ChooseHero">
</activity>
</application>
For some reason my app is called nis.history.card_game So what happens if in application tag android:label is missing?
I think the activity tag with the attribute android:label is not necessary, except you want make the activity the title different with the application label, you can use your own label. the default should be as same as the application label.
Elements of the AndroidManifest.xml file
The elements used in the above xml file are described below.
manifest
manifest is the root element of the AndroidManifest.xml file. It has package attribute that describes the package name of the activity class.
application
application is the subelement of the manifest. It includes the namespace declaration. This element contains several subelements that declares the application component such as activity etc.
The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
android:theme represents a common theme for all the android activities.
activity
activity is the subelement of application and represents an activity that must be defined in the AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
android:label represents a label i.e. displayed on the screen.
android:name represents a name for the activity class. It is required attribute.
intent-filter
intent-filter is the sub-element of activity that describes the type of intent to which activity, service or broadcast receiver can respond to.
action
It adds an action for the intent-filter. The intent-filter must have at least one action element.
category
It adds a category name to an intent-filter.
I dont' know WHY this happens - in case android:label attribute is missing, the system automatically assigns the package name. No idea why this happens - might be just convention from Google.
But the solution is just to add label in tag, not above!

How do I change the android actionbar title and icon

I'm trying to do some things on the ActionBar in Android.
I've already added new items in the right side of the action bar.
How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens
This is very simple to accomplish
If you want to change it in code, call:
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
Or, in the Android manifest XML file:
<activity android:name=".MyActivity"
android:icon="#drawable/my_icon"
android:label="My new title" />
To enable the back button in your app use:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
The code should all be placed in your onCreate so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle.
To make a single icon be usable by all your action bars you can do this in your Android Manifest.
<application
android:logo="#drawable/Image">
...
</application>
You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
In Android 5.0 material design guidelines discourage the use of icon in actionBar
to enable it add the following code
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
credit goes to author of this article
If you want to change the Action bar title just give the following 1 line code in the onCreate() of your Activity
getActionBar().setTitle("Test");
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
You can change the icon in your by adding whatever icon you want to your respective drawable folders, then changing this line in your AndroidManifest.xml file:
android:icon="#drawable/ic_launcher"
to match whatever the name of your icon is in there. Or put your icon as ic_launcher, if they're the same icon. As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file. Then, once again in your AndroidManifest.xml file, change this line:
android:label="#string/app_name"
to whatever the string you have in their. You'll have to do this for the application as a whole, and whichever activities you want, but the lines are the same.
Hope this helps.
For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar
So:
XML:
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
Java:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
For set Title :
getActionBar().setTitle("Title");
For set Icon :
getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
Add the below code inside an onCreate function in your activity.
setTitle("NewName");
I used the following call inside onNavigationItemSelected:
HomeActivity.this.setTitle(item.getTitle());
Go to manifest in which specific activity you want to change Action bar Title name and write
android:label="Title name"
This work for me:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
The action bar title will, by default, use the label of the current activity, but you can also set it programmatically via ActionBar.setTitle().
To implement the "Back" (more precisely, "Up") button functionality you're talking about, read the "Using the App Icon for Navigation" section of the Action Bar developer guide.
Finally, to change the icon, the guide covers that as well. In short, the action bar will display the image supplied in android:icon in your manifest's application or activity element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) named ic_launcher.png, and place it in your drawable-* directories.
I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class. I solved it by using getActivity().getActionBar().setTitle("new title");
You can also do as follow :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
Go to AndroidManifest.xml file.
Find the <application> tag
There you can see a attribute
android:label="#string/app_name"
Now go to res > values > strings.xml
Change the
<string name="app_name">MainActivity</string>
to
<string name="app_name">Your Desired Name</string>
Example
AndroidManifest.xml
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubmitForm">
</activity>
</application>
strings.xml
<resources>
<string name="app_name">Your Desired Name</string>
<string name="action_settings">Settings</string>
</resources>

How to change Android app name?

I want to change android application name programatically. Is there any API to do this? Thanks.
You cant do it pragmatically.its static.Go to menifest file and add android:label="Your application name" in application tag like below
<application
android:icon="#drawable/ic_launcher"
android:label="Your application name">
You can't change the app name programatically. It is in the manifest. Why do you want to do that anyways?
You could try to change the name using Antscripts, but this could just happen during devTime.
The appname itself does stand in the manifest.xml:
<application android:label="My App name">
Mostly its refered to the R.String file.
Otherwise as codeBegin already said, use setTitle("AppName") to change the title in the current activity.
If you really want to change the name of the WHOLE app, this would NOT be possible, just imagine what a chaos will be created if apps are renamed frequently ;)

Categories

Resources