Reputation: 31
I have an application I made using android studio. As of the moment, the admob ids are defined in the strings.xml
file. However, I want to make the app get the admob ids from either my server or from firebase as I want to be able to change them remotely should any problems occur. How would I go about doing this?
Upvotes: 1
Views: 3681
Reputation: 599101
There is a new API that allows you to manage Firebase projects and get configuration data from them. This would be the most logical place to look up such configuration data programmatically.
I checked the projects.get
and projects.getAdminConfig
, but haven't been able to find the ID you're looking for yet.
If that ID is indeed missing, it might be worth it to file a feature request.
Upvotes: 0
Reputation: 280
Either you can load Ads From Firebase Using Remote Config
or you Can Use the Firebase database. Refer this Sample Project from my Github Load Ads From Firebse
Upvotes: 2
Reputation: 2143
You can do it programmatically.
Layout
From your layout file, add AdView
:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
Retrieving Ad Unit Id
Setting Ad Unit Id
In Java
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Set ad unit id
In Kotlin
val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // Set ad unit id
Additional
Depending on your persistent storage implementation, here's a one liner way of saving the Ad Unit Id using SharedPreferences
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MY_AD_UNIT_ID", "YOUR_AD_UNIT_VALUE").apply();
Here's how you will retrieve it
PreferenceManager.getDefaultSharedPreferences(context).getString("MY_AD_UNIT_ID", "DEFAULT_STRING_IF_NOTHING_WAS_FOUND");
Where context
is your Context.
Read more:
Upvotes: 1