Android Ads with AdMob and Firebase - android

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

Related

Admob banner visible in preview mode, but not when running

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.

AdMob banner issues android

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

Showing error Required XML attribute "adSize" was missing

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.

Add admob in app error

I try add admob Ads in my app. Initial I add code :
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("abc")
.build();
adView.loadAd(adRequest);
but it not run, it show error
11-23 11:16:20.285: E/AndroidRuntime(24361): FATAL EXCEPTION: main
11-23 11:16:20.285: E/AndroidRuntime(24361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.phohaycom/com.example.phohay.activity.ReadingActivity2}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
So I try add code :
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-1845170371498416/12345678");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("D5AF9D9AFD269EF3B817CD9804BF2CE9")
.build();
mAdView.loadAd(adRequest);
It run but when I run it, it not show Ads which only displays text "Either XML attribute "ad Size" or XML attribute "supported Ad Sizes" should be specified, but not both.
My xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
it fails anywhere? Please help me
I think remove either of ads:adSize="BANNER" or mAdView.setAdSize(AdSize.BANNER); will help.
I have done it.
I don't add code in xml. Instead I create Linear Layout and add Ads in code
AdView adView = new AdView(getApplication());
adView.setAdUnitId("ca-app-pub-1845170371498416/123456789");
adView.setAdSize(AdSize.BANNER);
LinearLayout adLinLay = (LinearLayout) findViewById(R.id.Llayout);
adLinLay.addView(adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
"D5AF9D9AFD269EF3B817CD9804BF2CE9").build();
adView.loadAd(adRequest);
Thank everyone! good luck
Can you please try this one ? :
Remove this one:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
Delete the xmlns:ads*** tag
Add this one:
xmlns:ads="http://schemas.android.com/apk/res-auto"
xml file should looks like below:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>
Hope this will help you.

Fixed ad at the bottom of PrefereceActivity

I have a PreferenceActivity for a LiveWallpaper and have used the solution here (LINK) to create an Admob ad. It works, but the ad disappers when the user scrolls up or down and loads again when it comes into view. Is there a way so that the ad always stays at the bottom (or top) of the activity (like in a regular activity)?
Before
((LinearLayout)view).addView(adView);
Try setting the layoutParam like
adView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT))
OR Change the layout like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_unit_id"/>
</RelativeLayout>
And code part would be like this:
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new ToastAdListener(this));
mAdView.loadAd(new AdRequest.Builder().build());

Categories

Resources