Skip to content
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

Banner ad doesn't work in android library #23

Open
nboy1 opened this issue Sep 20, 2023 · 1 comment
Open

Banner ad doesn't work in android library #23

nboy1 opened this issue Sep 20, 2023 · 1 comment

Comments

@nboy1
Copy link

nboy1 commented Sep 20, 2023

i'm trying to create a android library from huawei ads sdk i've tried everything i can imaging but banner doesn't show up.
note: i'm testing on nox player and your example apk work fine on this nox player,
this my code:

public class AdaptiveBanner {
private FREContext context;
private FrameLayout frameLayout;
private BannerView hwBannerView;
boolean initialLayoutComplete = false;
int height;
int total;
int ColorValue;TextView textView;
public void setContext(FREContext ctx) {
this.context = ctx;
}
public void showAdaptiveBanner(String bannerKey, String position) {

    hwBannerView = new BannerView(context.getActivity());
    hwBannerView.setAdId("testw6vs28auh3");
    hwBannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_320_50);
    hwBannerView.setBannerRefresh(30);

    hwBannerView.setBackgroundColor(Color.BLUE);


    textView = new TextView(context.getActivity());
    height = AdaptiveBannerHeight();
    int total = height;
    if (initialLayoutComplete == false) {
        FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, total);
        ColorValue = Color.BLACK;

        if (position.trim().equals("BOTTOM") || position.trim().equals("bottom") || position.trim().equals("Bottom")) {
            pa.gravity = Gravity.BOTTOM;
        } else {
            pa.gravity = Gravity.TOP;
        }

        frameLayout = new FrameLayout(context.getActivity());
        frameLayout.setBackgroundColor(ColorValue);
        context.getActivity().addContentView(frameLayout, pa);

        textView.setText("Banner Ad");
        textView.setTextSize(16);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.WHITE);
        frameLayout.addView(textView);

        // Create LayoutParams for the hwBannerView
        FrameLayout.LayoutParams bannerParams = new FrameLayout.LayoutParams(
                BannerAdSize.BANNER_SIZE_320_50.getWidthPx(context.getActivity()),BannerAdSize.BANNER_SIZE_320_50.getHeightPx(context.getActivity()));


        bannerParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        frameLayout.addView(hwBannerView, bannerParams);

        frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (!initialLayoutComplete) {
                            initialLayoutComplete = true;
                            loadBanner(bannerKey); // Call loadBanner after the layout is complete
                        }
                    }
                });

    }
}

private void loadBanner(String bannerKey) {
    hwBannerView.setAdListener(adListener);

    AdParam adParam = new AdParam.Builder().build();
    hwBannerView.loadAd(adParam);




}
 public AdListener adListener = new AdListener() {            @Override
 public void onAdLoaded() {
     // Called when an ad is loaded successfully.
     Toast.makeText(context.getActivity(), "inside onAdLoaded", Toast.LENGTH_LONG).show();
 }

     @Override
     public void onAdFailed(int errorCode) {
         String msg = String.valueOf(errorCode);
         // Called when an ad fails to be loaded.
         Toast.makeText(context.getActivity(), msg, Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdOpened() {
         // Called when an ad is opened.
         Toast.makeText(context.getActivity(), "inside onAdOpened", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdClicked() {
         // Called when an ad is clicked.
         Toast.makeText(context.getActivity(), "inside onAdClicked", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdLeave() {
         // Called when an ad leaves an app.
         Toast.makeText(context.getActivity(), "inside onAdLeave", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdClosed() {
         // Called when an ad is closed.
         Toast.makeText(context.getActivity(), "inside onAdClosed", Toast.LENGTH_LONG).show();
     }
 };


public int AdaptiveBannerHeight() {
  
    return BannerAdSize.BANNER_SIZE_320_50.getHeightPx(context.getActivity());
}

public void Hide() {
    // banner_view.removeAllViews();
}

}

note:

  • i've tried using XML Source file too but nothing same result, i give it height and background color just to debug if bannerview show up in my activity. i've tried build banner before initial the layout too but same result too no banner ad, sometime it's gives me code error 4 and sometimes 3, i think it's just when time to refreshing the ad it calls the listeners.
  • interstitial work fine,
  • i've added necessary declarations needed for huawei ads jar's and resources in xml file

thank you Huawei Support

@nboy1
Copy link
Author

nboy1 commented Sep 21, 2023

Edit:
now i downgrade version from 13.4.66.300 to 13.4.62.302, to can add the missing ads-template jar and it resources, and now i get a close ad button but there is no ads on banner, and when i click on that close button the app crash.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant