Android app displaying ads or app screen, not both - java

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.

Related

How can I add an AdMob banner to a camera Activity, after startCamera() method(using the xzing library to scan QR Codes)?

I use a library called zxing from the package com.google.zxing.Result and me.dm7.barcodescanner.zxing.ZXingScannerView to scan QR codes in my new app, however I wasn't able to put a Admob banner at the bottom of the Camera Activity who scans the QR code, I don't know how to make it appear in that Camera Acvitity because it seems the camera activity doesn't have a layout XML to put the admob XML code.
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
}
}
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera
I tried this code to put the banner at the bottom of the activity camera but it doesn't work.
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.main_layout);
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder().build();
//mAdView.loadAd(adRequest)
mainLayout.addView(mAdView,rlParams);
// Load the ad:
//AdRequest adRequest = new AdRequest();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
The scanner view initializes, but it doesn´t show the banner, how can I call the camera Activity and put the banner there? your help will be appreciated. Thanks.
public void QrScanner(View view) {
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera
Instead of using setContentView(mScannerView);, you can inflate your own xml having FrameLayout, which you are already doing as setContentView(R.layout.activity_main);
So add in that activity_main.xml, add FrameLayout, add one LinearLayout container to host the scanner and place one other LinearLayout as a container for AdMob banner.
add thee views in respective LinearLayout runtime by calling mLinearLayoutContainerForScanner.addView(mScannerView); and mLinearLayoutContainerForAdMobBanner.addView(mScannerView).

How to get ADmob banner adds to show in Libgdx

I am trying to get a banner ad to show at the bottom of my game in libgdx. The game compiles and runs fine but no ads are showing. How can i check if ads are being setup properly and what can i change in my code to display ads.
public class AndroidLauncher extends AndroidApplication {
private AdView adView;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
View gameView=initializeForView(new BuckyRun(), config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
gameView.setLayoutParams(gameViewParams);
layout.addView(gameView);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("secret");
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adView.loadAd(adRequestBuilder.build());
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
topParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layout.addView(adView, topParams);
adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
setContentView(layout);
}
#Override
protected void onResume() {
super.onResume();
adView.resume();
}
#Override
protected void onPause() {
super.onPause();
adView.pause();
}
#Override
protected void onDestroy() {
super.onDestroy();
adView.destroy();
}
The Ad Unit ID will take time 12 hours to come live on google.
Upto that time test with App ID to initialize the SDK: ca-app-pub-3940256099942544~3347511713.
Refer MobileAds

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.

admob banner position / notification bar

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);

Categories