how to change meta-data value Programmatically in manifest or string resource in string value
-
Possible duplicate of [Change value of R.string programmatically](https://stackoverflow.com/questions/9674925/change-value-of-r-string-programmatically) – TheWanderer Sep 17 '18 at 17:07
-
I want to change one of them meta-data value or string resource – Ahmed Wahdan Sep 17 '18 at 17:09
3 Answers
Using Inject build variables into the manifest you can change metadata value programmatically
In build.gradle
defaultConfig {
manifestPlaceholders = [ senderid:"12345"]
}
buildTypes {
release {
manifestPlaceholders = [ senderid:"56789"]
}
}
In Manifest file
<meta-data
android:name="SENDER_ID"
android:value="${senderid}" />
Referance Link : https://developer.android.com/studio/build/manifest-build-variables.html

- 116
- 1
- 5
You can't modify a resource, when you provide info as a Resource (in the folder res) it can't be modified programmatically, You can't write in that directory. Is the same for Android Manifest, the manifest meta-data can't be changed at runtime.
It's not better to have two strings, like the first value and the second one so you can just use this:
Java:
thing.setText(getResources.getString(R.id.variablename2))
Kotlin:
thing.text = resources.getString(R.id.variablename2)

- 488
- 2
- 15
-
the problem is I have I have meta-data value on manifest i want to change it – Ahmed Wahdan Sep 17 '18 at 17:19
-
You cannot change the meta-data at runtime but you can set up multiple flavors and set up your build to build appropriately for each flavor of the application. If you are looking for a more robust solution I would recommend setting up a setting service that would allow you to give settings on a per client/organization basis. – Randy Sep 17 '18 at 17:41
Changing metadata can be done by adding metadata to AndroidManifest.xml file. Later you can access it using Bundle from your activity. Here is the example.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.readmetadata"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainMenu" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="my_api_key" android:value="mykey123" />
</application>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>
Reading this meta-data takes just a few lines of Java
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_api_key");
} catch (NameNotFoundException e) {
Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());
}
Activity extends ContextWrapper which has a getPackageManager() method. That method returns the PackageManager, which is used to fetch the ApplicationInfo, passing the package name and the meta-data flag. The returned ApplicationInfo contains a field, metaData, which is actually a Bundle containing all the meta data. Line 4 fetches a String that is the same as the “android:name” parameter in the XML. Hope it helps!

- 47
- 7
-
4
-
Can i apply / change activity meta-data value programmatically ? – Hardik Paranjape Jan 14 '21 at 21:40