Ahmed Wahdan
Ahmed Wahdan

Reputation: 352

Change meta-data value android Programmatically

how to change meta-data value Programmatically in manifest or string resource in string value

Upvotes: 8

Views: 7334

Answers (3)

Kirti
Kirti

Reputation: 126

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

Upvotes: 8

Kapocs Zoltan
Kapocs Zoltan

Reputation: 47

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!

Upvotes: -1

Dinorah Tovar
Dinorah Tovar

Reputation: 488

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) 

Upvotes: 0

Related Questions