Jump to content
Info
  • Welcome to PhCyber
  • Explore and Enjoy Browsing!
  • Lots of useful topics
  • Stay active and receive prizes
SuperVegita1

Banner Ads Android Code

Recommended Posts

 Step1 add depedencies:

 compile 'com.google.android.gms:play-services-ads:8.4.0'  

 

Step2 Add Android Manifest Permission:

<uses-permission android:name="android.permission.INTERNET" />  

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

 

Step3 MainActivity.Java:

package com.eut.banner

  

import android.support.v7.app.AppCompatActivity;  

import android.os.Bundle;  

  

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

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

  

public class MainActivity extends AppCompatActivity {  

    private AdView adView;  

    AdRequest adRequest;  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        adView = (AdView) findViewById(R.id.ad_view);  

        adRequest = new AdRequest.Builder().build();  

        adView.loadAd(adRequest);  

  

  

    }  

    @Override  

    public void onPause() {  

        if (adView != null) {  

            adView.pause();  

        }  

        super.onPause();  

    }  

  

    @Override  

    public void onResume() {  

        super.onResume();  

        if (adView != null) {  

            adView.resume();  

        }  

    }  

  

    @Override  

    public void onDestroy() {  

        if (adView != null) {  

            adView.destroy();  

        }  

        super.onDestroy();  

    }  

}  

 

 

Step4 Layout xml Add this to activity_main.xml

<TextView  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="Banner Ads Example" />  

  

    <com.google.android.gms.ads.AdView  

        android:id="@+id/ad_view"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:layout_centerHorizontal="true"  

        android:layout_alignParentBottom="true"  

        app:adSize="BANNER"  

        app:adUnitId="@string/banner_ad_unit_id"  

        /> 

 

Step5 Go to res values string.xml add this

<resources>  

    <string name="app_name">BannerAds</string>  

//replace unit ad to your original ad id

    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>  

</resources>  

 

compile and build

 


Edited by SuperVegita1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...