Android AdView orientation change height is 0 - android

I'm trying to reload manually an AdView when orientation changes. I'm able to issue the request and get an add back. The problem is that when I try to remove the old view and add a new one, I get this warning:
W/Ads: Not enough space to show ad. Needs 592x32 dp, but only has 640x0 dp.
And the view is not shown. This is my onConfigurationChanged method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
ViewGroup.LayoutParams adViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParent.removeView(adView);
adView.destroy();
AdView newAdView = new AdView(this);
newAdView.setAdSize(AdSize.SMART_BANNER);
newAdView.setAdUnitId(getString(R.string.admob_ad_unit_id_main));
newAdView.setLayoutParams(adViewParams);
newAdView.setId(R.id.adView);
adView = newAdView;
adViewParent.addView(adView);
loadAd();
}
I've read other similar answers like this one android admob resize on landscape but they didn't work for me and I really don't know what I'm doing wrong.
Thank you for the help.

I hope it helps you:
ViewGroup.LayoutParams adViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dpToPx(32));
And hide adViewParent.setVisibility(GONE) if loading of ad is failed

Related

Admob ads appear only after first refresh

i added banner ads from admob to my game and the banner add appears only after the first refresh (30 seconds) . And for the first 30 seconds it's there and if you click the bottom of the page where the addvertisment should be it takes you to the advertising page but the ad is not visible.
You can see for yourself when you start the game and click bottom of the screen.
Game #1
And my other game (if you start the intent from the menu in this game ( like facebook) and then return to the menu, ads appear instantly):
Game #2
Anyone had this happen ? What can i do to fix this ?
How I initialize my ads :
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
opener = new AndroidGalleryOpener(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 Zaidimas(opener), config);
AdView adView = new AdView(this);
adView.setAdUnitId("xxxxx my secret number xxxxxxxx");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.build());
layout.addView(gameView);
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);
layout.addView(adView, adParams);
setContentView(layout);
//initialize(new Zaidimas(opener), config);
}
It is hard to say what happens in your case, but I have a suspect that your libgdx screen with the admob banner might show up before the ad finished loading. You can try adding an AdListener that forces the ad-view to draw itself once the ad finished loading. Something like:
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
System.out.println("ad banner finished loading!");
adView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);
}
});
The code looks a bit odd, but changing visibility should really make sure that your adView gets drawn again ;-)
If you're using a Relative Layout be sure to include the position of the view relative to other layout objects. I was having the same issue until I placed android:layout_below="#+id/adView" on the object directly below my adView object. You could probably just as well place android:layout_above="#+id/someView" on the adView object itself.

How to display banner ad in eclipse using libgdx

I am really new to programming. I have followed the tutorial in Libgdx and google,
but i cannot put a banner ad using admob in my application.
Can someone help me to teach what i should do to put admob banner ad in my application ?
Thank you,
You need to create a View to contain both the adMob View and the Libgdx View, then setContentView() to this layout to which you must add both Views. Is this the tutorial to which you refer? It is very helpful.
Inside your android game-android project open the Activity and create a relative layout that is fullscreen and has no title bar as-
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);
Now below this change this line-
initialize(new Mygame(), cfg);
to
View gameView=initializeForView(new MyGame() , cfg);
This method initializeForView(new MyGame() , cfg); returns a view which is saved as a instance of view that will be added to the relative layout. Now create adview also to add to relative layout as-
Adview adView=new AdView(this);// create new adview instance
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);// set the adsize
adView.setAdUnitId("XXXXXXXXX");// your adunit id
//Create layout params for adview
RelativeLayout.LayoutParams adParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_RIGHT);//set position
//Add both gameview and adview to the relative layout-
layout.addView(gameView);
layout.addView(adView, adParams);
//Now just request ad by creating new instance of AdRequest
AdRequest request=new AdRequest.Builder().build();//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) to add test device
adView.loadAd(request);
// At last setcontent view to the Relative Layout that has both gameView and adView.
setContentView(layout);
You need to override onPause , onResume and onDestroy also to handle pause , resume or destorying of ad.
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onPause() {
super.onPause();
if(adView!=null){
adView.pause();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(adView!=null){
adView.destroy();
}
}
Upto here adView is added to your game, but if you want to handle on when to show adView.You may want to hide adView when game is running and show when game is over, which can be achieved by creating handler.

Best proactive when implementing adView

I am making a game and I would like the ad to be visible during the menu but not when the game is actually being played.
If I simple set the adView Visibility to INVISIBLE when I don't want it shown then the ad will not refresh and thus i don't think will register with admob.
Is there an accepted way of implementing ads like this?
edit* Code in main activity to add the adView
private void initialiseAd()
{
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("");
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adView.setLayoutParams(lp);
//load ads
adView.loadAd(adRequest);
//add the adView to the window
mainLayout.addView(adView);
}
The way you're facing it is correct, you can easily hide the AdView simply wrapping it within a FrameLayout (which you already seem to be doing) and calling the setVisibility() method over it.
If you're using Activity, this would be the way:
FrameLayout containingFrame = (FrameLayout) findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);
If you're using Fragment:
FrameLayout containingFrame = (FrameLayout) getActivity().findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);

replace layout on orientation change

My app has a webview and some buttons inside a LinerLayout.
the problem is, I want the buttons to be on bottom in portrait mode and on left in landscape mode while the webview maintains it's state.
Two different layout doesn't work as it force recreation of the activity that refresh the webview. for now I use android:configChanges="orientation" in activity tag so webview doesn't get refreshed on orientation change.
Is there anyway to replace the layout of buttons on the change of screen mode?
portrait mode
landscape mode
I tested fragments, but dealing with fragment makes things much more complex and the fragment itself needs saving and restoring which may not work in a webview which has javascript state, So I searched more and find a nice article somewhere and with some modification I came to a solution which I suggest:
First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" so the app handles the config changes instead of android.
Make two different layout for landscape and portrait. In both layouts instead of webview place a FrameLayout which acts as a placeholder for the webview.
Define initUI method like this and put everything related to UI initialization in this method:
public void initui()
{
setContentView(R.layout.main);
if (wv == null) wv = new WebView(this);
((LinearLayout)findViewById(R.id.webviewPlace)).addView(wv);
findViewById(R.id.home).setOnClickListener(this);
}
If the webview doesn't exist yet it will be created and after setContentView(R.layout.main) it will be added to the layout. Any other UI customization you need came after this.
and in onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
((LinearLayout)findViewById(R.id.webviewPlace)).removeAllViews();
super.onConfigurationChanged(newConfig);
initUI();
}
In onConfigChange the webview is removed from old placeholder and initui will be called which will add it back to the new layout.
In oncreate() call initui() so the ui will be initialized for the first time.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initUI()
}
I wish it would be helpful for someone.
If you are using android:configChanges="orientation|screenSize"
In manifest,it ignores the XML in "layout-land". If you create a different XML for landscape don't use the android:configChanges="orientation|screenSize" tag for that activity in manifest.
put that at layout-land for the layouts you want as landscape.
The idea is, you shouldn't really use configChange="orientation" because it has its downsides. You can find a detailed post here . You should manually handle your state if you want to change your layout. Of course you can programmaiclly do this but if you want to use xml to do this. You can maintain you webView with fragments.
Try to use this code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_splash);
}
Make your view a relative layout. When the orientation changes just adjust the layout params for each view in code.
Have your buttons be contained in a linear layout
As in..
Portrait
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLinearLayout.setLayoutParams(lp);
LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ABOVE, R.id.buttons);
webView.setLayoutParams(webParams);
Landscape
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
buttonLinearLayout.setOrientation(LinearLayout.VERTICAL);
buttonLinearLayout.setLayoutParams(lp);
LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.TO_RIGHT_OF, R.id.buttons);
webView.setLayoutParams(webParams);
Make sure the LayoutParams you are using are the RelativeLayout params (always use the Layoutparams of the view's parent)
Add in the manifest
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
android:label="#string/app_name">
and
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Resources.getSystem().getDisplayMetrics().widthPixels>
Resources.getSystem().getDisplayMetrics().heightPixels)
setContentView(R.layout.activity_layout1);
else setContentView(R.layout.activity_layout2);
...
}
and
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_layout1);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_layout2);
}
}
Documents here:
https://developer.android.com/guide/topics/resources/runtime-changes.html
You can add values-(configuration that you require - eg. land,portrait) folder under resources and mention the layout you need for this configuration.. It's as simple as that.
Please checkout the below link for further information-
http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters

Remove Ads or Hide AdView

I'm trying to hide the Ads from AdMob after 2 minutes, what I've done is, after 2 minutes, I'll stop requesting fresh ad and to hide AdView, have written
if (adView.getVisibility() == AdView.VISIBLE)
adView.setVisibility(AdView.GONE);
what's happening is Ad becomes invisible but still occupies the space, not releasing it. and I want to release that space.
Any Ideas, Thanks in Advance.
if (adView.getVisibility() == AdView.VISIBLE)
adView.setVisibility(View.GONE);
I would use a flag and wrap it like this. Then set it to true/false whenever.
if (Settings.ADMOB_ACTIVE) {
if (Settings.D) {
Log.d(TAG, "Adding Admob");
}
final AdView adView = new AdView(this, AdSize.BANNER, Settings.PUBLISHERID);
final LinearLayout layout = (LinearLayout)findViewById(R.id.adLayout);
if (adView != null && layout != null) {
layout.addView(adView);
final AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
}
I handled this function inside a thread, that's why I got exception and it didn't releases the space, instead now I used Thread Handler to handle the function and now its working fine and now its not occupying any space.

Categories

Resources