英文:
How to add admob ads in fragments in android studio?
问题
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 = rootview.findViewById(R.id.adView); // Changed findViewById to rootview.findViewById
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Your existing click listeners setup
mAdapter = new RecentListAdapter(this);
recentList.setAdapter(mAdapter);
return rootview;
}
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>
In your Manifest file, make sure to add the following permissions and the Google Play services metadata:
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<!-- Add this within the <application> section -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID" />
Replace YOUR_ADMOB_APP_ID
with your actual AdMob App ID.
Please note that you should replace "ca-app-pub-3940256099942544/6300978111"
with your actual Ad Unit ID in the XML layout. Also, ensure that you have the necessary dependencies and imports for AdMob set up correctly in your app.
英文:
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.
答案1
得分: 0
在你的 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) {}
});
}
在你的 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);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论