[SDL] SDL2 on Android: android_app struct pointer

glezmen bokorn at tvn.hu
Sat Oct 12 01:57:57 PDT 2013

what I have so far:

C++ code, in my MainMenu.cpp:

 #ifdef      __ANDROID__
 #include    "jni.h"
 #include    "android_native_app_glue.h"
 void showAds()
     JNIEnv *env = static_cast<JNIEnv*>(SDL_AndroidGetJNIEnv());
     jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
     jclass cls = env->GetObjectClass(activity);
     jclass localcls = reinterpret_cast<jclass>(env->NewGlobalRef(cls));
     jmethodID showAds = env->GetStaticMethodID(localcls, "showAdPopup", "()V");
     env->CallStaticVoidMethod(localcls, showAds);

in my Java activity class:


    // Our popup window, you will call it from your C/C++ code later
    public static void showAdPopup()
        //  return;
        if(adView!=null)  {  
            _activity.runOnUiThread(new Runnable()  {  
                public void run()  {  
                    adsinited = true;
                    // Out popup window
                    popUp = new PopupWindow(_activity);
                    // This is the minimum size for AdMob, we need to set this in case our target device run at 320x480 resolution (Otherwise no ad will be shown, see the padding kill below)
                    popUp.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    layout = new LinearLayout(_activity);
                    mainLayout = new LinearLayout(_activity);
                    // The layout system for the PopupWindow will kill some pixels due to margins/paddings etc
 (No way to remove it), so padd it to adjust
                    layout.setPadding(-5, -5, -5, -5);
                    MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    params.setMargins(0, 0, 0, 0);
                    layout.addView(adView, params);
                    _activity.setContentView(mainLayout, params);
                    AdRequest adRequest = new AdRequest();
                    // Enable this if your are testing AdMob, otherwise you'll risk to be banned!
                    // Show our popup window
                    popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 0, 0);

Now i tried to call the showAdPopup() right before starting my Main Menu loop, and the only thing I get is a black screen with a small gray rectangle at the bottom

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20131012/b81c8a2e/attachment-0009.htm>

More information about the SDL mailing list