Android Question webview(show text) - copy and paste

kisoft

Well-Known Member
Licensed User
Hello. I use webview to show text. When the user scrolls the text sometimes the copy fields appear, paste. How to block this behavior?
 

Brandsum

Well-Known Member
Licensed User
  1. If you are using loadhtml to show text then you can prevent text selection via some CSS.
  2. If you are showing a webpage and you have access to that page content then you can prevent text selection via CSS or javascript.
  3. If you are showing a webpage and you don't have access to that page content then you can use WebViewExtra to execute javascript to prevent text selection.
If you don't know how to do that in css or javascript then tell me which one you need I'll post sample css or javascript code.
 

kisoft

Well-Known Member
Licensed User
HI
I display text files
B4X:
Dim xui As XUI
WebView1.LoadUrl(xui.FileUri(File.DirAssets, "elem2.html"))
 

Brandsum

Well-Known Member
Licensed User
Then just add this css inside head tag of that html file.
HTML:
<style>
* {
   -webkit-user-select: none;
}
</style>
Though this following code sample will work for all the cases (local and remote webpage).
B4X:
Private wve As WebViewExtras
wve.Initialize(webview1)
wve.JavaScriptEnabled = True
wve.LoadUrl(xui.FileUri(File.DirAssets, "elem2.html"))

Sub webview1_PageFinished (Url As String)
    wve.ExecuteJavascript("document.querySelector('*').style.webkitUserSelect = 'none';")
End Sub
 

kisoft

Well-Known Member
Licensed User
Ok, thank you so much for this example. It will be a bit problematic for me, I use over 1000 html files in the project
 

Brandsum

Well-Known Member
Licensed User
Then just use the webviewextra example. In that case, you don't have to edit any of those html files.
 
Top