Создайте новый класс AdPreference.java и включите этот код:
public class AdPreference extends Preference {
public AdPreference(Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}
@Override
protected View onCreateView(ViewGroup parent) {
// this will create the linear layout defined in ads_layout.xml
View view = super.onCreateView(parent);
// the context is a PreferenceActivity
Activity activity = (Activity)getContext();
// Create the adView
AdView adView = new AdView(activity, AdSize.BANNER, "YOUR_ADMOB_ID_HERE");
((LinearLayout)view).addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
adView.loadAd(request);
return view;
}
}
В папке res / layout создайте новый макет xml под названием ad_layout.xml, который должен быть точным Затем включите этот код:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Затем в ваших «настройках» xml добавьте это сразу после строки xmlns:
<com.yourpackagename.AdPreference android:layout="@layout/ad_layout"/>
В вашем AndroidManifest.xml добавьте это до
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="false" />
Также добавьте это в свой манифест:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
И это все. Это будет отлично работать.