3

I have an activity in form of a small game which uses SurfaceView. Below are the code snippets.. I am confused how to implement admob on surfaceview. Please suggest.

public class DroidzActivity extends Activity { private static final String TAG = DroidzActivity.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requesting to turn the title OFF requestWindowFeature(Window.FEATURE_NO_TITLE); // making it full screen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // set our MainGamePanel as the View setContentView(new MainGamePanel(this)); Log.d(TAG, "View added"); } } public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { } 

Answer

Updated the code as below

public class DroidzActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG = DroidzActivity.class.getSimpleName(); private static final String MY_AD_UNIT_ID = "XYZ"; private AdView adView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requesting to turn the title OFF requestWindowFeature(Window.FEATURE_NO_TITLE); // making it full screen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // set our MainGamePanel as the View //setContentView(new MainGamePanel(this)); adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); adView.setLayoutParams(lp); RelativeLayout layout = new RelativeLayout(this); layout.addView(new MainGamePanel(this)); layout.addView(adView); adView.loadAd(new AdRequest()); setContentView(layout); Log.d(TAG, "View added"); } @Override protected void onDestroy() { Log.d(TAG, "Destroying..."); if (adView != null) { adView.destroy(); } super.onDestroy(); } 

1 Answer 1

2

Have the root view for DroidzActivity be a LinearLayout containging an AdView and MainGamePanel.

Sign up to request clarification or add additional context in comments.

2 Comments

Thansk for the hint William, updated the question with answer. Please review!
Personally I would have defined the layout in XML but you can code it if you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.