How to draw admob on canvas?

I have a problem with AdMob showing up on my canvas.

My code is shown below.

layout = new LinearLayout(getContext()); adView = new AdView((Activity) getContext(), AdSize.BANNER, "############"); layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); adView.setVisibility(View.VISIBLE); layout.addView(adView); TextView textView = new TextView(context); textView.setVisibility(View.VISIBLE); textView.setText("Hello world"); layout.addView(textView); AdRequest adRequest = new AdRequest(); adRequest.addTestDevice(AdRequest.TEST_EMULATOR); adRequest.addTestDevice("TEST_DEVICE_ID"); adView.loadAd(adRequest); 

In my onDraw method I have.

  canvas.drawColor(Color.GRAY); layout.measure(getWidth(), getHeight()); layout.layout(0, 0, getWidth(), getHeight()); layout.draw(canvas); 

"Hello World" will appear, but not Ad.

+4
source share
2 answers

I assume that โ€œhello worldโ€ was for testing?

Have you tried calling adView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); ?

When you call addView in a view group, by default the child inherits the layoutparams of the parent, and since in your case the parent layoutparams are wrap_content, then this may cause the ad window to be invisible.

In addition, you should not call layout.measure in your onDraw, you should probably do this in the onMeasure method.

0
source

If you haven't decided this yet: get rid of Hello World TextView or declare it before

 adView.setVisibility(View.VISIBLE); layout.addView(adView); 
0
source

Source: https://habr.com/ru/post/1388838/


All Articles