I have a problem with AdSence. I have tried to resolve it the last couple of days but without any good results. I used developers guide but it still empty on the place of banner.
What am I doing in an application. First of all I have added a TableRow object and added AdView to it:
banner = new TableRow(engine);
banner.setGravity(Gravity.TOP & Gravity.RIGHT);
adView = new AdView(engine, AdSize.BANNER, "a14def8xxxxxxxx");
banner.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
Afterwards I just added this "banner" object to the other view. At the end - there is no output there. In case I changed AdView to TextView, just for the proof of concept, it works fine.
The output log:
06-13 18:04:38.476: INFO/Ads(576): Received ad url: <"url": "http://r.admob.com:80/ad_source.php?... >
06-13 18:04:40.406: INFO/Ads(576): onReceiveAd()
The only strange thing for me in log is:
06-13 18:04:40.336: WARN/webcore(576): Can't get the viewWidth after the first layout
I haven't found what is it means and is it because of AdSence.
Update
I have the class:
public class QuestEngine extends Activity {
Then I am trying to produce new AdView in method:
public IntroView(QuestEngine engine) {
That is why in "new AdView" I am using engine object.
Change the new AdView declaration to:
adView = new AdView(this, AdSize.BANNER, "a14def820df0417");
The engine object does not contain the context for its dimensions. The dimensions of the table row are in the Activity context "this"
Well if you are setting your adView via xml/layout file then do as directed below:-
In your activity.xml file insert the following tag where you want your AdView to be shown:-
<com.google.ads.doubleclick.DfpAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="/XXXXX/XXXXX"
ads:adSize="BANNER"
/>
And in your Activity.java file write this code in your onCreate() Method of the Activity Life Cycle:-
// Look up the DfpAdView as a resource and load a request.
adView = (DfpAdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
Now if you want to programmatic set the AdView in your Activity then follow the steps as directed below:-
Write the following code in your onCreate() method,
I am demonstrating for Fragment, so write the following code in your onActivityCreated()Method.
// Create an ad view as a second header for now to meet deadlines
DfpAdView mAdView=null; //you can also define this as a Global Variable.
if (mAdView == null) {
mAdView = new DfpAdView(getActivity(), AdSize.BANNER, "/XXXX/XXXX"); //give your adId incase of XXXX
iaddHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); //This code is used to set the height of the addHeight as 50 pixels, which could be used in layout params.
RelativeLayout fl =new RelativeLayout(this.getActivity());
RelativeLayout.LayoutParams flParams =new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
flParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
FrameLayout.LayoutParams para =new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
para.setMargins(0, 0, 0,iaddHeight);
getListView().setLayoutParams(para);
this.getActivity().addContentView(fl, flParams);
fl.addView(mAdView,flParams);
}
mAdView.loadAd(new AdRequest());
Hope This can help Any Future users...
Related
I implemented admob express ads in my application. I use them in listview and are implemented like this:
NativeExpressAdView adView = new NativeExpressAdView(activityContext);
AdSize adSize = new AdSize(AdSize.FULL_WIDTH, 150);
adView.setAdSize(adSize);
adView.setAdUnitId(getString(R.string.ad_unit_id));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
adSize.getHeightInPixels(activityContext));
adView.setLayoutParams(layoutParams);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
adQueue.add(adView);
In the listview adapter they are then shown just by returning the adview.
It works normaly but when scrolling for the first time the ad goes out of the view bounds and then in drops back in and works normally after that.
I am not really sure how to explain it so I made a gif:
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.
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);
I have this:
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
super.root.requestLayout();
}
When the ad loads, it covers up the bottom of my application. If I switch the orientation, everything re-renders correctly. This must mean that the WebView is the right size, but Sencha Touch isn't respecting the size change, right?
How do I fix this?
When adding an AdView programmatically to would normally add it into a ViewGroup that you have specially created and sized for that purpose. I have no idea what Sencha Touch is but if super.root is the ViewGroup for the whole Activity then your display will depend entirely upon what type of Layout what specified for the root View.
Suggest you create a AdViewParent in your layout and add the AdView into that.
The trick is to put adView not onto the super.root layout but onto the layout of WebView (appView of DridGap). Here is the code for horizontal align banner in bottom of the screen
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
//
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
AdSize.BANNER.getHeightInPixels(this));
rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);
//
appView.addView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
request adRequest = new AdRequest();
adView.loadAd(request);
}
Using this method you have to avoid overlapping of content by admob banner. You can do it in app.scss, someting like
#bottomitemid{
padding-bottom: 50px;
}
I dont know how to get this to working within my game...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
int newID = glView.getId();
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "a14e3ef0948eb58");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(newID);
// Add the adView to it
layout.addView(adView);
Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
This gives me a Null Pointer Exception and I cant figure out what else to do with this. Im just needing to know how to get this to work with the way I have programed this. I dont use any XML.
Also the "newID" value become -1 so thats where the error is coming from
Any help will be appreciated
Thanks
Try this,
You don't use XML so that means your view isn't created static(in xml) but dynamic(in java).
Dynamically created views don't have a id already associated with them yet, so you must set one explicitly.
view.setId(1)**//choose a non negative integer
Check this out: Get ID for views that are added dynamically