I would love to add admob ads to fragments in my app. I have already added the ads in the XML files. I am having trouble in the java part, however. Could you please share the java code with rootview? I have checked other articles but I am confused.
HomeFragment.java
private AdView mAdView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, rootview);
mFragmentPositionMap = CommonCodeUtils.getInstance().fillNavigationItemsMap(true);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
imagesToPdf.setOnClickListener(this);
qrbarcodeToPdf.setOnClickListener(this);
textToPdf.setOnClickListener(this);
viewFiles.setOnClickListener(this);
viewHistory.setOnClickListener(this);
splitPdf.setOnClickListener(this);
mergePdf.setOnClickListener(this);
compressPdf.setOnClickListener(this);
removePages.setOnClickListener(this);
rearrangePages.setOnClickListener(this);
extractImages.setOnClickListener(this);
mPdfToImages.setOnClickListener(this);
addPassword.setOnClickListener(this);
removePassword.setOnClickListener(this);
rotatePdf.setOnClickListener(this);
addWatermark.setOnClickListener(this);
addImages.setOnClickListener(this);
removeDuplicatePages.setOnClickListener(this);
invertPdf.setOnClickListener(this);
zipToPdf.setOnClickListener(this);
excelToPdf.setOnClickListener(this);
extractText.setOnClickListener(this);
addText.setOnClickListener(this);
mAdapter = new RecentListAdapter(this);
recentList.setAdapter(mAdapter);
return rootview;
}
Added This To fragment_home.xml
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
However, it return an error with the findViewById part. And I would also like to know what should be added to the Manifest file.
You have to run this on your Activity
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
}
and this on your Fragment
:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.