Admob ads in Android Studio

Admob ads in Android studio

Hello friends if you are looking for Admob ads in Android Studio? You are in right place. In this post I will tell you how to set admob ads in android studio. In this post i will tell you procedure of Admob Banner, Interstitial  ads in android studio. Banner ads are rectangular ads and small size ads like attach image.

Important Note:-  I used test ids when u publish your app do not forget to add your original ids. Thanks

AdMob Banner Ads

So start how to add

Admob Banner ads in Android Studio

First add dependency in build.gradle add these line and click sync this project.

implementation 'com.google.android.gms:play-services-ads:20.1.0'

then open AndroidManigest.xml  and add these lines

 <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713" />
</application>

then go to activity_main.xml and add these line

<FrameLayout
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ad_Container_View"/>AdMob Banner Ads

Admob Interstitial Ads in Android Studio

Then go to MainActivity.xml and add blackcolor dark lines add in your project.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.FrameLayout;

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

public class MainActivity extends AppCompatActivity {

    AdView adView;
    FrameLayout adContainerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adView = new AdView(this);
        AdSize adSize = getAdSize();
        adView.setAdSize(adSize);

        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

        adContainerView = (FrameLayout) findViewById(R.id.ad_Container_View);
        adContainerView.addView(adView);

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

    }
    private AdSize getAdSize() {
        //Determine the screen width to use for the ad width.
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;

        //you can also pass your selected width here in dp
        int adWidth = (int) (widthPixels / density);

        //return the optimal size depends on your orientation (landscape or portrait)
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    }

}

Note:- If you like my post please share with friends and Facebook etc and hire me for your android projects.

We are a professional team of Android, Ios App Development, Graphic Design, Digital Marketing, After affects, Maya, 3ds max, Blender, Autocad, Sletchup pro, Professional voice over and Translation service you need any work visit my website https://www.jkssoftsolutions.com and contact me in WhatsApp +919103540799

4 thoughts on “Admob ads in Android Studio”

  1. Admob ads in Android studio tutorial is very good. Pls also add interstitial ads in Android studio. And also add startapp and Facebook ads in Android studio.

Leave a Comment

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

GET A FREE QUOTE

Open chat
1
I need help in my project