Sencha Touch - won't resize when AdView is added - android

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

Related

admob of google play service does not showing over surfaceview

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

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.

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

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

Ad is not coming at bottom

i have a problem regarding alignment of ad. The ad is coming at top of the app but i need it at the bottom of the app. I didnt defined any layout for ad just directly tried in java. And its working fine but only thing is its coming at top even after i inserted adView.setGravity(Gravity.BOTTOM);
The code goes as below
// Lookup R.layout.main
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
AdView adView = new AdView(this, AdSize.BANNER, "a14e36b9902bfcc");
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
adView.setGravity(Gravity.BOTTOM);
You need to set the LayoutParams of your RelativeLayout to :
RelativeLayout.LayoutParams params=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
Specify the parameters when you add the view to the relative layout
Change
// Add the adView to it
layout.addView(adView);
to
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
layout.addView(adView,params);

Categories

Resources