Right way to replace legacy ad unit IDs with new IDs - android

I updated to a new AdMob and in "Here's what to do next" section of the new Admob, there is a guide: Replace legacy ad unit IDs with new IDs (https://support.google.com/admob/v2/answer/6025739?hl=en)
In upgrade guide it says that you just need to "replace each of the legacy Publisher and Mediation IDs in your app with the corresponding new ad unit IDs".
I have this code
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a..."
ads:adSize="SMART_BANNER"
android:layout_alignParentBottom="true" />
and this
adView = new AdView(this, AdSize.SMART_BANNER, "a...");
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adView);
layout.addView(adView);
adView.loadAd(new AdRequest());
Is it enough just to replace "a..." with new ad unit ID "ca-app-pub-...", because on Banners guide on Google developers website it says that this method is deprecated (https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android).
If I need to change code also, can you give me new code that would do the same as my code?

The "new code" requires you to use the google play services library. Follow the official guide on migrating to the new admob here.
The changes you'll need to make in your Activity class are:
Change the lines:
AdView adView = new AdView(this, AdSize.SMART_BANNER, "xxxxxxxx");
adView.loadAd(new AdRequest());
to:
AdView adView = new AdView(activity);
adView.setAdUnitId("xxxxxxx");
adView.setAdSize(AdSize.SMART_BANNER);
adView.loadAd(new AdRequest.Builder()
.build());
In your layout, Change the ad xml namespace from:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
To :
xmlns:ads="http://schemas.android.com/apk/res-auto"
Then in your manifest's application tag, Change:
<activity android:name="com.google.ads.AdActivity"/>
to:
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
Add:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
Don't forget the permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Related

admob is not displaying any ad on android app

Admob is not working correctly when putting my banner id, but it's working when adding debug banner id for testing, any help ?
XML code
<com.google.android.gms.ads.AdView
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="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
Java code
MobileAds.initialize(this, "ca-app-pub-****************");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
and I have these permission in manifist
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is the banner ad that I have is it right ?
If the debug ads shows up, then your implementation is correct. Sometimes ads don't show up until they get released (not always true). But yeah it does take a while for google to find the right ads for your app. Be patient
The problem was because I wasn't setting my payment address in admob.

Admob ads showing in emulator but not in real device

I have integrated admob in my app but the ads showed up in emulator but when i tried in real device it's not showing up. For integration i have added below code in AndroidManifest.xml
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Google Play service -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<!-- Ad activity -->
<activity
android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
Then in my activity's layout i have added below Linearlayout to load the ad in it :
<LinearLayout
android:id="#+id/main_menu_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:orientation="horizontal" />
Then in activity i have written below java code for integration :
/* The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "My_admob_key";
// Create an ad.
adView = new AdView(MainMenu.this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.main_menu_banner);
layout.addView(adView);
// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().build();
// Load the adView with the ad request.
adView.loadAd(adRequest);
layout.bringToFront();
Any help would be appreciated why ads are not showing up!!!! Also you can point to any good tuto for integrating admob will also help. Thanks in advance
Add the extras as well
AdMobExtras extras = new AdMobExtras(bundle);
AdRequest adRequest = new AdRequest.Builder()
.addNetworkExtras(extras).build();
adView.loadAd(adRequest);
May be you missed adding Test device or added wrong ID, same thing happened with me as till I noticed in the Logcat this alert;
04-22 10:04:04.887 I/Ads: Use
AdRequest.Builder.addTestDevice("6612F2AEECD2A4ADBD699CFC349AB01A") to
get test ads on this device.
Once I added it, it started working fine.
In my case there were an issue with the size of the view.
So I debugged the app in Android Studio and found the right hint in the Android Monitor Tab:
Not enough space to show ad. Needs 320x150 dp, but only has 318x0 dp.
I faced the same issues when I was working on changing the targetSDKversion.
I was using a Galaxy S7 phone to test.
I manged to get it working by allocating more space for the adview.
For ads to show up, tried making the view 3:1 with
android:layout_weight="3" for upper activity (75%)
android:layout_weight="1" for adView activity (25%)
android:layout_height="0dp"

Admob is never shown

I'm trying to display an Admob ad but it's never shown. This is so weird.
This is my implementation:
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
XML:
<RelativeLayout
android:id="#+id/containerGlobal"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C7CFD9"/>
And the code:
ViewGroup holder = (ViewGroup) findViewById(R.id.containerGlobal);
AdView adView = new AdView( this, AdSize.BANNER, "adunit..." );
holder.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("5A33BC58556417FFF74F329CDFA95B7E");
adRequest.addTestDevice("8DAC38F2726F4802D60BF7EBA8A5FC39");
adView.loadAd( adRequest );
Logcat:
Any tips?
Edit
This is my code now:
And logcat output... Looks like everything is working, but nothing is shown...
**Edit 2 **
Several hours later, still have the issue.
When you initialise your AdView, try doing the following after it:
AdView adView = new AdView(this, AdSize.BANNER, "adUnitId");
adView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LayoutParams params = (LayoutParams)adView .getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
adView.setLayoutParams(params);
holder.addView(adView);
To set the height/width of the add, and then it's position at the bottom-centre of the screen.
Ok... Finally solved. it was all related with my layouts. I don't really know what was the issue, just removed some of them, changed params of them, and it's being shown now.
Thank you everyone.
instead of "adunit..." place there your real publisher ID that can be found on admob page

Problems adding new AdMob to android app

I am trying to add the new AdMob ads to my first application, I managed to arrange the code in the correct way but I have to problems:
First problem is my ad ID ca-app-pub-61413779example is getting multiple errors like: ca cannot be resolved to a variable, ba cannot be resolved to a variable, the literal
61413779 of type int is out of range.
The second problem is R.id.mainLayout, which is mainLayout, I don't get it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
// Create the adView
adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
adView.loadAd(new AdRequest());
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
I'm not familiar with your admob adding way, but I got another great way to add admob to your app using xml only.
first in your manifest add these permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
* the second is most important
then add this in your manifest inside the application tag
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
now in every activity you want the ad to appear on you add this in the layout.xml of the activity
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_gravity="center"
android:layout_margin="10dp">
</com.google.ads.AdView>
and inside the top parent layout ex. linearlayout you add these under xmlns:android="xxx
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
now the ads should work perfectly :D
UPDATE: don't forget to download and place the GoogleAdMobAdsSdk-6.4.1.jar under the libs folder.
The AdView constructor expects a String as the third argument, so you're basically missing double quotes:
adView = new AdView(this, AdSize.BANNER, "ca-app-pub-61413779example");
As for your XML, ensure that you have the right ID. If only mainLayout is underlined, it is not the correct ID. If R is underlined, then it means your XML has an error somewhere and R.java isn't being generated.
since you set your content with activity_basic_screen.xml, you have to go to that xml.
you need to show that code for activity_basic_screen.xml and tell where you want to put the ad for specific details
for general, in your activity_basic_screen.xml, you have to have a LinearLayout or other layouts where you want to show/inflate the ad. for the one in your screenshot problem/Linearlayout, you have to have this one:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout"
>
you're getting that error because i think you don't have a linear layout with
android:id="#+id/mainLayout"
you can name it differently
android:id="#+id/givenName"
but you have to changed your reference in java of findViewById to "R.id.givenName"
for me, i usually use Ahmed's suggestion so i won't need to program it in java but i think programming in java is more dynamic.
hope that helps.
PS.
in your screenshot you cover your app name/project but you showed your package name. it's like the same thing because you can find your app in google play by using that.
This maybe a different answer to what you are looking for, but I would highly recommend using SMART_BANNER Over BANNER. look here at The table that list the standard banner sizes:
https://developers.google.com/admob/android/banner
Banner: size 320x50 only
Smart Banner: size change to Screen width x 32|50|90
Here is an example on how to do it:
-Layout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_r"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adexample">
<RelativeLayout
android:id="#+id/adView_test"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
-Activity:
public class MainActivity extends AppCompatActivity {
private AdView adView;
private AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
admobCall();
}
// Function to Set Ads for Main Layout
private void admobCall(){
// Set the RelativeLayout id to the view
View adContainer = findViewById(R.id.adView_test);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
//Real Admob Main Activity Banner
adView.setAdUnitId(getString(R"YOUR_ADMOB_AD_ID"));
// Test google ID for Banner
// adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
((RelativeLayout)adContainer).addView(adView);
// This Request will bulid the ADs as a Test
//adRequest = new AdRequest.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// This Request Will bulid the ADs as Real
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Also be sure to add "google()" to the project-level build.gradle, and
this implementation to dependencies
implementation 'com.google.android.gms:play-services-ads:17.2.1'
Check the Implementation instructions site just to be sure you are not missing something:
https://developers.google.com/admob/android/quick-start
Hope this was helpful :)

Integrating admob into Android app

Been having some trouble trying to integrate the admob sdk into my application to display ads. Im using the AdmobSDK version 4.1.0. I've read loads of posts and there seems to be lacking some decent documentation and many discussions on the topic including http://groups.google.com/group/google-admob-ads-sdk/browse_thread/thread/3b885d3fe5bb21a5?pli=1 So far my layout is..
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="MY_PUB_CODE"
ads:adSize="BANNER"/>
</LinearLayout>
</ScrollView>
my androidmanifest.xml contains...
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"></activity>
<meta-data android:value="MY_PUBLISHER_ID" android:name="ADMOB_PUBLISHER_ID"></meta-data>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
EDIT, UPDATE:
Ok i seem to have gotten admob working on my HTC by adding,
AdView ad = (AdView) findViewById(R.id.ad);
AdRequest r = new AdRequest()
r.addTestDevice("X3XFX518X7DE1FD879XA5XXAX1AX8BXX");
ad.loadAd(r);
however i only recieve a test banner, stating im ready to explore the google app galaxy. When i remove the "addTestDevice" method, the banner/ad does not show up atal and in the log i recieve, "ad not recieved due to lack of inventory" .. Anyone sheed some light on this?
thanks for the help so far!
To your comment, I use this and it works quite well for me. The size is the second param of the AdView constructor.
ad = new AdView(this, AdSize.BANNER, "<ID>");
LinearLayout layout = (LinearLayout) findViewById(R.id.main_admob_layout);
// Add the adView to it
layout.addView(ad);
AdRequest request = new AdRequest();
request.setTesting(TESTING_MODE);
ad.loadAd(request);

Categories

Resources