Suraj Neupane Suraj Neupane - 27 days ago 10
Android Question

set/get Data in webview android

I have this webview:

<textarea name="text" id="textPage" class="text_area"></textarea>
<script language="javascript" charset="utf-8">
//call some method of javascript.
</script>


Is it possible to set text to textarea and get text from textarea.
Set/get data from id(if any method possible) will be appreciated.

Answer

After many research i found solution, it might useful in future:

Set Data:

Android:

webView.loadUrl("javascript:setData()");

JS:

function setData(){
//js code
}

Get Data:

Android:

JavascriptInterface jsInterface = new JavascriptInterface(MainActivity.this);
converterWeb.addJavascriptInterface(jsInterface, "Android");//android is a tag.

class:

public class JavascriptInterface {
        Context mContext;

        JavascriptInterface(Context c) {
            mContext = c;
        }

        public boolean getData(String name) {
            Toast.makeText(mContext, "Text: "+name, Toast.LENGTH_SHORT).show();
            return true;
        }
    }

JS:

Android.convertedText(value);//send data in tag.

better option still welcome.