I'd like to place ads in my MainActivity below FrameLayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="#+id/fragmentContainer"
android:layout_centerHorizontal="true">
</FrameLayout>
<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>
I have added this to my Manifest:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
And my build.gradle also looks fine. Here is my java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(),"ca-app-pub-xxxxxxxx~xxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // An example device ID
.build();
mAdView.loadAd(request);
But when I run the app I can't see the ads and in the LogCat I got this errors:
W/Ads: Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?]
W/Ads: There was a problem getting an ad response. ErrorCode: 1
W/Ads: Failed to load ad: 1
I have tried almost everything I found on the web but nothing worked for me. I think maybe the issue is in my activity_main.xml because of the FrameLayout doesn't give enough place to the ads.
Your code seems fine (although you should call MobileAds.initialize() from the onCreate() method of your Application class, not from an Activity).
Looks like the only problem could be the banner_ad_unit_id resource being an invalid ad unit ID (as the error message also suggests).
Related
It is not a duplicate question. I already tried solutions from similiar questions but none of them worked.
My Google Ad Banner code is below:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/konstraitLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="0dp"
app:layoutDescription="#xml/activity_main_scene"
tools:context=".MainActivity">
...
<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"
ads:adSize="Banner"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
The code in MainActivity:
adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
So I added my banner in XML. I found it in java and then called it, but I guess I am missing something since I've got the following error:
The ad size and ad unit ID must be set before loadAd is called
Thanks for answer.
The adSize tag should have its value passed in capital letters. Replace this:
ads:adSize="Banner"
with this:
ads:adSize="BANNER"
Im trying to add a banner to my Android application, but can't get it to work. The banner is visible in the preview but not when running on my Genymotion device. This is my layout-file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.my.package.MapsActivity"
tools:layout="#android:layout/list_content"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="450dip"
tools:context="com.example.my.package.MapsActivity"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
</LinearLayout>
And in the Activity-file I have this code:
MobileAds.initialize(mContext, "ca-app-pub-xxxxxxxxxxxxxxxxx~xxxxxxxxxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It does not work with test ads either. What can be wrong?
Hank
One thing I would like to say that whenever you create a banner or interstitial than you have to wait for few hours for ads to display on your mobile screen.
Maybe your gmail account used in the Genymotion is the same with admob account. Try change to another gmail in your Genymotion.
I'm trying to add a smaller banner image to the bottom of one of the pages in my app. I'm running into a sort of loopy problem:
The code in the XML file is:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/com.sidekickz.myadventure.activity_hero_picture.xml"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
<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_alignParentBottom="true"
android:layout_centerHorizontal="true">
</com.google.android.gms.ads.AdView>
I've tried all variations of xmlns:app/android/ads but I either run into the issue of all the "android" tags not being supported, or something else.
MobileAds.initialize(getApplicationContext(), "appnumber");
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("appnumberthing");
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
But at BEST my app shows an image saying that the ad size isn't set. If I follow the suggestions online to alter it, doing so results in Adsize and AdUnitId need to be set before loading.
Any help would be awesome and you would deserve a hug.
Thanks
I am implementing banner ads, but getting the error "Required XML attribute "adsize" was missing". I was using the developer.google.com link to add admob
I also visited other stackoverflow questions like as Required XML attribute 'adsize' was missing (google play service)
and I have already implemented everything but the error remains the same. Here is the code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.abhishek.canddatastructureprograms.MainActivity">
<FrameLayout
android:id="#+id/sample_content_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
MainActivity.java
added the following code in onCreate() method.
AdView mAdview= (AdView) findViewById(R.id.adView);
//AdRequest mAdRequest= new AdRequest().Builder.build();
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("9F099BD5E5700664591DEC5FDC92A40E") // An example device ID
.build();
mAdview.loadAd(request);
Please let me know if I need to add any more details or code.
Code seems to be right. Try to remove all left and right paddings or margins, maybe you get this error because you have too small screen to show add.
I have added all required code but admob ad is not visible neither on emulator nor on my device. I have created Admob account today itself, is it the reason why i am not able to see add?
The ad part is completely blank while running the app.
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("MyDeviceId_1").addTestDevice("MyDeviceId") // An example device ID
.build();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<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>
remove these lines from your root layout
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
if your AdView will not get full width of your screen, it will not be visible
Also you need to load ad in your AdView
adView.loadAd(request);
check this
For load & show the Admob banner add need to more space .Don't get more space to show on Admob banner add for declaration your root layout(RelativeLayout).Please remove those two line..
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
Must be you need to add this line on your java code.
adView.loadAd(request);
I don't able to show this line on your code.Are you add this line.Please check this.
Be sure to add Internet permission to your manifest. Check RRR's answer too.