-
-
Notifications
You must be signed in to change notification settings - Fork 3
Integrate native ad into Android Studio lessons #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MihaiCristianCondrea
merged 9 commits into
main
from
codex/create-list-style-ad-layout-and-preferences
Sep 11, 2025
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
551d207
Add native ad preference for Android Studio lessons
MihaiCristianCondrea 013ed22
Randomly insert native ads in Android Studio lessons
MihaiCristianCondrea 832ee8c
Force ad container relayout after native ad loads
MihaiCristianCondrea 7b90c90
Refactor Android Studio list to RecyclerView with inline ads
MihaiCristianCondrea f54b463
Restore lesson categories in RecyclerView
MihaiCristianCondrea 2f7f75e
Remove card from category row layout
MihaiCristianCondrea 0c9d355
Refine lesson list spacing and ad placement
MihaiCristianCondrea 5273ef2
Tweak native ad layout and list spacing
MihaiCristianCondrea a4a8da1
Remove stray override annotation
MihaiCristianCondrea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/d4rk/androidtutorials/java/ads/preferences/NativeAdPreference.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.d4rk.androidtutorials.java.ads.preferences; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.AttributeSet; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.preference.Preference; | ||
| import androidx.preference.PreferenceViewHolder; | ||
|
|
||
| import com.d4rk.androidtutorials.java.R; | ||
| import com.d4rk.androidtutorials.java.ads.views.NativeAdBannerView; | ||
| import com.google.android.gms.ads.AdRequest; | ||
|
|
||
| /** | ||
| * Preference that displays a NativeAdBannerView. | ||
| */ | ||
| public class NativeAdPreference extends Preference { | ||
|
|
||
| public NativeAdPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
| super(context, attrs, defStyleAttr, defStyleRes); | ||
| setLayoutResource(R.layout.native_ad_preference); | ||
| } | ||
|
|
||
| public NativeAdPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
| this(context, attrs, defStyleAttr, 0); | ||
| } | ||
|
|
||
| public NativeAdPreference(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
| this(context, attrs, 0); | ||
| } | ||
|
|
||
| public NativeAdPreference(@NonNull Context context) { | ||
| this(context, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { | ||
| super.onBindViewHolder(holder); | ||
| NativeAdBannerView adView = (NativeAdBannerView) holder.itemView.findViewById(R.id.native_ad_view); | ||
| if (adView != null) { | ||
| adView.loadAd(new AdRequest.Builder().build()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?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="match_parent" | ||
| android:layout_height="wrap_content"> | ||
|
|
||
| <com.google.android.material.card.MaterialCardView | ||
| android:id="@+id/ad_card" | ||
| style="@style/Widget.Material3.CardView.Outlined" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginHorizontal="16dp" | ||
| android:layout_marginTop="8dp" | ||
| android:layout_marginBottom="8dp" | ||
| app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewBottomRounded"> | ||
|
|
||
| <LinearLayout | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:orientation="vertical" | ||
| android:padding="16dp"> | ||
|
|
||
| <include layout="@layout/ad_attribution" /> | ||
|
|
||
| <LinearLayout | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="8dp" | ||
| android:orientation="horizontal" | ||
| android:gravity="center_vertical"> | ||
|
|
||
| <FrameLayout | ||
| android:layout_width="60dp" | ||
| android:layout_height="60dp" | ||
| android:layout_marginEnd="16dp"> | ||
|
|
||
| <com.google.android.gms.ads.nativead.MediaView | ||
| android:id="@+id/ad_media" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" /> | ||
|
|
||
| <ImageView | ||
| android:id="@+id/ad_app_icon" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:visibility="gone" /> | ||
| </FrameLayout> | ||
|
|
||
| <LinearLayout | ||
| android:layout_width="0dp" | ||
| android:layout_height="wrap_content" | ||
| android:layout_weight="1" | ||
| android:orientation="vertical"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/ad_headline" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:textAppearance="@style/TextAppearance.Material3.TitleSmall" /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/ad_body" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:textAppearance="@style/TextAppearance.Material3.BodySmall" /> | ||
|
|
||
| <me.zhanghai.android.materialratingbar.MaterialRatingBar | ||
| android:id="@+id/ad_stars" | ||
| style="@style/Widget.MaterialRatingBar.RatingBar.Small" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:isIndicator="true" | ||
| android:numStars="5" | ||
| android:stepSize="0.5" /> | ||
| </LinearLayout> | ||
|
|
||
| <com.google.android.material.button.MaterialButton | ||
| android:id="@+id/ad_call_to_action" | ||
| style="@style/Widget.Material3.Button.TextButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" /> | ||
| </LinearLayout> | ||
| </LinearLayout> | ||
| </com.google.android.material.card.MaterialCardView> | ||
| </com.google.android.gms.ads.nativead.NativeAdView> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <com.d4rk.androidtutorials.java.ads.views.NativeAdBannerView xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| android:id="@+id/native_ad_view" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| app:nativeAdLayout="@layout/android_studio_list_native_ad" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Add title or null-check for native ad preference
The newly inserted
NativeAdPreferencehas notitleattribute.AndroidStudioFragment.applyFilterassumes everyPreferencereturns a non-null title and callspref.getTitle().toString()when the search view is used, which now throws aNullPointerExceptionbecause this ad preference lacks a title. Either give this preference a dummy title or make the filter logic null-safe so searching in the Android Studio lessons screen doesn’t crash.Useful? React with 👍 / 👎.