Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import androidx.lifecycle.ViewModelProvider;

import com.d4rk.androidtutorials.java.databinding.FragmentHomeBinding;
import com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;

Expand Down Expand Up @@ -54,11 +53,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
promotedContainer.clearFocus();
promotedContainer.removeAllViews();
java.util.List<PromotedApp> apps = state.promotedApps();
int adPosition = new java.util.Random().nextInt(apps.size() + 1);
for (int i = 0; i < apps.size(); i++) {
if (i == adPosition) {
addPromotedAd(promotedContainer);
}
PromotedApp app = apps.get(i);
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding itemBinding =
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding.inflate(inflater, promotedContainer, false);
Expand All @@ -69,9 +64,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
itemBinding.shareButton.setOnClickListener(v -> shareApp(app));
promotedContainer.addView(itemBinding.getRoot());
}
if (adPosition == apps.size()) {
addPromotedAd(promotedContainer);
}
});
new FastScrollerBuilder(binding.scrollView)
.useMd2Style()
Expand Down Expand Up @@ -120,17 +112,4 @@ private void loadImage(String url, android.widget.ImageView imageView) {
.centerInside()
.into(imageView);
}

private void addPromotedAd(ViewGroup container) {
android.widget.FrameLayout adContainer = new android.widget.FrameLayout(requireContext());
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(dpToPx(160), dpToPx(180));
params.setMarginEnd(dpToPx(8));
adContainer.setLayoutParams(params);
NativeAdLoader.load(requireContext(), adContainer, com.d4rk.androidtutorials.java.R.layout.promoted_native_ad);
container.addView(adContainer);
}

private int dpToPx(int dp) {
return Math.round(dp * requireContext().getResources().getDisplayMetrics().density);
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
android:id="@+id/large_banner_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:nativeAdLayout="@layout/native_ad" />
Expand Down Expand Up @@ -218,6 +219,7 @@
android:id="@+id/small_banner_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
app:nativeAdLayout="@layout/native_ad" />

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/layout/native_ad.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
style="@style/Widget.Material3.CardView.Filled"
style="@style/Widget.Material3.CardView.Outlined"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardView">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
android:layout_margin="16dp"
android:orientation="vertical">

<TextView
android:id="@+id/ad_attribution"
Expand Down
69 changes: 0 additions & 69 deletions app/src/main/res/layout/promoted_native_ad.xml

This file was deleted.