admob of google play service does not showing over surfaceview - android

I have an android game that draws on a surfaceView and I have admob ads on top of that using a FrameLayout.
FrameLayout lay = new FrameLayout(this);
this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx");
lay.addView(renderer, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
adView.setGravity(Gravity.LEFT);
this.setContentView(lay);
adView.loadAd(new AdRequest());
and everything is fine. I want to integrate the new google play services admob and I have changed the above code to
FrameLayout lay = new FrameLayout(this);
this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
adView = new AdView(this);
adView.setAdUnitId("xxxxxxx");
adView.setAdSize(AdSize.BANNER);
lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.setContentView(lay);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
the admob view is not shown up. I know that it is the view that does not come up because if i add only the AdView into the frameLayour then I see the ads without any problem
Has anyone came across this problem? Has anyone any suggestion about that?
king regards

Make sure you set a background color for the adview. It can be "transparent". Then, using a RelativeLayout or a FrameLayout as your parent layout, define the layout parameters for the adView to be positioned, for example at the bottom center of the screen like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
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(renderer);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}

Related

how to include banner ad on SurfaceView ?

I'm using surface view in my android app (no any XML for that activity). I want to include banner ad in that activity.
how to include banner ad on surface view
You should post your game's main activity. To integrate banner ads on surface views programmatically, you use a RelativeLayout or a FrameLayout as your parent layout, and define the layout parameters for the adView to be positioned (for example at the bottom center of the screen) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
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(myView);
layout.addView(adView, adParams);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
//Set main renderer
setContentView(layout);
}

AdMob Banners and GLSurfaceview (programmatically)

Finally, I have a banner ad at the top of my GLSurfaceview. However, it appears with a black background that takes up the whole width of the screen and covers the top of my game area like so (I should also point out that the play area is also moved downwards a little so the bottom is also missing).
What I need to do is move the banner to the bottom of the screen and, keep it central and remove this black background so it looks like this:
I tried to use XML but was getting a lot of errors so I switched to doing this completely in Java (and managed to get this far) - however quality information on how to do this with GLSurfaceView is lacking IMHO so I'm hoping someone can tell me where I'm going wrong.
Code
Here is my onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create an ad request.
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
layout.addView(adView);
layout.addView(myView);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
//Set main renderer
setContentView(layout);
}
When the banner changes, it also appears to 'flicker', but I can deal with that in a separate question.
Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned at the bottom center of the screen like this. Below is a solution:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
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(myView);
layout.addView(adView, adParams);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
//Set main renderer
setContentView(layout);
}
You have two separate views that are divided when you add them both to a linear layout (surface view and your ad view). I think what you want to accomplish is to overlay the ad on top of your surface view. This page is old but check out http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/ for an example on how to do this.

Why can't I see the ad?

I tried to display an ad on top of a custom view but somehow I can't see it.
layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
view = new GameView(this);
adView = new AdView(this);
adView.setAdUnitId("id");
adView.setAdSize(AdSize.BANNER);
layout.addView(view);
layout.addView(adView, 640, 100);
adView.loadAd(new AdRequest.Builder().addTestDevice("id").build());
setContentView(layout);
I think it has something to do with the way you're adding Views to the LinearLayout. The adView seems ok, but my bet is there's something with the GameView class that makes it not show up.
I'd recommend defining the AdView containing LinearLayout in the layout file instead of declaring a new LinearLayout programmatically, and add the View afterwards like you're doing right now. The following code is the one I use and it works, that's why I assume this is a layout problem.
final LinearLayout adLayout = (LinearLayout) findViewById(R.id.your_ad_containing_linearlayout);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-id");
adView.setAdSize(AdSize.BANNER);
adLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
adReq.addTestDevice("...");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
I think the problem is the LinearLayout.
If you want to display the ad on top of the GameView, you should use FrameLayout simply like below
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view = new GameView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdView adView = new AdView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
adView.setLayoutParams(params);
adView.setAdUnitId("id");
adView.setAdSize(AdSize.BANNER);
layout.addView(view);
layout.addView(adView, 640, 100);
adView.loadAd(new AdRequest.Builder().addTestDevice("id").build());
I think the problem is that you set your 'parent' layout's orientation as Horizontal. Try set it as Vertical.
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
And make sure your custom view GameView doesn't have the layout height as MATCH_PARENT, because add it first, and if it's height is set to fill the parent the view below it won't be visible.

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

Help Adding another view to GLSurfaceView

I am trying to add another view to my GlSurfaceView however I keep kicking up errors.
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
glView.setId(932203934);
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);
GLSurfaceView layout = (GLSurfaceView)findViewById(newID);
// Add the adView to it
this.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
I have added "setID" as the glView always gave -1, but now with this I get "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Clearly I am not doing this right so how do I had this additional view to the glsurfaceview?
Thanks
Anyone else having this problem....
AdView adView = new AdView(this, AdSize.BANNER, "a14e3ef0948eb58");
LinearLayout ll = new LinearLayout(this);
ll.addView(adView);
this.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);

Categories

Resources