admob banner position / notification bar - java

I'm working on putting ads in my android game project with libgdx. I saw this code on the web and it worked for me. The problem is it shows the notification bar on the top and the position of the ad is on bottom left part of the screen which obscures the view of the character. The game is on landscape position and I want to put the ad on bottom center part. How can I take off the notification bar and put the ad on a correct position. I'm just new in java and I don't understand this code. Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
//setContentView(R.layout.main);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams gameViewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//gameViewParams.bottomMargin = 150;
requestWindowFeature( Window.FEATURE_NO_TITLE );
View gameView = initializeForView(new ZBGame(), cfg);
layout.addView(gameView, gameViewParams);
AdView adView = new AdView(this);
adView.setAdUnitId("YOUR ADMOB AD ID");
//adView.setAdUnitId("app-id");
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
setContentView(layout);

I haven't done this yet, but I would assume you would add another rule to adParams to center the view.
For example:
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

Related

Android AdMob overlapping GLSurfaceView

I want simply to display a AdMob banner on the bottom side in portrait orientation under a GLSurfaceView.
But it does not work.
Why the AdMob banner is overlapping the GLSurfaceView ?
And why I can only see the AdMob banner if I pause and resume the app after creation once?
I don't like to use a XML layout only for this 2 views so I search for a simple solution completely in code.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adView = new AdView(this);
adView.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("356613124234016")
.build();
adView.loadAd(request);
gameView = new GameView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView);
layout.addView(adView, lp);
setContentView(layout);
}
OK I found a solution,
everyone who needs a quick and simple solution:
Here how I solved it:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adView = new AdView(this);
adView.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("356613124234016")
.build();
adView.loadAd(request);
gameView = new GameView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams gameP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
gameP.gravity = Gravity.TOP;
LinearLayout.LayoutParams adP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.0f);
adP.gravity = Gravity.BOTTOM;
layout.addView(gameView, gameP);
layout.addView(adView, adP);
setContentView(layout);
}

Admobs Banner not Displaying Libgdx

Hi all im trying to implement Admobs into my game but ive been stuck getting the banners to show for the last 2 days. My code is as follows.
public class MainActivity extends AndroidApplication {
private String adId = "ca-app-pub-3083731858905600/3602333561";
private String deviceId = "D117452E0EC02313A049B184D977046B";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = 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);
View gameView = initializeForView(new FMGame(), cfg);
AdView AdView = new AdView(this);
AdView.setAdSize(AdSize.BANNER);
AdView.setAdUnitId(adId);
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice(deviceId);
AdView.loadAd(adRequest.build());
layout.addView(gameView);
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);
initialize(new FMGame(), cfg);
}
}
My admob account says that my game is sending requests but the banner just won't show.
My logcat says the Ad is not visible since I have less than 10 reputation
Thanks
p.s ive been trawling this site for days and tried many different solutions please help....

libGDX: Trying to implement AdMob but can't get it to work

I've been busy going through a bunch of documentation and tutorials to figure out how to implement AdMob into my game, but no luck so far.
The LibGDX wiki documentation is a bit outdated and some of the code is simply not working -> link
f.e. initializeForView doesn't take in boolean values anymore , or the adView method can't be used like this anymore:
View gameView = initializeForView(new HelloWorld(this), false);
.... or
// Create and setup the AdMob view
       
adView = new AdView(this, AdSize.BANNER, "xxxxxxxx"); // Put in your secret key here
adView.loadAd(new AdRequest());
 
I also worked through the Google AdMob documentation enter link description here, but it isn't very good aplicable to a LibGDX project.
I even tried just getting it running with AndroidStudio without using libgdx, but even that I didn't get to work accordingly.
I tried to work around it, but after a full days of tryingI still haven't been able to get any test ads on my android device when debugging on my device & any help would be more than welcome.
Can someone help me with this?
Does anyone know some more up to date info on how to set up AdMob in your libgdx project?
Here's what my AndroidManifest.xml looks like: [url=http://www.java-gaming.org/index.php?action=pastebin&hex=f9d35261d1111[/url]
And here's a version of my AndroidLauncher.java that I was hoping to show me some adds but it isn't:[url=http://www.java-gaming.org/?action=pastebin&id=1113[/url]
Here's the updated guide on setting up both admob banner and interstitial ads in a libgdx game using the google play services.
A simpler way to keep the banner shown is to set its background.
adMobView.setBackgroundColor(Color.BLACK);
You can modify your code as provided in the docs as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();
layout.addView(admobView);
View gameView = createGameView(cfg);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
}
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new AdTutorial(), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) adView.resume();
}
#Override
public void onPause() {
if (adView != null) adView.pause();
super.onPause();
}
#Override
public void onDestroy() {
if (adView != null) adView.destroy();
super.onDestroy();
}
You should check/post your logcat for any Admob related logs.
Anyway I've came across something like your problem some time ago and solved it by making the adView background transparent:
adView.setBackgroundColor(Color.TRANSPARENT);
Don't know the reason why it worked.

Adding layout views from an xml file to the content view

As a bit of background on this - I created a layout for a user interface that sat ontop of a GLSurfaceview. As I progressed I wanted to add adverts from admob on top of the GLSurfaceview. After some reading it seemed like the only way to do this was to create a new Layout view then add each dynamically created view to this. Then set the new Layout view as the content view. This means that all the views in my UI are dynamically created in java code.
My question is:
Is there any way you can use a view from an xml layout that is not set as the content view and somehow add it to the content view just like you would when dynamically creating a view?
I did try this but got null pointer exceptions from which ever view I tried to findViewById.
Any sort of write up explaining this would be helpful
EDIT added code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("******************************").build();
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
final TextView Score = new TextView(this);
Score.setText(" 0");
RelativeLayout.LayoutParams scoreParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Score.setLayoutParams(scoreParams);
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/BOYCOTT_.ttf");
Score.setTextSize(getResources().getDimension(R.dimen.textsize));
Score.setTypeface(tf);
Score.setTextColor(0xFFFFFFFF);
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
mGLSurfaceView = new RBGLSurfaceView(this); new GLSurfaceView(this);
mGLSurfaceView.setEGLContextClientVersion(2);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mGoogleApiClient = getApiClient();
mGLSurfaceView.setRenderer(new RBRenderer(this, Score, mGoogleApiClient),displayMetrics);
layout.addView(mGLSurfaceView);
layout.addView(Score, scoreParams);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
return;
}
}
Ok so the above code is creating an adview, textview and surface view and adding them to the layout which is then getting set as the contentview - works great.
But is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Doing this doesnt work
final TextView test= (TextView) findViewById(R.id.test);
// set properties here etc
layout.adView(test);
because 'test' is null because the layout in which the view 'test' belongs to is not the set as the content view.
is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Yes, it's totally possible! Try this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View textView = inflater.inflate(R.layout.text_view, null);
layout.addView(textView);
Hope this helps!

Android app displaying ads or app screen, not both

I am having trouble getting both my ads and my application to run at the same time. I have tried for two days to find a fix and nothing has worked. Please help.
This code displays my google play services Admob ads. - The ad displays but i cannot see my game screen, it is a black background.
public class MainActivity extends AndroidApplication {
private static final String TEST_DEVICE_ID = "8FB8CE7A6B82BA648ED21C697F4076";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new ZBGame(), cfg);
setContentView(R.layout.main);
// The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(TEST_DEVICE_ID)
.build();
adView.loadAd(adRequest);
}
This code (commented out ad code) runs my game successfully.
What is causing the two to not work together?
public class MainActivity extends AndroidApplication {
private static final String TEST_DEVICE_ID = "8FB8CE7A6B82BA648ED21C697F4076";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new ZBGame(), cfg);
// setContentView(R.layout.main);
// // The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
// AdView adView = (AdView) this.findViewById(R.id.adView);
// AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice(TEST_DEVICE_ID)
// .build();
// adView.loadAd(adRequest);
}
Hi William. I am just starting in Java and the tutorials I used were on http://www.kilobolt.com/zombie-bird-tutorial-flappy-bird-remake.html.
I did get the app and ads working as well. I realized after testing a while that the ad layout was replacing what i already had there (the game).
Utilizing the below code to get it working, which to my understanding. Creates a content view and adds both to it.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
//setContentView(R.layout.main);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams gameViewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//gameViewParams.bottomMargin = 150;
requestWindowFeature( Window.FEATURE_NO_TITLE );
View gameView = initializeForView(new ZBGame(), cfg);
layout.addView(gameView, gameViewParams);
AdView adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4445204025228818/6510368088");
//adView.setAdUnitId("app-id");
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
setContentView(layout);
OK, you haven't shown the code for #initialize, nor the XML config for R.layout.main but I suspect I can guess what is going on.
Since in the 2nd scenario your game works with #onCreate not calling #setContentView then #initialize must be calling #setContentView. THis means that in the first scenario you are calling #setContentView twice. You should only ever call this once as this defines the layout for your Activity.
Your layout needs to provide elements for your game and for the AdView. I suspect you do not yet have that. That will also need to be fixed.
Can you please post the link that you copied the game integration from. This is the 6th SO post I have seen using this code fragment and I would like to nip the source in the bud.

Categories