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 @@ -9,6 +9,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.LayoutRes;

import com.d4rk.androidtutorials.java.R;
import com.google.android.gms.ads.AdLoader;
Expand All @@ -23,10 +24,20 @@
public class NativeAdLoader {

public static void load(@NonNull Context context, @NonNull ViewGroup container) {
load(context, container, R.layout.native_ad);
}

public static void load(@NonNull Context context, @NonNull ViewGroup container, @LayoutRes int layoutRes) {
AdLoader adLoader = new AdLoader.Builder(context, context.getString(R.string.native_ad_banner_unit_id))
.forNativeAd(nativeAd -> {
LayoutInflater inflater = LayoutInflater.from(context);
NativeAdView adView = (NativeAdView) inflater.inflate(R.layout.native_ad, container, false);
NativeAdView adView = (NativeAdView) inflater.inflate(layoutRes, container, false);
adView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
adView.setPadding(container.getPaddingLeft(), container.getPaddingTop(),
container.getPaddingRight(), container.getPaddingBottom());
container.setPadding(0, 0, 0, 0);
populateNativeAdView(nativeAd, adView);
container.removeAllViews();
container.addView(adView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
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;

import me.zhanghai.android.fastscroll.FastScrollerBuilder;

import com.bumptech.glide.Glide;
import dagger.hilt.android.AndroidEntryPoint;
import com.d4rk.androidtutorials.java.data.model.PromotedApp;

@AndroidEntryPoint
public class HomeFragment extends Fragment {
Expand Down Expand Up @@ -51,7 +53,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
binding.scrollView.clearFocus();
promotedContainer.clearFocus();
promotedContainer.removeAllViews();
for (com.d4rk.androidtutorials.java.data.model.PromotedApp app : state.promotedApps()) {
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);
loadImage(app.iconUrl(), itemBinding.appIcon);
Expand All @@ -61,6 +69,9 @@ 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 @@ -109,4 +120,17 @@ 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/native_ad.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<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"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardView">

<LinearLayout
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/res/layout/promoted_native_ad.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="160dp"
android:layout_height="180dp"
android:layout_marginEnd="8dp">

<com.google.android.material.card.MaterialCardView
style="@style/Widget.Material3.CardView.Filled"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="24dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ad_app_icon"
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/ad_headline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
app:layout_constraintTop_toBottomOf="@id/ad_app_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/ad_body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Material3.BodySmall"
app:layout_constraintTop_toBottomOf="@id/ad_headline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/ad_call_to_action"
style="@style/Widget.Material3.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/ad_body"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</com.google.android.gms.ads.nativead.NativeAdView>