Reputation: 91
I have been trying to implement Native Ads from admob.
I got some sample code from Github. I tried to implement this code, and the demo code works fine. The native ads show up properly.
I then created my own Admob account and obtained an Ad Unit ID
. The previous demo code for native ad did not work with this id, and I got an error with error code 0
although my created Ad Unit ID
works fine for banner type ads.
Can someone help me out?
Upvotes: 5
Views: 5286
Reputation: 870
guys i have facing same issue and finally i have a solution for it hope this will you all..:)
Here in this image when you are trying to create an AdUnitId make sure that the width and height here will be same width and height in the XML file like this
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="360x320"
ads:adUnitId="@string/NativeAdId">
</com.google.android.gms.ads.NativeExpressAdView>
One more thing you have to keep in mind that the width and height you are going to make ad will be fit in your xml layout ,So for this just first try It in your xml and then create an Native add for this.
Just try it and feel free to tell me if you will be facing any problem after doing this. Thank you
Upvotes: 2
Reputation: 437
Recently I stucked with the same question. Then I decided to post my solution for that to admobadapter. Hope it will help you.
The basic usage could look like:
ListView lvMessages;
AdmobAdapterWrapper adapterWrapper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListViewItems();
}
/**
* Inits an adapter with items, wrapping your adapter with a {@link AdmobAdapterWrapper} and setting the listview to this wrapper
* FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
*/
private void initListViewItems() {
lvMessages = (ListView) findViewById(R.id.lvMessages);
//creating your adapter, it could be a custom adapter as well
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
adapterWrapper = new AdmobAdapterWrapper(this);
adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
//here you can use the following string to set your custom layouts for a different types of native ads
//adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
//adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);
//Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
adapterWrapper.setLimitOfAds(3);
//Sets the number of your data items between ad blocks, by default it equals to 10.
//You should set it according to the Admob's policies and rules which says not to
//display more than one ad block at the visible part of the screen,
// so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
adapterWrapper.setNoOfDataBetweenAds(10);
//It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
//Otherwise your Admob account could be banned
//String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
//adapterWrapper.setAdmobReleaseUnitId(admobUnitId);
lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView
//preparing the collection of data
final String sItem = "item #";
ArrayList<String> lst = new ArrayList<String>(100);
for(int i=1;i<=100;i++)
lst.add(sItem.concat(Integer.toString(i)));
//adding a collection of data to your adapter and rising the data set changed event
adapter.addAll(lst);
adapter.notifyDataSetChanged();
}
And the result will look like this
Upvotes: 0
Reputation: 5595
Try the following code:
XML file:
<com.google.android.gms.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentBottom="true"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxx" />
The .java class:
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
Make sure to open the commented lines:
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")
when you are in debug mode.Clicks on real Ads in debug mode may result in blocking of your Admob account.You will get the testDevice id in your logcat,replace that with that of mine.
Define the following in your manifest.xml
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
and also the permission:
<uses-permission android:name="android.permission.INTERNET" />
and you are ready to go.
**Note:**If ads are not showing up then wait atleast for 30 mins.
Upvotes: 0
Reputation: 2596
when i first implemented admob from my newly created admob account, it took my account 2 days to finally get the ads, so dont worry, just wait...also if your account is not newly created, then see if you added this permission or not
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Upvotes: -1