Is there a way to add an admob advert to a PreferenceActivity? How to?
I implemented the proposed approach, but faced problem regarding placing the banner at the top and with padding issues. So, i used a different approach and interested to share with you. I was working on a preference activity, although it has been deprecated but i had to work on it for some reason instead of changing to androidx preference library.
Here are the following steps:
Get the parent of preference activity.
LinearLayout root = (LinearLayout)
findViewById(android.R.id.list).getParent().getParent().getParent();
Create a xml file named banner_ad.xml.
Inflate the layout.
LinearLayout adViewLayout = (LinearLayout)
LayoutInflater.from(this).inflate(R.layout.banner_ad,root,false);
AdView adView = adViewLayout.findViewById(R.id.adview);
Load add.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(device_id)
.build();
adView.loadAd(adRequest);
Add layout view as a child of root view. In my case i used this below toolbar at index 1.
root.addView(adViewLayout,1);