3

I am developing an Android native application with WebView. I have used an HTML file to load the WebView and given functionality to the UI elements through javascript by enabling Javascript and adding a Javascript interface.

WebView myWeb=(WebView)findViewById(R.id.webView1);
    myWeb.getSettings().setJavaScriptEnabled(true);
    myWeb.getSettings().setLoadWithOverviewMode(true);
    myWeb.loadUrl("file:///android_asset/webview.html");

    myWeb.addJavascriptInterface(new Object()
    {
        @JavascriptInterface          
        public void recognizeIt(String message)
        {

            Toast.makeText(WebViewActivity.this, message,
                    Toast.LENGTH_LONG).show();
            Intent Intent = new Intent(WebViewActivity.this, StartActivity.class);
            startActivity(Intent);
        }
    }, "Android");    

below is my html file.

<html>
    <head>
          <script type="text/javascript">
                function shareIt(toast) {
                   var uname=document.getElementById("message").value;
                   Android.recognizeIt(uname);

                }
        </script>
    </head>
    <body>
        <label id="username"><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="uname" required id="message">
        <button type="button" onclick="shareIt('message')"> SEND </button>


    </body>
</html>

Now instead of html file, I want to use ReactJS to create UI screens in my app. I tried to search for the same but did not find any samples. I would be very grateful if someone could help / guide me here.

2 Answers 2

2

Any JavaScript React application you write can be loaded into the Android WebView. Just try following the basic tutorials at https://reactjs.org/. Then host those applications on a web server and you'll be able to load them into your Android WebView.

To make sure the Android WebView is configured properly, you can try loading an existing React application into your WebView. For example, try calling:

myWeb.loadUrl("https://ahfarmer.github.io/calculator/");

Does it load ok? If yes, then your WebView has been configured such that it should be able to show your own React applications.

For a simple way to run a web server to serve files from a directory containing your html file, use the following command:

browser-sync start --server --https

To install browser-sync, follow the instructions at https://browsersync.io/

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Michael, i want to ask you. i have one project that exist using android studio - java. and i want to show a interactive offline web view using reactjs in that project. So i want to import my new reactjs to my existing android project. Should i install web server in that android ? cause our customer doesn't use any connection
Yes you could try running a web server on the Android device so you can serve the ReactJS project to the WebView. If the Android device has no Internet connection though, I'm not sure the Android project will succeed when you call myWeb.loadUrl(...).
Update here. you can build reactjs project first. Then you can put that project into internal tab memory. and then you can access it by "myWeb.loadUrl("file:///"+ Environment.getExternalStorageDirectory()+"/build/index.html");" it works on me note: This is only static web that doesn't need database
0

Instead of writing ReactJS in an Android WebView, you could try writing your whole application in React Native, "React Native lets you build mobile apps using only JavaScript. It uses the same design as React, letting you compose a rich mobile UI from declarative components."

You wouldn't need the Android WebView in order to use React Native.

1 Comment

Yes Michael, i agree. Since my client wanted the app in mobile webview only we are doing in react js.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.