How to Get Content from WebView from Android Studio
If you want to get content from a WebView, you’ll need to evaluate JavaScript, like this:
webview1.evaluateJavascript("(function() { return document.body.innerText; })();",new ValueCallback<String>() {@Overridepublic void onReceiveValue(String htmlContent) {// htmlContent is the content of the pageshareContent = htmlContent;// Now share itIntent shareIntent = new Intent(Intent.ACTION_SEND);shareIntent.setType("text/plain");shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);startActivity(Intent.createChooser(shareIntent, "Share via"));}});Here's a complete Java example showing how to load content into a WebView, extract it using JavaScript, and allow the user to either copy it to clipboard or share it via other apps.
✅ Full Working Java Code (Activity)
import android.content.ClipData;import android.content.ClipboardManager;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.webkit.ValueCallback;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {WebView webview1;String shareContent = "";Button btnCopy, btnShare;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // Make sure you have this layoutwebview1 = findViewById(R.id.webview1);btnCopy = findViewById(R.id.btnCopy);btnShare = findViewById(R.id.btnShare);// Enable JavaScriptwebview1.getSettings().setJavaScriptEnabled(true);// Ensure content is fully loaded before extractingwebview1.setWebViewClient(new WebViewClient() {public void onPageFinished(WebView view, String url) {// Extract text content after loadingextractContentFromWebView();}});// Load your local HTML filewebview1.loadUrl("file:///android_asset/btn41.html");// Copy to clipboardbtnCopy.setOnClickListener(v -> {if (!shareContent.isEmpty()) {ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);ClipData clip = ClipData.newPlainText("webview content", shareContent);clipboard.setPrimaryClip(clip);Toast.makeText(MainActivity.this, "Copied to clipboard", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "Content not loaded yet", Toast.LENGTH_SHORT).show();}});// Share contentbtnShare.setOnClickListener(v -> {if (!shareContent.isEmpty()) {Intent shareIntent = new Intent(Intent.ACTION_SEND);shareIntent.setType("text/plain");shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared from my app");shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);startActivity(Intent.createChooser(shareIntent, "Share via"));} else {Toast.makeText(MainActivity.this, "Content not loaded yet", Toast.LENGTH_SHORT).show();}});}private void extractContentFromWebView() {webview1.evaluateJavascript("(function() { return document.body.innerText; })();",new ValueCallback<String>() {@Overridepublic void onReceiveValue(String value) {// Clean the received value (remove quotes)shareContent = value.replaceAll("^\"|\"$", "").replace("\\n", "\n").replace("\\t", "\t");}});}}🖼️
res/layout/activity_main.xml
(UI Layout Example)<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:padding="16dp"android:layout_width="match_parent"android:layout_height="match_parent"><WebViewandroid:id="@+id/webview1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><Buttonandroid:id="@+id/btnCopy"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Copy Content" /><Buttonandroid:id="@+id/btnShare"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Share Content" /></LinearLayout>
How to Get Content from WebView from Android Studio
Reviewed by TechTubeHQ
on
June 26, 2025
Rating: 5

No comments: