Good Morning all, see you again in block AYO NGOPREK IT, for this tutorial I will share a little about displaying Ads from Google AdMob on your Android App you made. Before we start the process of development, Prepare the ID and Application ID ID that we will install on android that we make. If you guys make it please read the previous article Cara Mendapatkan ID Aplikasi Iklan di AdMob.
Ok for how to display this ad in our application is quite easy, and simple, just a few minutes me for the making, to display in my application use the type of advertisement that is banner. Immediately, for the first step we open the application that you are making or so.
Next we import the Google Mobile Ads SDK in build.gradle in you.
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
After you add in all project. Then add in dependencies to call its Google Service Ads.
dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.android.gms:play-services-ads:11.8.0'
}
After that we call the SDK Ads before the Ads process.
package ...import ...
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
}
...
}
Okay then after we call the Ads SDK we create a layout to display ads ,, copy the script below here in your layout.
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<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="ID ADMOB Iklan Kalian">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
After we create the layout we create a process to display ads, Okay we just open MainActivity.class
package ...
import ...
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(this,
"ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
Okay after that then run your application on the virtual device or directly can compile it to your phone and test whether it appears ads or not, for the appearance of the ad belom nampil please wait approximately 10 minutes,
So many of my articles may be useful for all of you and can nunyul through google Admob;) hehehehe,
do not forget to read other articles.
0 komentar