How to select text from android webview programmatically

Course Queries Syllabus Queries 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I've an app for displaying my college syllabus where I display a bunch of html files stored as assets in an activity with a webview. The App supports only API 14+ The html files are plain text files. What I'm trying to do now is provide a share button which would copy the all the text in the webview and provide option to share it via the shareintent with the copied text as the body. I'm able to do this manually in the webview by long pressing on text and using select all button and copying it and then pasting it anywhere I want. This works perfectly. I just wanna replicate this action with the click of a button

Here's the code I tried :

android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
KeyEvent shiftPressEvent = new KeyEvent(0, 0,
        KeyEvent.ACTION_DOWN,
        KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(webView);

if(clipboard!=null) {
    String text = clipboard.getText().toString();
    Toast.makeText(SyllabusPage_alternative.this, "select_text_now     " + text, Toast.LENGTH_LONG).show();

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = text;
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

But this doesn't seem to be working right, when I press the button only the previous contents of the clipboard are presented in the toast and the body of the shareIntent. I just wanna know how to do this, select the whole text and then copy it into a string programmatically? Or Please Tell me of any other way I can approach this

Thank you for all the responses in advance

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.