• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Android Preference Admob ad integration  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello . How can i show admob ads in android preference screen .


my preference xml is



and my settings class that extends preference activity is



Thank you
 
Saloon Keeper
Posts: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't AdMob a commercial product that should come with documentation? I'm sure they have example integration code available on their site.
 
siddharth sekhar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Isn't AdMob a commercial product that should come with documentation? I'm sure they have example integration code available on their site.



Hello , Yes . But not on a preference screen . Earlier when I have to integrate admob I used to use the following in the xml layout


and in the  corresponding java file this code


But not able to use this code in a preference activity
 
Tim Moores
Saloon Keeper
Posts: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "not able" mean? What did you try, and how did or didn't that work out?
 
siddharth sekhar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:What does "not able" mean? What did you try, and how did or didn't that work out?



I tried to put the admob view in the preference xml but cannot do that . it gives a warning like "element com.google.android.gms.ads.AdView is not allowed here" , so i cannot access it on my settings class
 
Tim Moores
Saloon Keeper
Posts: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am near certain that it is possible. What results did you get when you googled that error message? I'm sure people have run into that before.

Also post the layout XML where you tried that.
 
siddharth sekhar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:I am near certain that it is possible. What results did you get when you googled that error message? I'm sure people have run into that before.

Also post the layout XML where you tried that.



After some hunting , I found this solution .

1 create a xml layout that would hold the ad view (ad_layout.xml)
2 create a class file extending preference that will define the xml file (AdPreference.java)
3 place this xml file in our Preference xml (preference.xml)

xml file that will hold the adview


Class file for defining the xml file


Place it in our preference xml


It is working , But I have some confusions

a. Where should I Place MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); line of code

b. How the class AdPreference is associated with ad_layout.xml . How this class know which layout file to look for . since there is no mention of as_layout.xml file in this class

c. How can I place the ad on the bottom of the screen . At this time it is just below the edit text .

d. The solution only uses the unit ID of the ad . So we do not need the admob app ID here.

 
Tim Moores
Saloon Keeper
Posts: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

b. How the class AdPreference is associated with ad_layout.xml . How this class know which layout file to look for . since there is no mention of as_layout.xml file in this class



The association is rather obvious, isn't it:

<com.mygdx.game.AdPreference android:layout="@layout/ad_layout"/

 
siddharth sekhar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

b. How the class AdPreference is associated with ad_layout.xml . How this class know which layout file to look for . since there is no mention of as_layout.xml file in this class



The association is rather obvious, isn't it:

<com.mygdx.game.AdPreference android:layout="@layout/ad_layout"/



Thanks for pointing out the obvious for me. I was searching for something like R.layout.ad_layout  and missed to see what you have shown to me .
 
I love a good mentalist. And so does this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!