I dont know how to get this to working within my game...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
int newID = glView.getId();
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "a14e3ef0948eb58");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(newID);
// Add the adView to it
layout.addView(adView);
Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
This gives me a Null Pointer Exception and I cant figure out what else to do with this. Im just needing to know how to get this to work with the way I have programed this. I dont use any XML.
Also the "newID" value become -1 so thats where the error is coming from
Any help will be appreciated
Thanks
Try this,
You don't use XML so that means your view isn't created static(in xml) but dynamic(in java).
Dynamically created views don't have a id already associated with them yet, so you must set one explicitly.
view.setId(1)**//choose a non negative integer
Check this out: Get ID for views that are added dynamically
Related
I’m trying to insert Appodeal banner adds into a LibGDX application but the banner ad is covering the screen instead of being placed above it. I cannot find any tutorials for using Appodeal in LibGDX, so I’m working from this tutorial on using Admob in LibGDX. https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx
I cannot figure out how to get the Appodeal banner ad in the form of a View which can be added to the RelativeLayout using the addView() method. I have tried a couple ways to do this which are labeled “ATTEMPT #1” and “ATTEMPT #2” but in both attempts the banner ad does not display at all. The line “Appodeal.show(this, BANNER);” places the banner on top of the screen, so that is commented out. Any help getting this figure out would be greatly appreciated. Thanks.
Here is my AndroidLauncher.java file where I create and show the banner ad.
public class AndroidLauncher extends AndroidApplication {
private final int BANNER = 4;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// LAYOUT: combines LibGDX View and Ad View
RelativeLayout relativeLayout = new RelativeLayout(this);
// Ad View
Appodeal.initialize(this, "fee50c333ff3825fd6ad6d38cff78154de3025546d47a84f", BANNER);
Appodeal.setTesting(true);
// Appodeal.show(this, BANNER);
// ATTEMPT #1
BannerView bannerView = Appodeal.getBannerView(this);
// ATTEMPT #2
// BannerView bannerView = new BannerView(this, null);
// Appodeal.setBannerViewId(bannerView.getId());
// LibGDX View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View libgdxView = initializeForView(new AppodealGDXDemo(), config);
// add to view
relativeLayout.addView(bannerView);
relativeLayout.addView(libgdxView);
// set layout
setContentView(relativeLayout);
}
}
I finally found info on the Appodeal website that helped me out. https://wiki.appodeal.com/en/android/2-5-8-android-sdk-integration-guide/ad-types
It appears they have made a lot of changes to their website as the links to documentation on their website that I found from google redirected to the main page.
I got it working now and here is the updated code.
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// LAYOUT: combines LibGDX View and Ad View
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Ad View
Appodeal.initialize(this, "fee50c333ff3825fd6ad6d38cff78154de3025546d47a84f", Appodeal.BANNER_VIEW);
Appodeal.setTesting(true);
BannerView bannerView = Appodeal.getBannerView(this);
Appodeal.show(this, Appodeal.BANNER_VIEW);
// LibGDX View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View libgdxView = initializeForView(new AppodealGDXDemo(), config);
// add to layout
layout.addView(bannerView);
layout.addView(libgdxView);
// set layout
setContentView(layout);
}
}
i added banner ads from admob to my game and the banner add appears only after the first refresh (30 seconds) . And for the first 30 seconds it's there and if you click the bottom of the page where the addvertisment should be it takes you to the advertising page but the ad is not visible.
You can see for yourself when you start the game and click bottom of the screen.
Game #1
And my other game (if you start the intent from the menu in this game ( like facebook) and then return to the menu, ads appear instantly):
Game #2
Anyone had this happen ? What can i do to fix this ?
How I initialize my ads :
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
opener = new AndroidGalleryOpener(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View gameView = initializeForView(new Zaidimas(opener), config);
AdView adView = new AdView(this);
adView.setAdUnitId("xxxxx my secret number xxxxxxxx");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.build());
layout.addView(gameView);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(adView, adParams);
setContentView(layout);
//initialize(new Zaidimas(opener), config);
}
It is hard to say what happens in your case, but I have a suspect that your libgdx screen with the admob banner might show up before the ad finished loading. You can try adding an AdListener that forces the ad-view to draw itself once the ad finished loading. Something like:
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
System.out.println("ad banner finished loading!");
adView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);
}
});
The code looks a bit odd, but changing visibility should really make sure that your adView gets drawn again ;-)
If you're using a Relative Layout be sure to include the position of the view relative to other layout objects. I was having the same issue until I placed android:layout_below="#+id/adView" on the object directly below my adView object. You could probably just as well place android:layout_above="#+id/someView" on the adView object itself.
I am really new to programming. I have followed the tutorial in Libgdx and google,
but i cannot put a banner ad using admob in my application.
Can someone help me to teach what i should do to put admob banner ad in my application ?
Thank you,
You need to create a View to contain both the adMob View and the Libgdx View, then setContentView() to this layout to which you must add both Views. Is this the tutorial to which you refer? It is very helpful.
Inside your android game-android project open the Activity and create a relative layout that is fullscreen and has no title bar as-
RelativeLayout layout=new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
Now below this change this line-
initialize(new Mygame(), cfg);
to
View gameView=initializeForView(new MyGame() , cfg);
This method initializeForView(new MyGame() , cfg); returns a view which is saved as a instance of view that will be added to the relative layout. Now create adview also to add to relative layout as-
Adview adView=new AdView(this);// create new adview instance
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);// set the adsize
adView.setAdUnitId("XXXXXXXXX");// your adunit id
//Create layout params for adview
RelativeLayout.LayoutParams adParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_RIGHT);//set position
//Add both gameview and adview to the relative layout-
layout.addView(gameView);
layout.addView(adView, adParams);
//Now just request ad by creating new instance of AdRequest
AdRequest request=new AdRequest.Builder().build();//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) to add test device
adView.loadAd(request);
// At last setcontent view to the Relative Layout that has both gameView and adView.
setContentView(layout);
You need to override onPause , onResume and onDestroy also to handle pause , resume or destorying of ad.
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onPause() {
super.onPause();
if(adView!=null){
adView.pause();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(adView!=null){
adView.destroy();
}
}
Upto here adView is added to your game, but if you want to handle on when to show adView.You may want to hide adView when game is running and show when game is over, which can be achieved by creating handler.
I am trying to display an advertisement using Greystrip in AndEngine.
I cannot figure out how this is done because it doesnt use a Layout to inflate views, but yet sprites.
i use BaseGameActivity to create my application for each scene i would like to display adds on.
In GreyStrip this is how they tell you to integrate ads in your application..
Before adding calls in your application to GSSDK, you need to
incorporate the SDK into your AndroidManifest.xml. Add the following
in the section, replacing
with a package identifier that is unique to your application. This
Content Provider manages local storage of ad content, while the
Activity manages ad display.
<provider android:name="com.greystripe.android.sdk.AdContentProvider"
android:authorities="<YOUR_APPLICATION_PACKAGE>.AdContentProvider"
android:multiprocess="true"
android:exported="false" />
<activity android:name="com.greystripe.android.sdk.AdView"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To initialize the Greystripe SDK, call the initialize method at
startup. This should be done within your application’s onCreate()
method. This call will spawn a background thread to initialize our
activity, and then return control to your app. In this background, the
Greystripe activity will download ads as well as any SDK updates.
Parameters: ctx: Your application Context instance appId: Use the
appId provided during app registration. Providing an invalid appId
will cause the SDK to display error notification ads.
public static GSSDK initialize(Context ctx, String appId)
To use a banner, place the following in your main.xml file:
<view class="com.greystripe.android.sdk.BannerView"
android:id="#+id/gsBanner"
android:layout_width="320dp"
android:layout_height="48dp"/>
To reference the banner view in code, use findViewById, as with any
main.xml element:
BannerView myBanner = (BannerView) findViewById(R.id.gsBanner);
To request adds call
myBanner.refresh();
Now the problem is since i dont have an xml layout i cant figure out how i inflate the layout for the ad view?
Anyone have any ideas?
EDIT:
Ive seen someone do it like this in a tutorial online, but how can i inflate this in andengine?
try {
String applicationId = Utils.scrapeIgnoreCase(externalParams, "<param name=\"id\">", "</param>");
GSSDK.initialize(context, applicationId);
BannerView myBanner = new BannerView(context);
myBanner.setLayoutParams(view.getLayoutParams());
myBanner.addListener(new GreyStripeBannerListener());
view.addView(myBanner);
myBanner.refresh();
myBanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
I'm using AdMob but it should be similar.
Like #Sergey Benner referenced, you have to override onSetContentView in your activity, then create the RenderSurfaceView and your ad view manually.
First of all, create a FrameLayout to contain AndEngine's view and the ad view.
Add AndEngine's view and create your ad view, then set the frame layout as the content view.
#Override
protected void onSetContentView() {
//Creating the parent frame layout:
final FrameLayout frameLayout = new FrameLayout(this);
//Creating its layout params, making it fill the screen.
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
//Creating the banner view.
BannerView bannerView = new BannerView(this);
//....
//Do any initiallizations on the banner view here.
//....
//Creating the banner layout params. With this params, the ad will be placed in the top of the screen, middle horizontally.
final FrameLayout.LayoutParams bannerViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.TOP | Gravity.CENTER_HORIZONTAL);
//Creating AndEngine's view.
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, this);
//createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//Adding the views to the frame layout.
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(bannerView, bannerViewLayoutParams);
//Setting content view
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
Place this method in your BaseGameActivity class.
I have a problem with AdSence. I have tried to resolve it the last couple of days but without any good results. I used developers guide but it still empty on the place of banner.
What am I doing in an application. First of all I have added a TableRow object and added AdView to it:
banner = new TableRow(engine);
banner.setGravity(Gravity.TOP & Gravity.RIGHT);
adView = new AdView(engine, AdSize.BANNER, "a14def8xxxxxxxx");
banner.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
Afterwards I just added this "banner" object to the other view. At the end - there is no output there. In case I changed AdView to TextView, just for the proof of concept, it works fine.
The output log:
06-13 18:04:38.476: INFO/Ads(576): Received ad url: <"url": "http://r.admob.com:80/ad_source.php?... >
06-13 18:04:40.406: INFO/Ads(576): onReceiveAd()
The only strange thing for me in log is:
06-13 18:04:40.336: WARN/webcore(576): Can't get the viewWidth after the first layout
I haven't found what is it means and is it because of AdSence.
Update
I have the class:
public class QuestEngine extends Activity {
Then I am trying to produce new AdView in method:
public IntroView(QuestEngine engine) {
That is why in "new AdView" I am using engine object.
Change the new AdView declaration to:
adView = new AdView(this, AdSize.BANNER, "a14def820df0417");
The engine object does not contain the context for its dimensions. The dimensions of the table row are in the Activity context "this"
Well if you are setting your adView via xml/layout file then do as directed below:-
In your activity.xml file insert the following tag where you want your AdView to be shown:-
<com.google.ads.doubleclick.DfpAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="/XXXXX/XXXXX"
ads:adSize="BANNER"
/>
And in your Activity.java file write this code in your onCreate() Method of the Activity Life Cycle:-
// Look up the DfpAdView as a resource and load a request.
adView = (DfpAdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
Now if you want to programmatic set the AdView in your Activity then follow the steps as directed below:-
Write the following code in your onCreate() method,
I am demonstrating for Fragment, so write the following code in your onActivityCreated()Method.
// Create an ad view as a second header for now to meet deadlines
DfpAdView mAdView=null; //you can also define this as a Global Variable.
if (mAdView == null) {
mAdView = new DfpAdView(getActivity(), AdSize.BANNER, "/XXXX/XXXX"); //give your adId incase of XXXX
iaddHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); //This code is used to set the height of the addHeight as 50 pixels, which could be used in layout params.
RelativeLayout fl =new RelativeLayout(this.getActivity());
RelativeLayout.LayoutParams flParams =new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
flParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
FrameLayout.LayoutParams para =new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
para.setMargins(0, 0, 0,iaddHeight);
getListView().setLayoutParams(para);
this.getActivity().addContentView(fl, flParams);
fl.addView(mAdView,flParams);
}
mAdView.loadAd(new AdRequest());
Hope This can help Any Future users...