Highlight Text in Textview or Webview

Highlight Text in TextView or WebView

enable TextView's Spannable storage!
by default Spannable storage in EditText is true.

so

TextView myTV = (TextView)findViewById(R.id.textView1);
String textString = "StackOverFlow Rocks!!!";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);

Highlight the selected text in webview. [Android]

you need to run java script

 public static String Highlightscript = " <script language=\"javascript\">" +

"function highlightSelection(){" +
"var userSelection = window.getSelection();" +
"for(var i = 0; i < userSelection.rangeCount; i++)"
+ " highlightRange(userSelection.getRangeAt(i));" +
"}" +
"function highlightRange(range){"+
"span = document.createElement(\"span\");"+
"span.appendChild(range.extractContents());"+
"span.setAttribute(\"style\",\"display:block;background:#ffc570;\");"+
"range.insertNode(span);}"+
"</script> ";

and

  webView.loadUrl("javascript:highlightSelection()");

make sure you enabled javascript

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Highlight selected text in webview in android

I don't know of a way to take control of the selection contextual action bar. But you can prevent it from showing and replace it with your own version. First, create a HighlightWebView class that extends WebView, and include this override:

@Override
public android.view.ActionMode startActionMode(android.view.ActionMode.Callback callback) {
// this will start a new, custom Contextual Action Mode, in which you can control
// the menu options available.
highlightActivity.startActionMode(highlighActionModeCallback);

// this is to prevent the native text selection ActionMode
return null;
}

You'll need to create the HighlightActionModeCallback class as well. Here is a very basic version of a class that implements ActionMode.Callback:

public class HighlightActionModeCallBack implements ActionMode.Callback {

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.highlight_menu, menu);
return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}

}

You also want to google "contextual action mode" and/or "contextual action bar" for additional information.

Highlight a specific text in WebView android 4.0

Yes, you are right, highlight text on WebView works great on Android 2.2, but he appeared as not working on Android 4.0 and above

look at this link

here

Android webview: highlight a specific word in a page using javascript?

You should look at this page. Do a view source.

They use pure Javascript--not even jQuery--to highlight particular words on the webpage.

Basically they load all of the text they want to search through into a variable and continuously find the indexOf the search term they want to highlight. When they find the index of the term, they build a new string with everything before that index + a <span class="highlighted"> + the search term + </span>. Then they continue searching. Once they've searched and rebuilt all the text they replace the old text in the DOM with the new text.

They also have some CSS along the lines of .highlighted { background-color: yellow; }

How to select text or highlight text in webview android3.1 while clicking menu button

First of all, are you sure that's a convinient way of calling a webview's method?

Secondly, you should pay more attention to the docs, they say that method is deprecated, and not to rely on that functionality.

I think your best chance to access the html content and thus select/unselect any text is trough JavaScript. Create a JavaScript method to do that and call it from the webview.



Related Topics



Leave a reply



Submit