|
1 | 1 | package com.d4rk.androidtutorials.java.ads.views;
|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
| 4 | +import android.content.res.TypedArray; |
4 | 5 | import android.util.AttributeSet;
|
5 | 6 | import android.widget.FrameLayout;
|
6 | 7 |
|
| 8 | +import androidx.annotation.LayoutRes; |
7 | 9 | import androidx.annotation.NonNull;
|
8 | 10 | import androidx.annotation.Nullable;
|
9 | 11 |
|
| 12 | +import com.d4rk.androidtutorials.java.R; |
10 | 13 | import com.google.android.gms.ads.AdRequest;
|
11 | 14 | import com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader;
|
12 | 15 |
|
|
16 | 19 | */
|
17 | 20 | public class NativeAdBannerView extends FrameLayout {
|
18 | 21 |
|
| 22 | + private int layoutRes = R.layout.native_ad; |
| 23 | + |
19 | 24 | public NativeAdBannerView(@NonNull Context context) {
|
20 | 25 | super(context);
|
| 26 | + init(context, null, 0); |
21 | 27 | }
|
22 | 28 |
|
23 | 29 | public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
24 | 30 | super(context, attrs);
|
| 31 | + init(context, attrs, 0); |
25 | 32 | }
|
26 | 33 |
|
27 | 34 | public NativeAdBannerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
28 | 35 | super(context, attrs, defStyleAttr);
|
| 36 | + init(context, attrs, defStyleAttr); |
| 37 | + } |
| 38 | + |
| 39 | + private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 40 | + if (attrs != null) { |
| 41 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NativeAdBannerView, defStyleAttr, 0); |
| 42 | + layoutRes = a.getResourceId(R.styleable.NativeAdBannerView_nativeAdLayout, R.layout.native_ad); |
| 43 | + a.recycle(); |
| 44 | + } |
29 | 45 | }
|
30 | 46 |
|
31 | 47 | public void loadAd(AdRequest adRequest) {
|
32 | | - NativeAdLoader.load(getContext(), this); |
| 48 | + NativeAdLoader.load(getContext(), this, layoutRes); |
| 49 | + } |
| 50 | + |
| 51 | + public void setNativeAdLayout(@LayoutRes int layoutRes) { |
| 52 | + this.layoutRes = layoutRes; |
33 | 53 | }
|
34 | 54 | }
|
0 commit comments