http://nobuo-create.net/admob/
I tried to assemble the code as shown on this site and view AdMob.
It can be started, but for some reason it cannot be displayed.
The game has been completed, but AdMob shows it for about 4 days...
Please tell me what is wrong or where to fix it.Thank you for your cooperation.
Here is the code for Adsense.java
import jp.example.apuri.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class Adsense extensions Activity {
LinearLayout layout_ad;
AdView adView;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView (this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"; // UnitID for testing
adView.setAdSize (AdSize.BANNER);
layout_ad=(LinearLayout) findViewById(R.id.layout_ad);
layout_ad.addView(adView);
AdRequest adRequest=new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
@ Override
public void onPause() {
adView.pause();
super.onPause();
}
@ Override
public void onResume() {
super.onResume();
adView.resume();
}
@ Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
Below is the activity_main.xml code in layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="jp.example.apuri.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text="@string/hello_world"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#999"
android: orientation="vertical">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android: id="@+id/layout_ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
adUnitId="ca-app-pub-3940256099942544/6300978111"
adSize="BANNER"/>
</LinearLayout>
</RelativeLayout>
Here is the code for AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.example.apuri"
android:versionCode="1"
android:versionName="1.0">
<string android:name="application name">Apurimei</string>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android: icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name="jp.example.Apurimei.MainActivity"
android: label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android: name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
The layout contains the Adview
, which conflicts with the layout_ad.addView(adView);
in the Java code.
First, remove the Adview
during layout, and attach android:id="@+id/layout_ad"
to LinearLayout
.
<LinearLayout
android: id="@+id/layout_ad"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#999"
android: orientation="vertical">
</LinearLayout>
How about this?
(On the other hand, I wonder if it is possible to leave it to layout without using Java code.)
© 2024 OneMinuteCode. All rights reserved.