Question

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);
}
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top