I am new in android app development and I am creating an app in android studio, which has 2 activities. One is activity_main.xml which has a button to open second activity. When I set the banner ad it shows only on bottom of activity_main.xml and I want to set same banner ad in my second activity. What should I do. Please help me.thanks!!
Asked
Active
Viewed 1,397 times
2 Answers
0
You'd need to copy the ad banner view from activity_main.xml
to your second activity's layout.
Also, please include code in your questions as it makes it easier for people to help you!

David Liaw
- 3,193
- 2
- 18
- 28
-
I copied and paste it in second activity but not working, can you tell me that have I to do something in Main_activity.java after copy paste the ad view. – Suman Bharti Jul 06 '17 at 02:20
-
Could you edit your original post with the layout code of your first and second activity as well as your Main_activity.java? – David Liaw Jul 06 '17 at 02:23
-
sorry,i am not able to add layout code to this question, when i try to add code an error comes that your code is not properly formatted as code. so i can't add my layout code. – Suman Bharti Jul 06 '17 at 03:21
-
haha alright so in your activity_main.xml at the bottom I assume there would be a layout that contains the ad layout right? It'd probably be an `AdView` or something similar. You'd just have to copy that to your second activity layout then initialise it in the second activity class. For reference here's the official docs: https://developers.google.com/admob/android/banner – David Liaw Jul 07 '17 at 04:12
0
Create a super class for FirstActivity
and SecondActivity
by inheriting Activity
class. call integrateAdView()
from onCreate()
method of child class.
integrateAdView()
method of super class :
public void integrateAdView(){
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(android.view.Gravity.BOTTOM | android.view.Gravity.CENTER_HORIZONTAL);
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
adView = new AdView(this, AdSize.BANNER, ADV_PUB_ID);
adView.setAdListener(new AdListener(){
...
});
layout.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(request);
}

Abhishek Aryan
- 19,936
- 8
- 46
- 65