Breaking News

How to create a Banner ad in Android App | Place Banner Ads




If you are an Android Developer and want to earn money from your android app. Then you have to place Ads in your Android App.

There is some type of ads you can place in your Android App.



Today, In this post we learn how to create a Banner ad in your Android App.

To place a Banner Ad, we have to create a new Admob Account and add your app there and then create a new Banner Ad there and create Ad unit Id. seen in the pictures below.


Your Application Name


Type of Ad


Banner Ad set-up


Then, in Android Studio, first, we have to add new google ads dependency in our app-level Gradle file.

app-level Gradle file
 implementation 'com.google.android.gms:play-services-ads:18.3.0'  

Then, we create a banner ad layout in our XML file.

MainActivity.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Hello World!"  
     app:layout_constraintBottom_toBottomOf="parent"  
     app:layout_constraintLeft_toLeftOf="parent"  
     app:layout_constraintRight_toRightOf="parent"  
     app:layout_constraintTop_toTopOf="parent" />  
   <com.google.android.gms.ads.AdView  
     android:id="@+id/adView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_centerHorizontal="true"  
     android:layout_gravity="bottom|center"  
     app:adSize="BANNER"  
     app:adUnitId="@string/bannerAdMobId">  
     </com.google.android.gms.ads.AdView>  
 </LinearLayout>  

Then place your application id in your manifest.xml under meta-data tag

For Application Id open your Admob Account and open your Ad unit then click on implementation instructions, Upper one which contains tilde (~) sign is your Application id and another one is your Ad unit Id.



Application Id and Ad Unit Id


AndroidManifest.xml

 <meta-data  
       android:name="com.google.android.gms.ads.APPLICATION_ID"  
       android:value="@string/bannerAdAppId" />  

Then create an instance and find the id in MainActivity.java and Load your Banner Ad

MainActivity.java
 public class MainActivity extends AppCompatActivity {  
   AdView madview;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     madview = findViewById(R.id.adView);  
     AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXXXXXXXXXXXX").build();  
     madview.loadAd(adRequest);  
   }  
 }  

 Here, we load our ad and also add a test device because we run this ad through debugging. For test device id you can see logs of Ad-request inside run tab in Android Studio.


Find Test Device Id to run Banner Ad
  


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

3 comments:

  1. Great articles and great layout. Your blog post deserves all of the positive feedback it’s been getting. Phone Spy Apps

    ReplyDelete
  2. As always as widespread contractors go look at this web-site, app company
    agencies you're in inappropriate hands with those men!

    ReplyDelete