Android webview scroll and facebook comments problem when Admob loads advert

Ioannis Anifantakis
2 min readApr 5, 2019

--

There is bizarre behavior in Android when you combine a “WebView” together with “AdMob”.

The Scrolling Problem

When the AdMob finishes loading, there is a known bug where the WebView scrolls down. This can be frustrating and there are various articles on the web with questions in sites such as StackOverflow regarding this problem.

The Scrolling Solution

The only working solution so far, found in several StackOverflow answers is to block the descendants’ focusability from your layout XML using the following code at the scrollbar’s XML:

android:descendantFocusability="blocksDescendants"

Doing the above hack will fix the scrolling problem that happens when your AdMob advert finishes loading.

However, that very fix might produce some other problems if in case you have any views as descendants inside that scrollbar since you will be unable to gain focus on them. One such example is if you have another WebView to display facebook comments.

You can find information regarding this approach here.

The Facebook Comments Problem

If you have a WebView to display Facebook Comments inside a parent view that blocks descendants’ focusability, then it will not be long until you start pulling your hair.

The actual problem is that this “blocking” does not allow you to gain focus on your WebView that displays the facebook comments, so you are unable to start typing there.

The obvious solution is to remove this blocking, but then you face the initial problem of the scrolled content of your WebView.

The Facebook Comments Solution

When you have identified what makes your WebView scroll down (in our case when AdMob loads its content), then you are in a very good position to fix this problem.

With the blocked descendants enabled via your XML, you have to wait until the AdMob finishes loading your advert.

adView.setAdListener(new AdListener(){
@Override
public void onAdLoaded() {
super.onAdLoaded();
myScrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
});

From the above code, you can easily see that our hack changes the value of the DescendantFocusability from blockDescendants to FocusBeforeDescendants.

When this programmatical change happens after the Advert is loaded, then you saved yourself from having the WebView content scrolled, but at the same time, you have allowed the descendant views to become focusable again.

The above hack allowed me to combine a Webview with an article content, followed by a WebView showing Facebook comments and an AdMob view.

Bonus

Here is an interesting article for integrating Facebook comments to your WebView.

P.S. The bonus material is irrelevant to my solution, but its a good starting point if you want to integrate facebook comments to your app via WebView.

--

--

Ioannis Anifantakis
Ioannis Anifantakis

Written by Ioannis Anifantakis

Senior Software Engineer @ Deloitte Digital — Android Apps Engineer, mobile developer