Is it possible to generate ad Banner using java coding in android eclipse, without using any xml <com.google.....>
tags?
Asked
Active
Viewed 984 times
-1

Pedro del Sol
- 2,840
- 9
- 39
- 52

gautam parmar
- 1
- 3
-
1Yes create a view programmatically and assign the view to ad – Prateekro Apr 07 '17 at 10:07
-
1have you any source code – gautam parmar Apr 07 '17 at 10:08
-
http://stackoverflow.com/a/24411058/2714340 check this to create a view – Prateekro Apr 07 '17 at 10:10
-
Also, Check this http://stackoverflow.com/a/24776339/2714340 and for customization have a look at my answer. – Prateekro Apr 07 '17 at 13:28
2 Answers
0
You can create an object of AdView and add to frame layout like,
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adView);
frameLayout.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Hope this help!

Wilson Christian
- 650
- 1
- 6
- 17
0
Add this in your Activity
//Add this in OnCreate of Activity to initialize the ad
MobileAds.initialize(getApplicationContext(), "< your-ad-unit-Id >");
//Add this wherever your code needs to add the ad
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//Additionally to adjust the position to Bottom
layout.setGravity(Gravity.BOTTOM);
// Create a banner ad
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("<your-ad-unit-Id>");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the AdView to the view hierarchy.
layout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequestBuilder.build());
setContentView(layout);
Then check if you have given the permissions in Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Add meta data below inside < application > in Manifest
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
And finally check if you have added required dependency to compile in app gradle
compile 'com.google.android.gms:play-services:9.6.1'

Prateekro
- 566
- 1
- 6
- 28