Jump to content

  • Log In with Google      Sign In   
  • Create Account


AdMob for Android without using XML layout


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Canvas   Members   -  Reputation: 229

Like
0Likes
Like

Posted 18 June 2013 - 07:22 AM

I have a massive question to ask as I am really stuck on this and it would be create to get ads on my free application, ok first off I have been following this book
 
Beginning Android games 2011 
 
 
Now this book implements a very nice and simple game framework which I use (a simpler version can be found here
 
 
Now this framework doesn't use any type of XML file what so ever, it uses a framebuffer to draw things onto the screen. now when the application is first started, this is the first method called which is in the AndroidGame.java
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        int frameBufferWidth = isPortrait ? 480: 800;
        int frameBufferHeight = isPortrait ? 800: 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);
        
        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(this);
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getInitScreen();
        
        setContentView(renderView);
        
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
    }

Where in here could i try and implement the admob banner? Also this is what a screen class looks like

    public LogoScreen(Game game)
   {
super(game);
}

@Override
public void update(float deltaTime)
{
Graphics g = game.getGraphics();
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

int len = touchEvents.size();

for(int i = 0; i < len; i ++)
{
try
{
TouchEvent event = (TouchEvent) touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_DOWN)
{
game.setScreen(new MainMenuScreen(game));
}
}
catch(IndexOutOfBoundsException io)
{

}
}

}

@Override
public void paint(float deltaTime) 
{
Graphics g = game.getGraphics();
g.drawImage(Assets.logoScreen, 0, 0);
}

@Override
public void pause() {
// TODO Auto-generated method stub

}

@Override
public void resume() {
// TODO Auto-generated method stub

}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void backButton() 
{
android.os.Process.killProcess(android.os.Process.myPid());
}
 
If I wanted to display an admob advert in the logoScreen what could i do that would work? I am really confused on how I can implement admob into my application, if any one can shine some light on this or help me that would be great smile.png
 
Thank you
 
Canvas
 
oohh also
 
Here is the code for the FastRenderView
 
 
package com.CC.framework.implementation;

//Imports
import com.CC.SphereFactorFree.R;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AndroidFastRenderView extends SurfaceView implements Runnable 
{
	//Variables
	AndroidGame game;
	Bitmap framebuffer;
	Thread renderThread = null;
	SurfaceHolder holder;
	volatile boolean running = false;

	public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) 
	{
		super(game);
		//AdView adView = (AdView)findViewById(R.id.ad);
		this.game = game;
		this.framebuffer = framebuffer;
		this.holder = getHolder();

	}

	public void resume() 
	{ 
		running = true;
		renderThread = new Thread(this);
		renderThread.start();
	}      

	public void run() 
	{
		Rect dstRect = new Rect();
		long startTime = System.nanoTime();
		while(running) 
		{  
			if(!holder.getSurface().isValid())
			{
				continue;           
			}

			float deltaTime = (System.nanoTime() - startTime) / 10000000.000f;
			startTime = System.nanoTime();

			if (deltaTime > 3.15)
			{
				deltaTime = (float) 3.15;
			}


			game.getCurrentScreen().update(deltaTime);
			game.getCurrentScreen().paint(deltaTime);

			Canvas canvas = holder.lockCanvas();
			canvas.getClipBounds(dstRect);
			canvas.drawBitmap(framebuffer, null, dstRect, null);                           
			holder.unlockCanvasAndPost(canvas);

		}
	}

	public void pause() 
	{                        
		running = false;                        
		while(true) 
		{
			try 
			{
				renderThread.join();
				break;
			} 
			catch (InterruptedException e) 
			{
				// retry
			}

		}
	}     


}


Sponsor:

#2 Rottz   Members   -  Reputation: 442

Like
0Likes
Like

Posted 18 July 2013 - 09:46 PM

I have a massive question to ask as I am really stuck on this and it would be create to get ads on my free application, ok first off I have been following this book
 
Beginning Android games 2011 
 
    http://www.apress.com/9781430230427
 
Now this book implements a very nice and simple game framework which I use (a simpler version can be found here
 
    http://www.kilobolt.com/day-5-the-android-game-framework-part-i.html
 
Now this framework doesn't use any type of XML file what so ever, it uses a framebuffer to draw things onto the screen. now when the application is first started, this is the first method called which is in the AndroidGame.java
 
 

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        int frameBufferWidth = isPortrait ? 480: 800;
        int frameBufferHeight = isPortrait ? 800: 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);
        
        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(this);
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getInitScreen();
        
        setContentView(renderView);
        
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
    }

Where in here could i try and implement the admob banner? Also this is what a screen class looks like

 
 
I don't know the inner workings of your engine, but you have to add a layout for AdMob on the setContentView part of your code.
 
Something like this on the onCreate (obviously this wont work, you have to adapt to your engine):
 

final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
 
// create the AdMob banner view
adMobView = new AdView(baseActivity, AdSize.BANNER, ADMOB_ID); 
adMobView.loadAd(new AdRequest());
 
final FrameLayout.LayoutParams bannerViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
 
renderView = new AndroidFastRenderView(this, frameBuffer);
// here comes the rest of your code to create whatever you need for the renderView, etc

// then you add both the renderView and the AdMob banner on top of it
frameLayout.addView(renderView);
frameLayout.addView(adMobView, bannerViewLayoutParams);
 
this.setContentView(frameLayout, frameLayoutLayoutParams);

Edit:

 

1) You may have to add a layoutParam for the renderView (like MATCH_PARENT, MATCH_PARENT)

2) You control the visibility of the AdMob to show whenever you want by simply using adMobView.setVisible(View.VISIBLE / View.GONE)

 

If you have any further questions I can try to elaborate it a bit more, but I think that is pretty much it.

 

Sorry, been a few yars I didnt post here, wasnt familiar with the quoting of the forum and everything else :).

 

Best,

- Pedro / RoTTer 


Edited by RoTTer, 18 July 2013 - 09:53 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS