I've seen similar questions but they couldnt solve my problem. I've installed Google Play SDKs, imported as a library to my app. Following Admob instructions, I've written that code below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.oguz.nfcdatareaderwriter.MainPage$PlaceholderFragment" >
<Button
android:id="#+id/writeNFC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/openingMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Write Custom Data to a Card" />
<TextView
android:id="#+id/openingMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="174dp"
android:text=".."
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Main Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main_page);
// Create an ad.
adView = new AdView(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.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("4df1dccd39006f0f")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
If you are getting NoClassDefFoundError then you have not properly included the GooglePlayServices library, or you have managed to exclude the classes using Proguard or similar. Try redoing the steps.
For me it was quite difficult to use AdMob with Eclipse LUNA and the last SDK and ADT.
What I've found it's that you must order your Java Build Path (Right Click then properties) as folow :
Android Dependecies checked, Android xxx Unchecked, Android Private Librairies Checked
And control that in your libs folder the google-play-services.jar is inside !
Related
My application was working with android 4.2 and using Admob x.x.x.jar file few days back.
i just configured android sdk 4.4 and i noticed Google play ads are not longer working, also found it should be upgraded as well so what i did following:
Before My XML Was:
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
ads:adSize="BANNER"
ads:adUnitId="asdadadasddas"
/>
Today I updated it
<com.google.android.gms.ads.AdView android:id="#+id/adView"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="asdadadasddas"
ads:adSize="BANNER"
/>
My Manifest
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
values => Integer.xml
<integer name="google_play_services_version">5089034</integer>
and it give me following error
Note: I am not using any java code to display it is that necessary?
any help would be appreciated.
I appreciate answers of all developers actually i solved this problem by combining the answers of all so i would like to enter steps here that might help others.
XML Layout:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-***"
ads:adSize="BANNER"
/>
Two mistakes in my code were:
1) xmlns:ads value was wrong & this mistake pointed out by donfuxx
2) i was using old AdUnitId with following format: a15220c7a5f3775 but in latest AdMob its format is entirely different as used in above example.
Note:
If you use old "ads:adUnitId" it will show blank ad and this was the case with me.
Javacode
Java code is also required
AdRequest adRequest = new AdRequest.Builder()
.build();
AdView adView = (AdView) view.findViewById(R.id.adView);
adView.loadAd(adRequest);
Then again i found another error:
Class not found exception although i had imported the Google Play project in my application.
Then i found an article in which it was mentioned copy Google Play Jar inside your "libs" folder and it will be automatically included in your android private libraries to resolve this issue.
Final Issue
No need to put value in integer.xml inside your app because when you include Google play services project inside your app, it will automatically pick version value out of it in your manifest file.
So these were the issues i faced during migration of adMob and will help others to implement it quickly.
You can still use ads:adSize xml attribute!
However, your xml namespace setting is wrong in the adview element, because it refers to the legacy admob:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
but since you already use the google play admob, you should use:
xmlns:ads="http://schemas.android.com/apk/res-auto"
that is why the ads: attributes were not recognized.
More infos see the official admob migration guide.
You should no longer be setting your adUnitId/adSize in the layout. Instead do:
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
Layout:
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And finally,
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("XXXXXXXXXXXXXXXXXXXX")
.build();
adView.loadAd(adRequest);
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 working for Admob in Android and I got success in getting test ad. But When I Change My Test UnitId To To Live UnitID something like this "/5479/ctv.abcd.ca/myid" I am getting the warning like "is your unit id correct ?". and also i commented the lines for adding test devices. but still i am not seeing any real adds.
Any Ideas..... Any Better Solution..
LinearLayout lladView = (LinearLayout) inflater.inflate(R.layout.addmob, null);
AdView adView = (AdView) lladView.findViewById(R.id.adView1);
adView.loadAd(new AdRequest());
and my xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="/5479/ctv.abcd.ca/myid"
ads:loadAdOnCreate="true"/>
</LinearLayout>
You seem to be providing the wrong adUnitId. You have to specify your AdMob publisher ID that can be obtained from the AdMob Sites & Apps -> [Your App Name] -> Manage Settings.
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
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);