Admob ad is not drawn, but is there? (LibGDX Admob 6.4.1)

I am using LibGDX 0.9.9 and Admob 6.4.1. I set up banner ads to appear in the upper right corner of my application. However, when I download my application, I do not see the banner. Although, when I click where the banner should be, it acts as normal and starts to appear. This happens both in test mode and in real mode. Here is my code:

    public class MainActivity extends AndroidApplication {
    private AdView adView;

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

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;
        cfg.useAccelerometer = false;
        cfg.useCompass = false;

        RelativeLayout layout = new RelativeLayout(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        // Create the libgdx View
        View gameView = initializeForView(new Game(), false);

        // Create and setup the AdMob view
        adView = new AdView(this);
        adView.setAdUnitId("SECRET ID");
        adView.setAdSize(AdSize.BANNER);

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)       // Emulator
        .addTestDevice("MY DEVICE ID") // My  Nexus test
        .build();

        adView.loadAd(adRequest);

        layout.addView(gameView);

        // Add the AdMob view
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        layout.addView(adView, adParams);

        setContentView(layout);
    }

    public void onDestroy() { 
        if (adView != null) { 
            adView.destroy();
        }

        super.onDestroy();
    }

and Android manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.keep.hopping"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />

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

    <application
        android:icon="@drawable/jumper"
        android:label="Keep Hopping" >
        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
        <activity
            android:name="com.keep.hopping.MainActivity"
            android:label="Keep Hopping"
            android:screenOrientation="landscape"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <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>

</manifest>

Does anyone have any ideas as to why this strange behavior is occurring?

+4
source share
4 answers

In my case, the problem was solved simply by setting the background of the advertisement:

...
layout.addView(adView, adParams);
adView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
...

.

+2

AdMob6.4.1 libgdx, :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:paddingTop="1dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</RelativeLayout>

main.xml

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        setContentView(R.layout.main);
        MainActivity.mContext = getApplicationContext();

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;
        cfg.useAccelerometer = false;
        cfg.useCompass = false;

        RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);


        mBlockPuzzleInstance = new BlockPuzzle(this);
        initialize(mBlockPuzzleInstance, false);
        View gameView = initializeForView(mBlockPuzzleInstance, true);

        adView = new AdView(this);
        adView.setAdUnitId("a152ffba4abde11");
        adView.setAdSize(AdSize.SMART_BANNER);

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

        layout.addView(gameView);

        // Add the AdMob view
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        layout.addView(adView, adParams);

        setContentView(layout);
 }

.

!

+1

, :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:paddingTop="1dp"
    ... >
</RelativeLayout>

I really implemented it programmatically as follows:

layout.setPadding(0, 1, 0, 0);

Hope this helps!

+1
source

Your problem may be that your first ad is hidden. If you wait 60 seconds and then your ad appears, you can fix this by adding AdView to the layout only after loading the first ad using AdListener:

        // Create and setup the AdMob view
        AdRequest.Builder ad_builder = new AdRequest.Builder();
        ad_builder.addTestDevice("XXXXXXXXXXXXX"); // my HTC phone
        m_AdRequest = ad_builder.build();

        m_AdView = new AdView(this);
        m_AdView.setAdSize(AdSize.SMART_BANNER);
        m_AdView.setAdUnitId("ZZZZZZZZZZZZ");
        m_AdView.setVisibility(View.GONE);
        m_AdView.setAdListener(new AdListener() {
            private boolean _first = true;

            @Override
            public void onAdLoaded() {
                if (_first) {
                    _first = false;

                    // Add the AdMob view when the first ad gets loaded, this makes sure the first ad gets displayed
                    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // places the ad at the bottom overlapping your game

                    layout.addView(m_AdView, adParams);
                }
            }
        });

Hope this solves your headache!

0
source

Source: https://habr.com/ru/post/1525933/


All Articles