Adding ads to the overlayView in Android

I’m trying to implement Flurry Ads on android. I’m adding it to the FrameLayout retrieved from getOverlayView(), as suggested in the documentation. In my CoronaApplication I do the following:

@Override  
 public void onStarted(com.ansca.corona.CoronaRuntime runtime) {  
  
 // Analytics  
 FlurryAgent.onStartSession( com.ansca.corona.CoronaEnvironment.getCoronaActivity() , "MYAPICODE");  
 FlurryAgent.logEvent("Game Started Android");  
  
 // Ads  
 FlurryAgent.initializeAds( com.ansca.corona.CoronaEnvironment.getCoronaActivity() );  
 //FlurryAgent.enableTestAds( true );  
 FlurryAgent.setLogEnabled(true);  
 FlurryAgent.setLogLevel( 1 );  
  
 // Showing the ad  
 FlurryAgent.getAd( com.ansca.corona.CoronaEnvironment.getCoronaActivity() , "Block Struggle Android", com.ansca.corona.CoronaEnvironment.getCoronaActivity().getOverlayView() , FlurryAdSize.BANNER\_BOTTOM, 0);  
  
  
 }  

When my game starts, the ads are loaded fine at the bottom of the screen. However, I ONLY see the ads, meaning that I get a full white screen blocking my game, and then the ads on top of that white screen.

When the ads are refreshed (every 30 seconds), the white screen is removed along with the banner, revealing the game beneath for half a second or so, and then a new whitescreen and banner appears.

Why does this happen, and can anyone suggest a solution? [import]uid: 66464 topic_id: 34139 reply_id: 334139[/import]

Anyone? I really need help on this one.

If I try to add a view to the FrameLayout provided by the getOverlayView() function, then I get the message that only the original thread that created the hirachy is allowed to do anything to the layout.

Is this a bug in Corona? I thought I would be able to add viewGroups to the overlay layout? [import]uid: 66464 topic_id: 34139 reply_id: 135767[/import]

Is this issue only happening on an Android 4.x device? If so, then that sounds like a famous Google hardware acceleration bug. Ads are typically implemented in Android via a WebView class with a transparent background, but hardware accelerated WebViews won’t show a transparent background and will show a white background instead.

To work-around this issue, you’ll need to fetch a reference to the Flurry ad view and set it up to use software rendering instead as follows…
[java]
// First, you’ll need to get a hold of the ad view reference.
android.view.View adView = FlurryAgent.getAd(…);

// Enable/disable hardware acceleration via reflection.
if (android.os.Build.VERSION.SDK_INT >= 11) {
try {
java.lang.reflect.Method setLayerTypeMethod = android.view.View.class.getMethod(
“setLayerType”, new Class[] {Integer.TYPE, android.graphics.Paint.class});
setLayerTypeMethod.invoke(view, new Object[] { 1, null });
}
catch (Exception ex) { }
}
[/java]

Note that we disable hardware acceleration up above via reflection because the [lua]setLayerType()[/lua] method only exists in Android API Level 11 (ie: OS version 3.0) and above. This allows your app to continue to support Android 2.2 and 2.3.

I hope this helps! [import]uid: 32256 topic_id: 34139 reply_id: 135787[/import]

Anyone? I really need help on this one.

If I try to add a view to the FrameLayout provided by the getOverlayView() function, then I get the message that only the original thread that created the hirachy is allowed to do anything to the layout.

Is this a bug in Corona? I thought I would be able to add viewGroups to the overlay layout? [import]uid: 66464 topic_id: 34139 reply_id: 135767[/import]

Is this issue only happening on an Android 4.x device? If so, then that sounds like a famous Google hardware acceleration bug. Ads are typically implemented in Android via a WebView class with a transparent background, but hardware accelerated WebViews won’t show a transparent background and will show a white background instead.

To work-around this issue, you’ll need to fetch a reference to the Flurry ad view and set it up to use software rendering instead as follows…
[java]
// First, you’ll need to get a hold of the ad view reference.
android.view.View adView = FlurryAgent.getAd(…);

// Enable/disable hardware acceleration via reflection.
if (android.os.Build.VERSION.SDK_INT >= 11) {
try {
java.lang.reflect.Method setLayerTypeMethod = android.view.View.class.getMethod(
“setLayerType”, new Class[] {Integer.TYPE, android.graphics.Paint.class});
setLayerTypeMethod.invoke(view, new Object[] { 1, null });
}
catch (Exception ex) { }
}
[/java]

Note that we disable hardware acceleration up above via reflection because the [lua]setLayerType()[/lua] method only exists in Android API Level 11 (ie: OS version 3.0) and above. This allows your app to continue to support Android 2.2 and 2.3.

I hope this helps! [import]uid: 32256 topic_id: 34139 reply_id: 135787[/import]

Thank you very much!! This helped a lot.

Unfortunately, Flurry doesn’t enable us to get the View of the ads. Is it at all possible to add a view to the FrameLayout provided by getOverlayView() by using addView()?

I constantly get an error saying that the view is owned by Corona and that only Corona has permission to add views to the layout? [import]uid: 66464 topic_id: 34139 reply_id: 136219[/import]

This is the message I get when I try to add a view to the getOverlayFrame().addView():

“Only the original thread that created the view hierarchy can touch its views” [import]uid: 66464 topic_id: 34139 reply_id: 136227[/import]

I don’t think I quite understand the getOverlayView() FrameLayout. We’re supposed to use it for overlaying views on the corona activity. But we can’t add views to it, because it is owned by corona.

How do I use it? Help would be very much appreciated! [import]uid: 66464 topic_id: 34139 reply_id: 136337[/import]

Sorry about the late response.

Yes, you are supposed to add your view to the layout returned by [lua]CoronaActivity.getOverlayView()[/lua], but Android does not allow you to manipulate UI objects from another thread. You see, the Corona runtime does not run on the main UI thread. So, you cannot access the UI via a CoronaRuntimeListener or JNLua NamedJavaFunction because their methods are called on the Corona runtime thread, which our API documentation warns about.

The best way to access the UI thread from the Corona runtime’s thread is to post a [lua]Runnable[/lua] object via the [lua]CoronaActivity.runOnUiThread()[/lua] method. Have a look at sample project “ExtendingUI” that is included with Corona Enterprise for an example on how to do this.

Also, have a look at “AsyncCallLuaFunction.java” in sample project “ExtendingLua” for an example on how to get back on the Corona runtime’s thread from the main UI thread in case you need to call a Lua function… because you should never access the LuaState object from another thread either or else race conditions and crashes can occur.

I hope this helps! [import]uid: 32256 topic_id: 34139 reply_id: 136379[/import]

Awesome, thank you! This makes insanely good sense, I guess I should have gone through the sample projects myself :slight_smile: … with this I am ready to become a full subscriber of the Enterprise version. Thank you! [import]uid: 66464 topic_id: 34139 reply_id: 136433[/import]

Glad I could help! And good luck with your project! :slight_smile: [import]uid: 32256 topic_id: 34139 reply_id: 136474[/import]

Thank you very much!! This helped a lot.

Unfortunately, Flurry doesn’t enable us to get the View of the ads. Is it at all possible to add a view to the FrameLayout provided by getOverlayView() by using addView()?

I constantly get an error saying that the view is owned by Corona and that only Corona has permission to add views to the layout? [import]uid: 66464 topic_id: 34139 reply_id: 136219[/import]

This is the message I get when I try to add a view to the getOverlayFrame().addView():

“Only the original thread that created the view hierarchy can touch its views” [import]uid: 66464 topic_id: 34139 reply_id: 136227[/import]

I don’t think I quite understand the getOverlayView() FrameLayout. We’re supposed to use it for overlaying views on the corona activity. But we can’t add views to it, because it is owned by corona.

How do I use it? Help would be very much appreciated! [import]uid: 66464 topic_id: 34139 reply_id: 136337[/import]

Sorry about the late response.

Yes, you are supposed to add your view to the layout returned by [lua]CoronaActivity.getOverlayView()[/lua], but Android does not allow you to manipulate UI objects from another thread. You see, the Corona runtime does not run on the main UI thread. So, you cannot access the UI via a CoronaRuntimeListener or JNLua NamedJavaFunction because their methods are called on the Corona runtime thread, which our API documentation warns about.

The best way to access the UI thread from the Corona runtime’s thread is to post a [lua]Runnable[/lua] object via the [lua]CoronaActivity.runOnUiThread()[/lua] method. Have a look at sample project “ExtendingUI” that is included with Corona Enterprise for an example on how to do this.

Also, have a look at “AsyncCallLuaFunction.java” in sample project “ExtendingLua” for an example on how to get back on the Corona runtime’s thread from the main UI thread in case you need to call a Lua function… because you should never access the LuaState object from another thread either or else race conditions and crashes can occur.

I hope this helps! [import]uid: 32256 topic_id: 34139 reply_id: 136379[/import]

Awesome, thank you! This makes insanely good sense, I guess I should have gone through the sample projects myself :slight_smile: … with this I am ready to become a full subscriber of the Enterprise version. Thank you! [import]uid: 66464 topic_id: 34139 reply_id: 136433[/import]

Glad I could help! And good luck with your project! :slight_smile: [import]uid: 32256 topic_id: 34139 reply_id: 136474[/import]