Home / Blog / Dev notes

Adding AI Chat to a WebView App: 3 Things to Check

Dev notesSOL LABS AI3 min read

Building an app with WebView lets you manage most screens with web code, so changes ship fast. That makes it tempting to think "we'll just add AI chat as one more web screen." But once you try, chat that worked fine in the browser stops mid-answer inside the app, the keyboard covers the input box, or pressing back closes the whole app.

A WebView is a small browser embedded in an app. It's almost the same as a browser, except the app also controls the screen size, keyboard and back button. Here are the three places where this difference shows up in AI chat app development, in order.

1. Keep streaming answers from breaking

AI chat usually streams: instead of waiting for the full answer, it shows the answer bit by bit as it's generated. We covered the principles and event handling in our post on streaming responses, so here we'll focus only on what happens in WebView.

  • When the app goes to the background and comes back: if a user checks another app while waiting, the connection can drop. Fetch the latest message state from the server again when the screen returns, and answers won't be left half cut off.
  • Retry button: add a "get it again" button to answers that were cut off. Resuming with the interrupted conversation ID saves both cost and time compared with sending the same question from scratch.
  • Keep API keys out of the app: assume users can pull out the code inside a WebView, and always route AI calls through your own server.

2. Make the keyboard, input box and back button behave like an app

The input box at the bottom is the heart of a chat screen, so problems show up first when the keyboard opens.

SymptomWhat to check
Keyboard covers the input boxOn Android, whether the screen is set to resize by the keyboard height; on the web side, whether the screen height unit is a fixed value
New answers don't scroll downCode that scrolls to the last line when a message is added
Back button closes the appCode that first checks whether the WebView has a previous page

The official Android docs explain that a WebView keeps its own history, so when back is pressed you should check canGoBack() for a previous page and call goBack() if there is one. If the chat screen is a page inside the web app, back should lead to the previous screen rather than closing the app. That's what users expect.

One more thing: JavaScript is disabled by default in Android WebView. Chat screens run almost entirely on JavaScript, so check that it's enabled in settings from the start.

Want to check this against your own data?AI Readiness Assessment · we review your material and map candidates and orderContact us

3. Decide the line between web and native first

For AI chat, drawing most of the screen on the web makes changes faster. But when you need device features such as photo attachments, notifications or payments, the web side has to signal the app to "do this for me."

  • Android: you can create a bridge (addJavascriptInterface) that lets web JavaScript call app code. The official docs warn to use it only with pages you built yourself, because if an external link opens inside the WebView, that page can also call app features.
  • iOS: the web side sends a message with window.webkit.messageHandlers.name.postMessage(), and the app receives it with WKScriptMessageHandler.

While implementing in-app payments and a PG payment window inside a WebView, what helped us most was first splitting, in a table, which screens the web handles and which actions the app handles. AI chat is the same.

In short: draw the line first, with the web handling the conversation screen and answer display, and the app handling device actions such as photos, notifications and payments. Once the line is set, web screens can be fixed with a server deploy alone, so you release new app versions less often.

Checklist before you add it

  • AI calls go through your server, and there are no API keys in the app
  • Cut-off answers can be fetched again
  • The input box and the last answer stay visible when the keyboard opens
  • Back leads to the previous screen
  • The bridge that lets the web call app features only opens on your own pages

References

  • Android Developers, Build web apps in WebView: https://developer.android.com/develop/ui/views/layout/webapps/webview
  • Apple Developer, WKScriptMessageHandler: https://developer.apple.com/documentation/webkit/wkscriptmessagehandler
Want to build something similar?

We review your current system and data, then outline the migration order and scope.

Talk to usStart with the 3-minute AI readiness check →
SOL LABS AI

We build web apps, mobile apps and business systems, then add and run the AI they need. We build and operate our own meeting notes, contract and company analysis AI.

See our projects
Get the next post by email?

Related posts