I would like to run an google developers example app which I have got from
here
I have registred at admobs.com from where I have my Ad unit ID which is in form: ca-app-pub-7.../... In main.xml I have then replaced AD_UNIT_ID_GOES_HERE for this new Ad unit ID.
Then I have replaced in main.xml TEST_DEVICE_ID_GOES_HERE for device id I have got from logcat:
09-07 19:49:30.881: I/Ads(5735): To get test ads on this device, call adRequest.addTestDevice("EFEA76C9D061FDD37B8ABF6EB712A991");
Also in BannerSample.java I have add code into onCreate:
package com.google.example.ads.xml;
import com.google.ads.AdRequest;
import android.app.Activity;
import android.os.Bundle;
/**
* A simple {#link Activity} which embeds an AdView in its layout XML.
*/
public class BannerSample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// This example requires no additional code since the optional
// "loadAdOnCreate=true" XML attribute was used. If "loadAdOnCreate" were
// not specified, the ad would have to be loaded by creating an AdRequest
// and using Activity.findViewById() to get the AdView.
//
// The "loadAdOnCreate" XML attribute makes it simpler to get ads since no
// code is required, but it also limits the developer's control over the ad
// request since a generic AdRequest is used.
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
request.addTestDevice("EFEA76C9D061FDD37B8ABF6EB712A991");
}
}
I waited few minutes and App was running but ads banner is not appear.
Logcat:
09-07 20:32:01.037: W/webcore(5945): Can't get the viewWidth after the first layout
09-07 20:33:00.306: I/Ads(5945): AdLoader timed out after 60000ms while getting the URL.
09-07 20:33:00.310: D/webviewglue(5945): nativeDestroy view: 0x1cb198
09-07 20:33:00.322: I/Ads(5945): onFailedToReceiveAd(A network error occurred.)
I have I also tried already add code to manifest.(I have seen it there on stackoverflow but didn't worked).
<meta-data
android:name="ADMOB_PUBLISHER_ID"
android:value="pub-7..."
>
</meta-data>
I am using latest version GoogleAdMobAdsSdkAndroid-6.4.1. Thanks for any suggestions.
EDIT:
I have tried my app on emulator and other phone and there was no problem. Ad banner was showing fine. Phone where banner isn't appearing is Samsung I5801 with android 2.3.7. It is not my phone originally and it is probably rooted phone what could be reason, but I am not sure(I have found it on forum).
Try
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}
in Main Layout add
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
and
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="YOUR_KEY"
ads:refreshInterval="32" />
Hope that helps :)
You can define your admob id in a string, along with all test devices for easy access
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/myAdView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
googleads:adSize="SMART_BANNER"
googleads:adUnitId="#string/admob_id"
googleads:testDevices="#string/ADS_TEST_DEVICES" />
Then in your activity:
package com.google.example.ads.xml;
import com.google.ads.AdRequest;
import android.app.Activity;
import android.os.Bundle;
/**
* A simple {#link Activity} which embeds an AdView in its layout XML.
*/
public class BannerSample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView myAdView = (AdView) this.findViewById(R.id.myAdView);
AdRequest request = new AdRequest();
myAdView.loadAd(adRequest);
}
}
Happy Coding!
You may forget to Add permissions on your Android maifest file. Seems like that
INTERNET and
ACCESS_NETWORK_STATE permissions are required in your Android manifest closer to your manifest tag.
The message: There was a problem getting an ad response. ErrorCode: 1 reffers that the ad unit ID is invalid.
public static final int ERROR_CODE_INVALID_REQUEST
The ad request was invalid; for instance, the ad unit ID was incorrect.
Constant Value: 1 (0x00000001)
The solution is changing the ad unit ID.
Related
I have been trying to implement Native Ads from admob.
I got some sample code from Github. I tried to implement this code, and the demo code works fine. The native ads show up properly.
I then created my own Admob account and obtained an Ad Unit ID. The previous demo code for native ad did not work with this id, and I got an error with error code 0 although my created Ad Unit ID works fine for banner type ads.
Can someone help me out?
guys i have facing same issue and finally i have a solution for it hope this will you all..:)
Here in this image when you are trying to create an AdUnitId make sure that the width and height here will be same width and height in the XML file like this
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="360x320"
ads:adUnitId="#string/NativeAdId">
</com.google.android.gms.ads.NativeExpressAdView>
One more thing you have to keep in mind that the width and height you
are going to make ad will be fit in your xml layout ,So for this just
first try It in your xml and then create an Native add for this.
Just try it and feel free to tell me if you will be facing any problem after doing this. Thank you
Try the following code:
XML file:
<com.google.android.gms.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentBottom="true"
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxx" />
The .java class:
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
Make sure to open the commented lines:
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")
when you are in debug mode.Clicks on real Ads in debug mode may result in blocking of your Admob account.You will get the testDevice id in your logcat,replace that with that of mine.
Define the following in your manifest.xml
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
and also the permission:
<uses-permission android:name="android.permission.INTERNET" />
and you are ready to go.
**Note:**If ads are not showing up then wait atleast for 30 mins.
Recently I stucked with the same question. Then I decided to post my solution for that to admobadapter. Hope it will help you.
The basic usage could look like:
ListView lvMessages;
AdmobAdapterWrapper adapterWrapper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListViewItems();
}
/**
* Inits an adapter with items, wrapping your adapter with a {#link AdmobAdapterWrapper} and setting the listview to this wrapper
* FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
*/
private void initListViewItems() {
lvMessages = (ListView) findViewById(R.id.lvMessages);
//creating your adapter, it could be a custom adapter as well
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
adapterWrapper = new AdmobAdapterWrapper(this);
adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
//here you can use the following string to set your custom layouts for a different types of native ads
//adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
//adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);
//Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
adapterWrapper.setLimitOfAds(3);
//Sets the number of your data items between ad blocks, by default it equals to 10.
//You should set it according to the Admob's policies and rules which says not to
//display more than one ad block at the visible part of the screen,
// so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
adapterWrapper.setNoOfDataBetweenAds(10);
//It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
//Otherwise your Admob account could be banned
//String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
//adapterWrapper.setAdmobReleaseUnitId(admobUnitId);
lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView
//preparing the collection of data
final String sItem = "item #";
ArrayList<String> lst = new ArrayList<String>(100);
for(int i=1;i<=100;i++)
lst.add(sItem.concat(Integer.toString(i)));
//adding a collection of data to your adapter and rising the data set changed event
adapter.addAll(lst);
adapter.notifyDataSetChanged();
}
And the result will look like this
when i first implemented admob from my newly created admob account, it took my account 2 days to finally get the ads, so dont worry, just wait...also if your account is not newly created, then see if you added this permission or not
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
So I have a project (module really since I'm using IntelliJ, but I'll use Eclipse terms) with Activity (Let's call it X) which loads an AdView and it all works just fine. Now I have another Activity (let's call it Y) which opens after certain actions, that Activity is inside a library project. Activity Y opens just fine and works well but the AdView I add to it never has anything in it, it is just just blank. Here are some odd messages I see on the logs:
I/Ads﹕ Starting ad request.
I/webclipboard﹕ clipservice: android.sec.clipboard.ClipboardExManager#4293f940
W/ResourceType﹕ Requesting resource 0x7f0b000e failed because it is complex
E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2087>: Format RGBA_8888.
onSizeChanged - w:640 h:100
W/ResourceType﹕ Requesting resource 0x7f0b000e failed because it is complex
E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
W/ResourceType﹕ Requesting resource 0x7f0b000e failed because it is complex
E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
These two messages happen later, after a bit of time (close to the 60 seconds):
W/Ads﹕ Timed out waiting for WebView to finish loading.
I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
W/Ads﹕ Failed to load ad: 2
And here is the code I use to create the AdView:
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(id);
buttonsAndSeekbarLayout.addView(adView);
final AdRequest.Builder builder = new AdRequest.Builder();
AdRequest adRequest = builder.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
I should also mention that I am using other parts of the Google Play Services on that library project so I think it is configured correctly.
Thanks.
EDIT: I should mention that even if I add the adView on the xml it is still just blank!
EDIT2: Added some extra messages above. Also I noticed this message which after doing a search seems to be AdView related:
D/WebKit﹕ SQLite database failed to load from /FileSyetmQuota.db
EDIT3: I found something odd. I added in app purchase to that screen, when the purchase is done, right before I remove the ad (because of the purchase), it shows up for a split of a second.
E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.. Ignore that message, it appears even when you've running your GP services correctly, that's not the issue.
The code part where you create your AdView and load an ad is perfect, that's neither the issue (I'm running exactly the same code on several projects and it works).
Having in account the above, my bets are just two:
Somehow, the library project is interfering with the loading of the ad. Ad loading has a timeout, if you're doing some hard work, probably there's no time enough to load the ad and it times-out, so probably that's why you're seeing the Timed out waiting for WebView to finish loading. message. In this case, try putting the ad loading within a Thread (or an AsyncTask).
A layout rendering issue. You have to check whether the Ad has enough space to show up, i.e., 640 in width x 100 in height minimally. If you don't have that space, the ad won't show up.
---- EDIT ----
To run the code within a Thread, use this code:
new Thread(
new Runnable() {
#Override
public void run() {
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(id);
// This will ensure it is run within the main UI.
runOnUiThread(new Runnable() {
public void run() {
buttonsAndSeekbarLayout.addView(adView);
}
});
final AdRequest.Builder builder = new AdRequest.Builder();
AdRequest adRequest = builder.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
}
}).start();
hey please try to load ads like bellow example
first update your Google play lib to 4.3
The easiest way to incorporate an ad is to simply define your AdView as you would any other part of your res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"/>
</LinearLayout>
you must replace MY_AD_UNIT_ID with your AdMob ad unit ID.
To fetch ads, look up the AdView as a resource via findViewById, create an AdRequest, and invoke loadAd:
import com.google.android.gms.ads.*;
public class BannerExample extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Figured it out, in my code I call webViewInstance.pauseTimers() and apparently that applies to all web views.
I recently migrated my project from AdMob SDK to Google service Ads, however I got a tiny problem with the Google service Ads.
I have a banner ad view in the activity, and I would dynamically adjust the buttons in the layout depending on whether or not an ad is loaded. It worked fine when I used AdMob SDK, but now with Google service Ads, the banner is always reserved there with blank before the ad is loaded. And if the ad cannot be loaded (say without network), the blank view is there, which is rather ugly! This is also why I would like to adjust the layout dynamically...
Did I missed anything while I changed the code? Thank you for help!
Here is an excerpt of the java code and layout file:
Java:
import com.google.android.gms.ads.*;
...
public class MyActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
AdView adView = (AdView)findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Layout XML:
...
<com.google.android.gms.ads.AdView
android:id="#+id/ad"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="AD_PUB_ID"
ads:adSize="BANNER"/>
</RelativeLayout>
You can add an AdListener, and make the ad visible only when it receives an ad.
The code looks like this:
final AdView ad = new AdView(context);
ad.setAdUnitId(publisherId);
ad.setAdSize(AdSize.SMART_BANNER);
final AdListener listener = new AdListener() {
#Override
public void onAdLoaded() {
ad.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
};
ad.setAdListener(listener);
ad.setVisibility(View.GONE);
adParent.addView(ad);
ad.loadAd(new AdRequest.Builder().build());
Since there is/was an issue, that an AdMob ad generates an ANR, when it is destroyed when still loading. I normally also check on the visibility of the ad, before calling pause() or destroy().
Be careful because this is against Google Ad policy to have the ad banner pops up like that.
I received a mail from Google a month ago and had to change that behavior to avoid my app to be banned from the Play Store.
A lot of developers are receiving the same mail.
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 :)
I am trying to set the AdMob banner ABOVE the Tabs in a TabsActivity:
public class FundsReportTabsActivity extends TabsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(activity, AdSize.BANNER, AD_UNIT_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
// Add the adView to it
getTabHost().addView(adView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("E0154BC3F11C778234254195FA1164F");
adRequest.setTesting(true);
// Initiate a generic request to load it with an ad
adView.loadAd(adRequest);
}
However, the banner does not appear, even though I have this logcat:
09-29 22:23:09.755: INFO/Ads(16158): onReceiveAd()
and don't see any related warning/error.
Does anyone have an idea? Thanks!
Check your logcat for a warning or verbose saying no ads in inventory. Some times when you first sign up or are using admob it takes some time before the ads start appearing. Also make sure you have provided a linear layout for the ad. And if you watch the logcat file during the activity with the ad you will see a content containing items that pertain to the ad. I know i had trouble with admob at first. The same issue you are having. It just took some time.