Problems adding new AdMob to android app - android

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 :)

Related

Need to put AdMob ad id in java code? (Android Studio)

For example, on one of my app screens where you have a Banner ad, you have the following code in the xml layout:
<com.google.android.gms.ads.AdView
android:id="#+id/adViewPrincipal"
android:layout_width="0dp"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/adId" />
And also in java code has the following code:
MobileAds.initialize(this, String.valueOf(R.string.adId));
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It seems that everything is working with this test id, but when I take the ID parameter in this java code, leaving:
MobileAds.initialize(this);
or
MobileAds.initialize(this, "");
The ad keeps running without any errors in the app, so do I just need to set the ad id in XML? Is that in all the tutorials I've seen people putting the ID also in java code. If I leave the parameter with nothing, nothing goes wrong, it's like this parameter is useless, and when I get the ID from the XML code, that crashes app. Can I put the ad id only in XML code then?
You are passing the second argument of initialize wrong. It is supposed to be app-id of admob on ad-unit id. The ad id is used normally only in xml views, where app_id is used in code to initialize ads for the first time. Read More here
MobileAds.initialize(this, getResources().getString(R.string.app_id));
Can you try everything programmatically:-
LinearLayout adContainer = <container>;
AdView adView = new AdView(activity)
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(<your-publisher-id>); // you can use id from constants
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);

Completely remove AdView

I have an AdView defined in my Layout's XML and I want to make it disappear if the user bought the ad-free version of the app.
What I'm doing is
if(userhaspurchased){
if (mAdView != null) {
mAdView.setVisibility(View.GONE);
mAdView.pause();
ViewGroup vg = (ViewGroup) mAdView.getParent();
mAdView.destroy();
vg.removeView(mAdView);
mAdView = null;
}
Log.d("Purchased","true");
} else { //Load ad }
And the ad is not visible, but in logs I'm still getting AdView messages:
02-25 11:14:58.688: I/Ads(9279): Ad is not visible. Not refreshing ad.
02-25 11:14:58.688: I/Ads(9279): Scheduling ad refresh 60000 milliseconds from now.
And they are repeated every minute. Is there any way to disable completely the AdView?
Update: #Doomsknight answer is fine in my case, where I just have 2 layouts with ads. Otherwise would be a best practice to check the purchase and eventually insert the AdView programmatically.
Your xml is defining the ad view. And the connection automatically. So while you hide it, it has already been initialised.
One solution would be to have an xml that excludes the adview completely, for the paid version.
and set it at the top
if(userhaspurchased)
setContentView(R.layout.paid_version_xml);
else
setContentView(R.layout.free_version_xml);
This however depends on how many xmls you have, and adviews. As maintaining a variation for each page with adview on it may be an issue.
Don't add the adview view in the xml file. Instead, add a placeholder in the xml layout file.
<LinearLayout
android:id="#+id/admob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
</LinearLayout>
In your code:
if(!userhasPurchased)
{
AdView adView = new AdView(this, AdSize.BANNER, adunit_id);
LinearLayout layout = (LinearLayout) findViewById(R.id.admob);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}

Android: how to display admob ads on GLSurfaceView

I am currently able to get the admob ads on ui view (in separate test project ), but i want to display this ad in GLSurfaceView .
I tried to load the ad in onCreate( ) method of activity and in present method of my screen (where all rendering is done) i called
MyGameActivity.mAdView.bringToFront(); //thought it will bring the ad in front of all the game objects.
now on running the project i can see the message in logcat window Recieved ad url "big url"
but i cant see the ad on screen. In my game there is only one activity and many game screens. please help me figure out how to display ads on my game screen.
You should modify your layout to be something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:tag="trueLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</RelativeLayout>
</LinearLayout>
This is my code, which is self explanatory.:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layoutMain = (LinearLayout) findViewById(R.id.layoutMain);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
adView = new AdView(this, AdSize.BANNER, "YourPersonalID#");
layoutMain.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout1.setOnTouchListener(this);
mTestHarness = new GLSurfaceView(this);
mTestHarness.setEGLConfigChooser(false);
mTestHarness.setRenderer(this);
mTestHarness.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
layout1.addView(mTestHarness);
}
After you get this right you will get the equivalent to the BannerEssentials tutorial app from the google play tutorials, but using GLSurfaceView instead.

Admob ads not showing up

I am trying to implement Admob in Android and I am seeing requests coming into AdMob. However, I am not seeing the Admob ads being displayed on the Android screen in the emulator and my Android test phones as well.
As stated before, I can see the requests coming into my AdMob account. However, the content is not being shown. Is there something that needs to be enabled in my account, the main.xml, AndroidManifest.xml, or in the loading of the application?
My application configuration and code are below. Please advise on what is needed. Thanks!
AndroidManifest:
<meta-data
android:value="My Publisher ID"
android:name="ADMOB_PUBLISHER_ID" />
<activity android:name="com.admob.android.ads.AdMobActivity"/>
<receiver
android:name="com.admob.android.ads.analytics.InstallReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"/>
</intent-filter>
</receiver>
<meta-data
android:value="true"
android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"/>
main:
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="My Publisher ID"
ads:loadAdOnCreate="true"/>
On Create Code:
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
adView.loadAd(re);
Any help is appreciated!!
Make sure that the layout the AdView is embedded in does not impose any padding on the AdView. The AdView's size has to be exactly 320x50 dip. It does not show up if it does not get enough room.
Also check the log output of your device. In Eclipse switch to the DDMS perspective, select your device and watch the LogCat output.
I had a similar issue. If you have any padding on your parent layout then you may not have enough width for the ads. If you have it in a portrait view try switching to a landscape view to see if it shows. If it does than you most likely have a width issue somewhere in your layout.
It usually takes some time for ads to start appearing via admob. This happens in the case when you have just registered on admob and started up with your first application. If the suggestions given above are taken care of and if your admob page shows a "green" status, I wont worry about it too much. Once your fill rate increases, you will start to see more and more ads.
Go to app settings on admob.com and make sure use location
data for ads is turned off if your app is not a location based app.
Use location data for ads is used to filter ads based on location and
works only on app with location permission granted. If app does not use
location permission, ads will not show.
Make sure your have filled payment details
Be sure that your Admob layout is displaying in xml view. Put your admob view inside RelativeLayout and try to use android:alignparentBottom:true
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Do our code that you want to show in xml -->
<!--Put adview in bottom of screen -->
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
ads:adUnitId="#string/addmob_id"
ads:loadAdOnCreate="true"
ads:testDevices="HT9CWP803129" />
</RelativeLayout>
</LinearLayout>
In your java code put these lines in onCreate method
// Load addvertisment
AdView adView = (AdView) findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder().addTestDevice("FF9CD441FA62FD456D7D571B91DD11DD").build();
adView.loadAd(adRequest);
This worked in my code, Hope it will help you too.
Be carefull with the id, there are 2 codes: the editor number (like this: pub-xxxxxxxxxxxxxxxx) and the other is the banner id (like this: ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx)
You need to use the last one, if you use the first doesnt work:)
You are using
ads:adUnitId="My Publisher ID"
replace it with
ads:adUnitId="Here AdUnitId for this app"
Note: You have to create app on Admob and you will get AdUnitId for that created app. You don't have to use Publisher ID.
I see there are two problems in your code based on what you posted:-
The <metadata> section should contain the ADMOB_APP_ID instead of your
publisher ID. This should be declared under <application> tag in
ApplicationManifest.xml.
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[ADMOB_APP_ID]"/>
you can find ADMOB_APP_ID by on ADMOB dashboard, click on the application
and check "App Settings". You can see the APP_ID which starts
typically with ca-app-pub-0123456789012345.
The second problem is, where you have declared AdView in your layout.
Remember you have to provide Ad unit not your publisherID, which
you can create in ADMOB dashboard by clicking on Ad Unit tab
under your application. Put the correct "ad unit" against your
AdView as below.
ads:adUnitId="ca-app-pub-3940256099942544/6300978111" <!-- remember this is adUnit not App ID and this value above is for test banner Ad. -->
Once you have fixed above problems, do the following:-
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); in onCreate of your first Activity. This needs to be done only once and thus the right place is either your first activity or at application's onCreate callback.
Find the AdView in the activity onCreate where you have included AdView in the layout.
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Test Ads work by providing test adunit published by google.
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
Additionally, if you want to handle Ad events, do as follows:-
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
Some of these possible solutions may sound obvious, but make sure you have these completed:
-replace "My Publisher ID" in android:value="My Publisher ID" with your actual publisher ID.
-make sure to include the internet permission in your manifest file:
<uses-permission android:name="android.permission.INTERNET" />
If you have completed those, you can also try placing the following code in the "On create" section instead of your current:
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
adView.loadAd(adRequest);
adView.loadAd(new AdRequest());
or
AdManager.setTestDevices( new String[] {AdManager.TEST_EMULATOR});
AdView adView = (AdView)findViewById(R.id.adView);
adView.requestFreshAd();
Before publishing, don't forget be sure to get rid of the setTestDevice though!
make sure, your admob publisher ID is correct. It had happend with me once and I could not figure out the error for about 15 days.
In Admob every app that you submit gets a different publisher ID. It is unique per Application and not per User.
It might be an issue with the space, you need to ensure that the space is available for the ad to be displayed. If you have a padding on the parent layout it might reduce the space available. According to the google Admob docs no ad will be displayed if the space isn't there for it to be displayed.
"The SDK will request whatever size the requesting AdView was instantiated with. If there isn't enough space on the device's screen to display the ad, nothing will be shown."
So my suggestion is to ensure no padding is on the parent layout and actually assign a height and width to the banner ad like this
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_alignParentBottom="true"
android:layout_width="#dimen/banner_width"
android:layout_height="#dimen/banner_height"
ads:adSize="BANNER"
ads:adUnitId="My Publisher ID"
ads:loadAdOnCreate="true"/>
And store the appropriate dimensions in your dimens.xml in the values folder.
This is because admin inventory requires some requests to be generated to check that the app can make some profit!
Please let setup the ads properly and publish the app, after about 1k requests are made, the ads will automatically show up!
I too was suffering from this problem and got my answer when I published my own app!
If admob "App Id" and "Unit Id" is correct then upload on google play store it will show the add.
I was facing same type of issue but after upload on google play it resolve automatic.
My ads were shown by just having few request here are the following steps I made
Get More Requests You are saying that 400 imp but no ads just make another ad unit in same app it will start showing I also did this this method and ad started In my previous ad unit it have many request but no imp then I made another unit it started showing ads
Have patient in my account it take 4 days to show ads and my account was approved by Google in 1 hr it take a lot of time to build inventory in the server so be patient you will have your ads
Complaint Google at admob complatint they will check your app and chances of app is approved is 50-50%

weird admob problem

I did some updates to my application today but now admob dont work right and it gives me an error in the ad saying adview missing xml attribute "adsize". now the changes i made have nothing to do with the ads and they have been the same since I implemented them and worked
this is my xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/com.notify.me.lite"
android:layout_width="fill_parent"
android:id="#+id/mainLayout1"
android:layout_height="fill_parent" android:orientation="vertical">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="id"
ads:adSize="BANNER"/>
<WebView android:layout_width="fill_parent" android:id="#+id/webView1" android:layout_height="fill_parent"></WebView>
</LinearLayout>
and where its implemented
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_webview);
WebView browser = (WebView) findViewById(R.id.webView1);
browser.loadUrl("file:///android_asset/about.html");
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
as you can see adsize is there in the xml file so there should be no reason why I should be getting that error. Ideas as to why this is happening all of the sudden?
Google updated the AdMob library but did not yet document the required changes, as per this forum thread
The substance of the change is to update the ads namespace:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
No idea why its suddenly stoped working but whenever i use Admob i always put it in a seperate layout, with xml like:
<RelativeLayout android:id="#+id/rLayAdMob"
android:layout_width="match_parent" android:layout_height="50dip">
</RelativeLayout>
Then in the onCreate function i setup the adView like:
adView = new AdView(Menu.this, AdSize.BANNER, MY_AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rLayAdMob);
layout.addView(adView);
I've never had any issues doing it this way, and would only require a small change to your code.
Hope this helps,
Kenny

Categories

Resources