Java – how to use setContentView(new Activity(this)); Programmatically create admob banners without using XML?

how to use setContentView(new Activity(this)); Programmatically create admob banners without using XML?… here is a solution to the problem.

how to use setContentView(new Activity(this)); Programmatically create admob banners without using XML?

This is onCreate(); My 2D hentai runs the game’s Game.java Activity method. I’m new to Java and Android. I finished the game and was ready to publish to the Playstore. But I can’t add admob banners to my game. All the admob tutorials I searched for ended in .xml layout, but my setContentView(); Execute Java classes. Please help me solve the problem.

public class Game extends Activity {

MediaPlayer bgsound;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(new GamePanel(this));

sound
    bgsound = MediaPlayer.create(Game.this, R.raw.bgsound);
    bgsound.setLooping(true);
    bgsound.start();

}

Solution

Programmatically create layout containers:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

 Create a banner ad
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("myAdUnitId");

 Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

 Optionally populate the ad request builder.
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

 Add the AdView to the view hierarchy.
    layout.addView(mAdView);

 Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());

setContentView(layout);
}

If you see this error code: W/Ads: Failed to load ad: 0

:

Check in build.gradle that you’re running the latest version of Google Play Services:

dependencies {
    compile 'com.google.android.gms:play-services-ads:7.5.0'
    }

Check that you have the correct permissions in the Manifest.xml:

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Check that your <meta-data> is correctly marked in Manifest.xml:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services.-->
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <activity...

Check that your AdActivity settings are correct:

<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:theme="@android:style/Theme.Translucent" />

Check that your ad unit ID is correct. For testing purposes, you can use this unit ID in res/strings.xml or elsewhere:

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

The Complete Guide here

Related Problems and Solutions