Displaying Ad In AndEngine - android

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.

Related

Best proactive when implementing adView

I am making a game and I would like the ad to be visible during the menu but not when the game is actually being played.
If I simple set the adView Visibility to INVISIBLE when I don't want it shown then the ad will not refresh and thus i don't think will register with admob.
Is there an accepted way of implementing ads like this?
edit* Code in main activity to add the adView
private void initialiseAd()
{
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("");
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adView.setLayoutParams(lp);
//load ads
adView.loadAd(adRequest);
//add the adView to the window
mainLayout.addView(adView);
}
The way you're facing it is correct, you can easily hide the AdView simply wrapping it within a FrameLayout (which you already seem to be doing) and calling the setVisibility() method over it.
If you're using Activity, this would be the way:
FrameLayout containingFrame = (FrameLayout) findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);
If you're using Fragment:
FrameLayout containingFrame = (FrameLayout) getActivity().findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);

Android AdMob AIR Native Extension crashes when adding AdView to LinearLayout

I'm creating an AIR native extension to integrate AdMob.
I've followed the guidelines for using external resources described here: http://www.adobe.com/devnet/air/articles/ane-android-devices.html
and packaged google's adMob jar in my native extension jar as described here: AIR 3 Native Extensions for Android - Can I/How to include 3rd party libraries?
I have a NewAdFunction class which implements FREFunction and is called by the actionscript side. The NewAdFunction.call() function contains essentially the following: (in my actual code I have the necessary try catches)
Intent intent = new Intent(context.getActivity(), AdMobActivity.class);
layoutID = context.getResourceId("layout.adlayout");
intent.putExtra("layoutID", layoutID);
context.getActivity().startActivity(intent);
In the above code adlayout is an xml located in res/layout. I package the entire res folder in my final jar and include the following code in my test AIR project's xml.
<application android:enabled="true" android:debuggable="true">
<activity android:name="com.mycompany.admob.AdMobActivity"></activity>
</application>
In the following code everything works up until it reaches layout.addView(adView); At this point the program crashes but nothing is printed through the console.
public class AdMobActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layoutID = getIntent().getIntExtra("layoutID", -1);
setContentView(layoutID);
LinearLayout layout = (LinearLayout)findViewById(layoutID);
AdView adView = new AdView(this, AdSize.BANNER, MY_DEVELOPER_ID);
layout.addView(adView);
}
}
All I could think of is that layout could be null, but I added a trace function and it isn't null.
I discovered that actually the layout variable was indeed null. Instead of creating a layout with an xml, I ended up instead creating it programmatically like so:
public FREObject call(FREContext context, FREObject[] args) {
AdView adView = new AdView(context.getActivity(), AdSize.BANNER, NewAdFunction.developerId);
Activity activity = context.getActivity();
ViewGroup viewGroup = (ViewGroup)activity.findViewById(android.R.id.content);
viewGroup = (ViewGroup)viewGroup.getChildAt(0);
LinearLayout layout = new LinearLayout(activity);
viewGroup.addView(layout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adView.setVisibility(View.VISIBLE);
layout.addView(adView, params);
}
That's the best way I've found to add a view in an AIR native extension.

Android, AdMob: AdMob ad refresh destroys frame rate

I am developing a game and it's coming along quite nicely. I do have a bit of a problem about the AdMob ad refreshing though. Every time the ad is refreshed or it draws a different aspect of the ad, my frame rate plummets and almost makes the game unplayable. Here is what I have for the loading of the ad...
ad = new AdView(this, AdSize.BANNER, "...");
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("...");
adRequest.addTestDevice("...");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
ad.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(renderView);
layout.addView(ad);
ad.loadAd(new AdRequest());
setContentView(layout);
My solution for rendering the ad on top of the SurfaceView was to just add it to a RelativeLayout and add both the SurfaceView and AdView to it. This all works fine and dandy, but every time the ad refreshes (UI or new Ad request), it bogs down the UI thread, which in turn slows down my render thread.
Is there a way that I can make all of this work together nicely to have all work done by the AdView done separately from the main thread? I am not too sure about dynamically updating the current layout from another thread.
Thanks for the help.
I had this problem too. I found it was caused by Google ads which animate when they change, rather than static Admob banner ads which don't animate at all. There's a setting in your admob app settings for controlling whether Google ads are used... try turning it off to see if it makes a difference.
Apologies for resurrecting this question, but I have managed to come across the same issue, and found out that turning LAYER_TYPE_SOFTWARE on the AdView's internal WebView made it smoother.
I have confirmed this working on my side with version 8.4.0 running on Android 5.1, although it is possible that this workaround will benefit more devices with stronger CPUs.
In my activity's onResume I have
/*
* The AdView attaches it's internal WebView only once it
* has been loaded, so we need to wait until that happens.
*/
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
runOnWebView(mAdView, new WebViewAction() {
#Override
public void run(WebView view) {
// the most important part is here
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
});
}
});
with the runOnWebView method and WebViewAction interface defined as
/**
* Recursively searches for a WebView instance located in
* the hierarchy of view and invokes the callback method on
* action with the found instance.
*/
private void runOnWebView(View view, WebViewAction action) {
if (view instanceof WebView) {
action.run((WebView) view);
return;
}
if (view instanceof ViewGroup) {
final ViewGroup parent = (ViewGroup) view;
for (int i = 0; i < parent.getChildCount(); i++) {
runOnWebView(parent.getChildAt(i), action);
}
}
}
private interface WebViewAction {
void run(WebView view);
}

AdMod & GLSurfaceView

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

Android AdSense. Can't see an advertisement in an application

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

Categories

Resources