Skip to content

Commit

Permalink
Add Six Elements for Native Ad
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqiuwei committed Mar 15, 2024
1 parent d1eb21d commit b270d92
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 146 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
151 changes: 109 additions & 42 deletions app/src/main/res/layout/native_ad_with_app_download_btn_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,119 @@
<com.huawei.hms.ads.nativead.MediaView
android:id="@+id/ad_media"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"/>

<RelativeLayout
android:id="@+id/left_bottom_view"
android:id="@+id/left_bottom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ad_media">

<TextView
android:id="@+id/ad_title"
android:layout_width="180dp"
android:layout_height="19dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:alpha="1"
android:textColor="#000000"
android:textSize="@dimen/hiad_text_13_sp" />
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" />

<TextView
android:id="@+id/ad_source"
<LinearLayout
android:id="@+id/ad_source_layout"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_height="wrap_content"
android:layout_below="@id/ad_title"
android:layout_marginStart="24dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="16dp"
android:alpha="0.6"
android:maxWidth="158dp"
android:textColor="#666666"
android:textSize="@dimen/hiad_text_12_sp" />
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/ad_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:maxWidth="100dp"
android:textColor="#666666"/>

<TextView
android:id="@+id/ad_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:gravity="center"
android:text="AD"
android:textColor="#666666"/>
<TextView
android:id="@+id/ad_app_developer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:maxWidth="158dp"
android:textColor="#666666"/>

</LinearLayout>

<LinearLayout
android:id="@+id/ad_app_detail_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ad_source_layout"
android:orientation="horizontal">
<TextView
android:id="@+id/ad_app_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:maxWidth="100dp"
android:text="@string/app_version"
android:textColor="#666666"/>

<TextView
android:layout_width="1dp"
android:layout_height="10dp"
android:layout_marginStart="8dp"
android:layout_gravity="center"
android:background="#666666"/>

<TextView
android:id="@+id/ad_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/app_detail"
android:textColor="#666666"/>

<TextView
android:layout_width="1dp"
android:layout_height="10dp"
android:layout_marginStart="8dp"
android:layout_gravity="center"
android:background="#666666"/>
<TextView
android:id="@+id/ad_privacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/app_privacy"
android:textColor="#666666"/>

<TextView
android:layout_width="1dp"
android:layout_height="10dp"
android:layout_marginStart="8dp"
android:layout_gravity="center"
android:background="#666666"/>

<TextView
android:id="@+id/ad_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/app_permission"
android:textColor="#666666"/>
</LinearLayout>

<TextView
android:id="@+id/ad_flag"
android:layout_width="20dp"
android:layout_height="14dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:layout_toEndOf="@+id/ad_source"
android:background="@drawable/native_flag_rounded_corners_shape"
android:gravity="center"
android:text="@string/ad_flag"
android:textColor="#FFFFFF"
android:textSize="8sp"
android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/right_bottom_view"
android:id="@+id/right_bottom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ad_media"
Expand All @@ -74,12 +139,13 @@
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_centerVertical="true"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="16dp"
progress_button:hiad_fixedWidth="true"
progress_button:hiad_maxWidth="96dp"
progress_button:hiad_minWidth="48dp"
progress_button:hiad_maxWidth="100dp"
progress_button:hiad_minWidth="72dp"
progress_button:hiad_textColor="#FFFFFF"
progress_button:hiad_textSize="12sp" />

Expand All @@ -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"/>
</RelativeLayout>

</RelativeLayout>
</com.huawei.hms.ads.nativead.NativeView>
21 changes: 10 additions & 11 deletions app/src/main/res/layout/native_common_medium_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<TextView
android:id="@+id/ad_source"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_below="@id/ad_title"
android:layout_marginStart="24dp"
android:layout_marginTop="2dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="16dp"
android:alpha="0.6"
android:maxWidth="158dp"
android:textColor="#666666"
android:textSize="@dimen/hiad_text_12_sp" />
android:textColor="#666666"/>

<TextView
android:id="@+id/ad_flag"
android:layout_width="20dp"
android:layout_height="14dp"
android:layout_below="@id/ad_title"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/ad_source"
android:background="@drawable/native_flag_rounded_corners_shape"
android:gravity="center"
Expand All @@ -73,9 +72,9 @@
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="20dp"
android:layout_marginBottom="24dp"
android:background="@drawable/native_button_rounded_corners_shape"
android:textColor="#FFFFFF"
android:textSize="10sp" />
Expand Down
Loading

0 comments on commit b270d92

Please sign in to comment.