Custom WebView Class Won’t Display Virtual Keyboard After Kotlinized

Bram Yeh
2 min readMay 11, 2022

This solution was found by my colledges, Ray and Cloud. Thanks for the investigation.

TL;DR

That WebView lost its ability to popup the virtual keyboard after initializing this CustomWebView by using the @JvmOverloads annotation.

Our team keeps going to refactor our Java code to Kotlin. We found our custom WebView can’t pop up the virtual keyboard when focused one day. We didn’t change any implementation or configuration settings during kotlinizing, but the keyboard won’t display anymore.

Our original Java code of WebView class looks like

public class CustomWebView extends WebView {    public CustomWebView(Context context) {
super(context);
initialize();
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
// some configuration setting for CustomWebView
....
}
}

and then its Kotlin code is

class CustomWebView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {
init {
// some configuration setting for CustomWebView
// the same as previous method initialize()
....
}
}

Everything looks good and works, our app display web pages as normal. Until we got a bug report that some users can’t display the keyboard when they click the edit text inside the WebView !!!!

We investigated our implementation again and again to see the difference between Java and Kotlin codes, but we didn’t find the root cause. However, one of our colleagues did a simple modification and this issue was resolved. (But he also said, he didn’t know why this worked, he just … try….)

What he modified is to remove @JvmOverloads and then provide multi constructors, like the following,

class CustomWebView : WebView {    constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : super(context, attrs, defStyleAttr )
init {
// some configuration setting for CustomWebView
// the same as previous method initialize()
....
}
}

Why WebView lost its ability to popup the virtual keyboard after initializing this CustomWebView by using the @JvmOverloads annotation with the default defStyleAttr: Int = 0.

This is because WebVIew’s constructor has a default defStyleAttr of com.androd.internal.R.attr.webViewStyle, that we shouldn't access this attribute.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bram Yeh
Bram Yeh

Written by Bram Yeh

Lead Android & iOS Mobile Engineer at Yahoo (Verizon Media) Taiwan https://www.linkedin.com/in/hanruyeh/

No responses yet

Write a response