adding admob banner into android studio without using activity template - step by step
Post on 21-Mar-2017
1.312 Views
Preview:
TRANSCRIPT
Steps in Adding
Banner in Android Studio
without using
Admob Activity Template
1. Add the following to your AndroidManifest.xml
• INTERNET and ACCESS_NETWORK_STATE permissions
• AdActivity, configChanges and theme.
2. Open build.gradle and add Google Play services dependency which is required by Admob
compile 'com.google.android.gms:play-services-ads:9.2.0'
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
For adding Admob banner using Android Studio templates, click to see video at YouTube.
4. Add Banner ad to the XML layout
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/admob_id" />
3. Create your Banner ad ID string resource
<string name="admob_id">ca-app-pub-3940256099942544/6300978111</string>
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
5. Import AdRequest and AdView
For adding Admob banner using Android Studio templates, click to see video at YouTube.
xmlns:ads="http://schemas.android.com/apk/res-auto"
Then make sure you have added the ads parameter in your layout:
6. Put the code to load the ad into Admob banner view (named “adView” in this case)
inside the onCreate method
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
create and assign an AdView variable to the ad view (adView)
create an AdRequest variable and instantiate it
load the ad to ad view using the AdView variable (adView)
by passing to its loadAd method the AdRequest variable (adRequest)
For adding Admob banner using Android Studio templates, click to see video at YouTube.
top related