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>() {
        @Override
        public void onReceiveValue(String htmlContent) {
            // htmlContent is the content of the page
            shareContent = htmlContent;

            // Now share it
            Intent 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Make sure you have this layout

        webview1 = findViewById(R.id.webview1);
        btnCopy = findViewById(R.id.btnCopy);
        btnShare = findViewById(R.id.btnShare);

        // Enable JavaScript
        webview1.getSettings().setJavaScriptEnabled(true);

        // Ensure content is fully loaded before extracting
        webview1.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                // Extract text content after loading
                extractContentFromWebView();
            }
        });

        // Load your local HTML file
        webview1.loadUrl("file:///android_asset/btn41.html");

        // Copy to clipboard
        btnCopy.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 content
        btnShare.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>() {
                @Override
                public 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"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btnCopy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Copy Content" />

    <Button
        android: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 How to Get Content from WebView from Android Studio Reviewed by TechTubeHQ on June 26, 2025 Rating: 5

No comments:

Powered by Blogger.