Android AdMob AIR Native Extension crashes when adding AdView to LinearLayout - android

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.

Related

Appodeal Banner Ad Covers Screen Android/LibGDX

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);
}
}

Sencha Touch - won't resize when AdView is added

I have this:
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
super.root.requestLayout();
}
When the ad loads, it covers up the bottom of my application. If I switch the orientation, everything re-renders correctly. This must mean that the WebView is the right size, but Sencha Touch isn't respecting the size change, right?
How do I fix this?
When adding an AdView programmatically to would normally add it into a ViewGroup that you have specially created and sized for that purpose. I have no idea what Sencha Touch is but if super.root is the ViewGroup for the whole Activity then your display will depend entirely upon what type of Layout what specified for the root View.
Suggest you create a AdViewParent in your layout and add the AdView into that.
The trick is to put adView not onto the super.root layout but onto the layout of WebView (appView of DridGap). Here is the code for horizontal align banner in bottom of the screen
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
//
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
AdSize.BANNER.getHeightInPixels(this));
rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);
//
appView.addView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
request adRequest = new AdRequest();
adView.loadAd(request);
}
Using this method you have to avoid overlapping of content by admob banner. You can do it in app.scss, someting like
#bottomitemid{
padding-bottom: 50px;
}

Displaying Ad In AndEngine

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.

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