0

I have created simple test project to test admob banner. I have issue when banner is loaded the whole activity seems laggy. I have button with animation which scale whole button up and down after click but its look laggy also I tried button click without animation and still problem remains and button click looks laggy. I was also looking to app profile to see whats going on and after button click CPU goes up to 30% and memory usage is around 100 mb. When I remove method loadAd() it runs smoothly. Tested on Pixel XL emulator 27 API.

MainActivity class:

public class MainActivity extends AppCompatActivity {

private Animation scaleUp,scaleDown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadAd();
    btnClickListener();

}
private void loadAd(){

    final LinearLayout adLayout = findViewById(R.id.adLayout);

    final AdView adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //test ad id

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            AdRequest adRequest = new AdRequest.Builder().build();
            adView.loadAd(adRequest); //load ad
            adLayout.addView(adView); //add ad to layout
        }
    },1000);
}


private void btnClickListener() {

    scaleUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_up);
    scaleDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_down);

    Button btnClick = findViewById(R.id.btn);
    btnClick.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                btnClick.startAnimation(scaleUp);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                btnClick.startAnimation(scaleDown);
            }
            return false;
        }
    });
}

}

activity xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/adLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/btn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Animation scale-up:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromYScale="1.2"
android:fromXScale="1"

android:toYScale="1.2"
android:toXScale="1.2"

android:pivotY="50%"
android:pivotX="50%"


android:duration="100"/>

Animation scale-down:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:fromYScale="1.2"
    android:fromXScale="1.2"

    android:toYScale="1"
    android:toXScale="1"

    android:pivotY="50%"
    android:pivotX="50%"


    android:duration="100"/>
zon1k
  • 87
  • 7
  • You don't need to initialize the banner inside a `Handler` or a `Thread` – cmak Dec 12 '21 at 18:14
  • Ok but it not helps my problem.. i tried Handler according to this thread : https://stackoverflow.com/questions/19446231/admob-banners-cause-high-cpu-usage – zon1k Dec 12 '21 at 18:20
  • Are you initializing AdMob before the banner (`MobileAds.initialize(this);`)? – cmak Dec 12 '21 at 19:13
  • I recently added MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); But problem still remains.. – zon1k Dec 12 '21 at 19:28
  • I tested it on several emulators with api 27 and its lagging but on emulators with api 30 is working smoothly – zon1k Dec 12 '21 at 19:33
  • I didn't notice any lags when I used AdMob (now I use mediation with another company), but apparently it's a common problem. Have you tried it like this `new Handler(Looper.getMainLooper())` ? Have you tried enabling hardware acceleration: https://stackoverflow.com/questions/20973834/android-admob-consuming-more-cpu ? Maybe it's fixed on newest APIs? – cmak Dec 12 '21 at 20:05
  • So I tested it on normal device with android 27 and its working fine so its seems like some kind of problem with emulators in android studio with android version 27 – zon1k Dec 13 '21 at 06:49

0 Answers0