Search code examples
androidwebviewhint

Android webview hint text


Is there a way to display a hint text in Android's WebView, such as in EditTexts? As in, text that displays then gets hidden when you start to type?


Solution

  • You can inject JavaScript-code which will do the trick, just like

    //Perform click on button 1
    webView.loadUrl("javascript:"+
            "document.getElementsByTagName('button')[1].click();");
    

    If there was a field for 'password', you could try the following untested code:

    webView.loadUrl("javascript:"+
                +"pass = document.getElementById('password');"+
                +"pass.value('Password...');"+
                +"setOnFocus(pass);"+
    
                +"function setOnFocus(element) {"+
                    +"element.onfocus = function() {"+
                        +"element.value='';"+
                    +"}"+
                +"}");