Android AdMob overlapping GLSurfaceView - java

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

Related

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

How to have dynamically a fixed header with scrollable content in Android?

I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment

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....

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

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