Use the permission Internet
MainActivity.java
import android.os.Bundle;
import android.app.*;
import com.google.android.gms.ads.AdRequest; //Import class AdRequest
import com.google.android.gms.ads.AdView; //import class Adview
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:text="@string/app_name" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--menambahkan elemen AdView untuk menampilkan iklan/ads-->
<com.google.android.gms.ads.AdView
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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
String.xml
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>