Saturday, December 3, 2011

AdMob with OpenGL ES

This is how I solved the integration between AdMob and OpenGL ES.

GLSurfaceView glView;
protected AdView adView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
glView = new GLSurfaceView(this);
        // Create AdMob LayOut
        adView = new AdView(this, AdSize.BANNER, "XXXXXXX"); // Your AdMob ID
  AdRequest request = new AdRequest();
  request.addTestDevice(AdRequest.TEST_EMULATOR);
  request.addTestDevice("D77E32324019F80A2CECEAAAAAAAAA");    // My test phone
  adView.loadAd(request);
        layout.addView(glView);
        RelativeLayout.LayoutParams adParams =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(adView, adParams);
        setContentView(layout);
}

This code make it very easy to place the ad where you want it. It's not pixel but you can place it kinda good around the phone. You change it with adParams.addRule. If you don't have a test device you can just remove request.addTestDevice.
If you want to turn on and off the add you need to add a Handler in the same class as you have create.
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case SHOW_ADS:
           {
               adView.setVisibility(View.VISIBLE);
               break;
           }
           case HIDE_ADS:
          {
               adView.setVisibility(View.GONE);
               break;
           }
        }
    }
};

public void showAds(boolean show) {
       handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}

This is the last code. Then you can just call showAds(true). I think this work very well.
Hope this is helpful for someone.

Thursday, December 1, 2011

Matches, OpenGL ES 1.1 game

This is the first screenshots from Matches. It is a very simple game. I wanted to develop a simple game for Android that use OpenGL ES so I get some feelings how it is to work with. There are alot todo but I'm on my way todo it.













This is the main screen. I think I will have some more buttons later on. But so far it's only one play button and a button to turn on and off the music.













This is the game screen.