How to place banner ads in android apps. [ Android studio tutorial ]

Hello, friends. In this android studio tutorial, we will see how we can place banner ads in the android studio. Before that, we will see what are banner ads and what are benefits of using banner ads.

Useful Links

What are banner ads?





The little wider ads at the bottom or upper of app screen are called banner ads. Most of the apps are using these ads because you can have extra revenue with these ads. These are not annoying and most of the user don’t hate banner ads. But keep in mind that you should not provide banner ad near to the buttons because the user will get irritated and he/she will uninstall your app.

Steps to place banner ads

  • Edit Gradle.build(app) file

In here you have to add a line of code. Then click on sync project.

dependencies {
compile fileTree
(dir: ‘libs’, include: [‘*.jar’])
compile
‘com.android.support:appcompat-v7:25.3.1’
compile ‘com.google.android.gms:play-services-ads:11.0.0’
}

  • Edit MainActivity.java file

Now in main java file add a simple line of code and replace  YOUR_ADMOB_APP_ID string with your AdMob app id. I repeat here you have to type your AdMob app id, Not ad unit. When you will create AdMob account and create a new app. They will give you two ids, First is AdMob app id and other is ad unit. Now type following code in onCreate method.

MobileAds.initialize(this, “YOUR_ADMOB_APP_ID”);

 

  • Edit the XML file.





Now open the XML file where you want to place a banner ad. Enter the following code and adjust height width according to the need. Please keep in mind that don’t use your original ad unit until your app is ready to be published on play store. Otherwise, your account can be banned because you are violating AdMob policies.

<com.google.android.gms.ads.AdView
xmlns:ads=“http://schemas.android.com/apk/res-auto”
android:id=“@+id/adView”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerHorizontal=“true”
android:layout_alignParentBottom=“true”
ads:adSize=“BANNER”
ads:adUnitId=“ca-app-pub-3940256099942544/6300978111”>
</com.google.android.gms.ads.AdView>

 

  • Edit MainActivity.java again

Now to load ad, enter following code

 
 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
 private AdView mAdView;

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MobileAds.initialize(getApplicationContext(),
 "ca-app-pub-3940256099942544~3347511713");

mAdView = (AdView) findViewById(R.id.adView);
 AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
 }
 }

 

Conclusion

Nowyou havee successfully implemented banner ad. If you are facing any problem then you can comment below or read official android developer documentation.

 


 

Gaurav singla

Founder of Techyunk.com. I write blogs about android tutorials, blogging tips, programming etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 6 =