Android Admob advert in PreferenceActivity

前端 未结 7 577
暖寄归人
暖寄归人 2020-12-04 22:32

Is there a way to add an admob advert to a PreferenceActivity? How to?

7条回答
  •  臣服心动
    2020-12-04 22:45

    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:

    1. Get the parent of preference activity.

      LinearLayout root = (LinearLayout) 
      findViewById(android.R.id.list).getParent().getParent().getParent();
      
    2. Create a xml file named banner_ad.xml.

      
      
      
      
      
    3. Inflate the layout.

      LinearLayout adViewLayout = (LinearLayout) 
      LayoutInflater.from(this).inflate(R.layout.banner_ad,root,false);
      AdView adView = adViewLayout.findViewById(R.id.adview);
      
    4. Load add.

      AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice(device_id)
              .build();
      adView.loadAd(adRequest);
      
    5. Add layout view as a child of root view. In my case i used this below toolbar at index 1.

      root.addView(adViewLayout,1);
      

提交回复
热议问题