From b270d924f33b85331f2a21ab4862d037435c7a23 Mon Sep 17 00:00:00 2001 From: chenqiuwei Date: Fri, 15 Mar 2024 18:03:28 +0800 Subject: [PATCH] Add Six Elements for Native Ad --- app/build.gradle | 4 +- .../huawei/hms/ads/sdk/NativeViewFactory.java | 50 +++++ ...tive_ad_with_app_download_btn_template.xml | 151 ++++++++++---- .../layout/native_common_medium_template.xml | 21 +- .../layout/native_small_image_template.xml | 166 +++++++++++---- .../layout/native_three_images_template.xml | 194 +++++++++++++----- app/src/main/res/values-zh-rCN/strings.xml | 147 +++++++++++++ 7 files changed, 587 insertions(+), 146 deletions(-) create mode 100644 app/src/main/res/values-zh-rCN/strings.xml diff --git a/app/build.gradle b/app/build.gradle index ab8d097..71dcb64 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,8 +29,8 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' - implementation 'com.huawei.hms:ads-lite:13.4.56.300' - implementation 'com.huawei.hms:ads-consent:3.4.54.300' + implementation 'com.huawei.hms:ads-lite:13.4.69.300' + implementation 'com.huawei.hms:ads-consent:3.4.69.300' implementation 'com.huawei.hms:ads-omsdk:1.3.35' testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java b/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java index cd94a7d..6bc0f96 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java @@ -7,6 +7,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import com.huawei.hms.ads.AppDownloadButton; @@ -103,6 +104,12 @@ public static View createSmallImageAdView(NativeAd nativeAd, final ViewGroup par nativeView.getCallToActionView() .setVisibility(null != nativeAd.getCallToAction() ? View.VISIBLE : View.INVISIBLE); + LinearLayout appDetailLayout = adRootView.findViewById(R.id.ad_app_detail_layout); + appDetailLayout.setVisibility(nativeAd.getInteractionType() == 2 ? View.VISIBLE : View.GONE); + if (nativeAd.getInteractionType() == 2) { + setSixElements(parentView.getContext(), adRootView, nativeAd); + } + // Register a native ad object. nativeView.setNativeAd(nativeAd); @@ -144,12 +151,48 @@ public static View createThreeImagesAdView(NativeAd nativeAd, final ViewGroup pa imageView3.setImageDrawable(nativeAd.getImages().get(2).getDrawable()); } + LinearLayout appDetailLayout = adRootView.findViewById(R.id.ad_app_detail_layout); + appDetailLayout.setVisibility(nativeAd.getInteractionType() == 2 ? View.VISIBLE : View.GONE); + if (nativeAd.getInteractionType() == 2) { + setSixElements(parentView.getContext(), adRootView, nativeAd); + } + // Register a native ad object. nativeView.setNativeAd(nativeAd); return nativeView; } + private static void setSixElements(final Context context, View adRootView, final NativeAd nativeAd) { + TextView developerView = adRootView.findViewById(R.id.ad_app_developer); + TextView appVersionView = adRootView.findViewById(R.id.ad_app_version); + developerView.setText(nativeAd.getAppInfo().getDeveloperName()); + appVersionView.setText(context.getResources().getString(R.string.app_version) + ":" + nativeAd.getAppInfo().getVersionName()); + + TextView privacyView = adRootView.findViewById(R.id.ad_privacy); + TextView permissionView = adRootView.findViewById(R.id.ad_permission); + TextView detailView = adRootView.findViewById(R.id.ad_detail); + permissionView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + nativeAd.getAppInfo().showPermissionPage(context); + } + }); + + privacyView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + nativeAd.getAppInfo().showPrivacyPolicy(context); + } + }); + detailView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + nativeAd.showAppDetailPage(context); + } + }); + } + public static View createAppDownloadButtonAdView(NativeAd nativeAd, final ViewGroup parentView) { LayoutInflater inflater = LayoutInflater.from(parentView.getContext()); View adRootView = inflater.inflate(R.layout.native_ad_with_app_download_btn_template, null); @@ -177,6 +220,13 @@ public static View createAppDownloadButtonAdView(NativeAd nativeAd, final ViewGr ((Button) nativeView.getCallToActionView()).setText(nativeAd.getCallToAction()); } + LinearLayout appDetailLayout = adRootView.findViewById(R.id.ad_app_detail_layout); + appDetailLayout.setVisibility(nativeAd.getInteractionType() == 2 ? View.VISIBLE : View.GONE); + if (nativeAd.getInteractionType() == 2) { + setSixElements(parentView.getContext(), adRootView, nativeAd); + } + + // Register a native ad object. nativeView.setNativeAd(nativeAd); diff --git a/app/src/main/res/layout/native_ad_with_app_download_btn_template.xml b/app/src/main/res/layout/native_ad_with_app_download_btn_template.xml index 21ca535..2711654 100644 --- a/app/src/main/res/layout/native_ad_with_app_download_btn_template.xml +++ b/app/src/main/res/layout/native_ad_with_app_download_btn_template.xml @@ -15,54 +15,119 @@ + android:layout_height="wrap_content"/> - + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginTop="10dp" + android:layout_marginStart="16dp" + android:textColor="@color/black" + android:textSize="16sp" /> - + android:layout_marginTop="10dp" + android:orientation="horizontal"> + + + + + + + + + + + + + + + + + + + + + - @@ -88,13 +154,14 @@ android:layout_width="72dp" android:layout_height="26dp" android:layout_alignParentEnd="true" - android:layout_marginTop="23dp" - android:layout_marginEnd="52dp" - android:layout_marginBottom="23dp" + android:layout_marginTop="24dp" + android:layout_marginEnd="16dp" + android:layout_marginBottom="24dp" android:background="@drawable/native_button_rounded_corners_shape" android:textColor="#FFFFFF" android:textSize="10sp" android:visibility="gone"/> + diff --git a/app/src/main/res/layout/native_common_medium_template.xml b/app/src/main/res/layout/native_common_medium_template.xml index 09bcc22..504c392 100644 --- a/app/src/main/res/layout/native_common_medium_template.xml +++ b/app/src/main/res/layout/native_common_medium_template.xml @@ -27,31 +27,30 @@ android:id="@+id/ad_title" android:layout_width="180dp" android:layout_height="19dp" - android:layout_marginStart="24dp" + android:layout_marginStart="10dp" android:layout_marginTop="16dp" android:alpha="1" android:textColor="#000000" - android:textSize="@dimen/hiad_text_13_sp" /> + android:textSize="16sp" /> + android:textColor="#666666"/> diff --git a/app/src/main/res/layout/native_small_image_template.xml b/app/src/main/res/layout/native_small_image_template.xml index 58d9c6f..a3be740 100644 --- a/app/src/main/res/layout/native_small_image_template.xml +++ b/app/src/main/res/layout/native_small_image_template.xml @@ -9,73 +9,163 @@ android:orientation="vertical"> + android:layout_alignParentTop="true" > - + android:layout_height="wrap_content" + android:textColor="@color/black" + android:textSize="13sp" /> - + android:layout_height="wrap_content" + android:layout_below="@+id/ad_title" + android:layout_alignParentStart="true" > + + + + + + + + + - + + + + + + + + + + + + + +