I'd like to allow users to highlight in yellow text in my webview.
So I wanna use some javascript to allow my app to get the selected text:
mWebview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>()
{
#Override
public void onReceiveValue(String value)
{
Log.v(TAG, "Webview selected text: " + value);
}
});
Ok, this will get the selected text in the webview. Now I need to save this texts in sharedpreferences to remember it next time user open the app, then find all the texts saved to highlight. Any ideas how to do this?
Related
I have a webview that I display some html texts (I have them in assets). I'd like to allow users to highlight some parts of it.
I was thinking in some solutions:
try to put the texts user hightlight in a shared pref and use:
webview.findAllAsync(shared_pref_string);
webview.setFindListener(new FindListener() {
#Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
// try to select the texts.
}
});
The problem I see is, user can select one word, like "what", and this code will select all "whats" the text has.
Use javascript:
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> ";
webView.loadUrl("javascript:highlightSelection()");
But this 2 solutions not seems nice to me, any other best way to do this and more modern?
this android library is implemented what you need:
https://github.com/FolioReader/FolioReader-Android
they are using this javascript library https://github.com/timdown/rangy, maybe this will make sense.
Here my scenario is -Verifying text color inside text box if user does not provide any data and directly clicks on search.
URL :- https://www.seniorhousingnet.com/
I am directly clicking on Search button without entering data in city text box ,in that case color is changing and i need to verify whether color of text is changing or not through selenium (java)
But from html if we observe before click and after click color is same rgb(113, 76, 93)
so developers are handling this through script, in this situation, can anybody explain me how to handle this test case?
Below is my code
#Then("Enter city name \"([^\"]*)\" in text field$")
public void enteringCityNameInsideTextField(String city) throws InterruptedException{
String functionName="searchBoxToEnterCity";
if(Driver.WebCtrl.isDisplayed("NewSHN1/HP1", "SearchBoxCity")){
System.out.println("city is "+city);
if(city.equalsIgnoreCase("")){
String textColorbefore = Driver.Gprops.GetWebDriver().findElement(By.cssSelector("input#searchHomeLocation")).getCssValue("color");
Driver.WebCtrl.sendKeys("NewSHN1/HP1", "SearchBoxCity", city);
Driver.WebCtrl.clickOnElement("NewSHN1/HP1", "Search");
Thread.sleep(4000);
String textColorafter =
System.out.println("text color before is "+textColorbefore);
System.out.println("text color after is "+textColorafter);
if(textColorbefore.equals(textColorafter)){
Driver.CUtil.WriteResults("Functioanlity", "wwithout entering any text directly clicking on search button ", "Failed");
}
else{
Driver.CUtil.WriteResults("Functioanlity", "wwithout entering any text directly clicking on search button ", "passed");
}
}
}
}
Below is the screenshot
I have a user search and a listview to populate with the results. The results have an "Add Friend" button.
This is what I have for the onClick:
OnClickListener addRequest = new OnClickListener() {
#Override
public void onClick(View v) {
userConnection = new UserConnection();
userConnection.setCurrentUser(User.getCurrentUser());
userConnection.setOtherUser(users.get(position).getObjectId()); ////HELP, this is where I need to get the objectId for the user and set it as OtherUser
userConnection.setFriendRequestStatus(appConstants.FRIENDREQUESTSTATUS.PENDING);
userConnection.setCurrentUserSentRequest(true);
Utils.showOKdialog(context, "Press OK to send friend request");
userConnection.saveInBackground();
Log.d(TAG, "You have sent a friend request to " + userConnection.getOtherUser().getLastName() + " " + userConnection.getOtherUser().getFirstName());
}
};
I have a comment on which line I need help with, the problem I am having is .getObjectId returns a String and .setOtheruser calls for a ParseUser. Any ideas on how to get the objectId which is a String on the user that is at the position of the list view clicked?
I also tried this but it came back with a null reference for .getParseUser("objectId")
userConnection.setOtherUser(users.get(position).getParseUser("objectId"));
Well I figured it out, I was making things too complicated as usual. The solution is just
userConnection.setOtherUser(users.get(position));
So I am trying to add the functionality that when you click on a phone number it would take you to the Dialer app with the pre-populated number. I have the code below:
mContactDetailsText.setText(phonetextBuilder.toString());
Pattern pattern = Pattern.compile("[0-9]+\\s+[0-9]+");
Linkify.addLinks(mContactDetailsText, pattern, "tel:");
and the Text is currently "T. 0123 4567890"
The current outcome is just having the above string without it being clickable. I have even tried added the following line, but to no luck:
mContactDetailsText.setAutoLinkMask(0);
Anyone got any ideas or can see what I am doing wrong?
Thanks
The autolink mask needs to include a search for phone numbers:
mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);
Then you'll need to set the links to be clickable:
mContactDetailsText.setLinksClickable(true);
You might also need movement method set like so:
mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())
You should be able to accomplish what you want with the other answers,
but this will definitely work and will give you more control over the display of the text and what will happen when you click the number.
String text = "T. ";
StringBuilder stringBuilder = new StringBuilder(text);
int phoneSpanStart = stringBuilder.length();
String phoneNumber = "0123 4567890"
stringBuilder.append(phoneNumber);
int phoneSpanEnd = stringBuilder.length();
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
startActivity(intent);
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
ds.setColor(Color.BLUE);
}
};
SpannableString spannableString = new SpannableString(stringBuilder);
spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannableString);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
You need to set an onClickListener() on your TextViews. Then they will respond to clicks.
I have created a custom view on Eclipse and defined a drag and drop listener for it. When I drag items from my view onto the native Java editor (using TextTransfer as the mechanism), the text successfully gets pasted on the editor.
However, when I try to do the opposite i.e. when I select a piece of text from the Java editor and drag it to my view, then the cursor shows an invalid sign and the drop doesn't work as expected. The Drop Target is also set to accept TextTransfer instances.
When I have two Java editors open and I drag text from one to the other, it works perfectly. Why does it not work when I drag the text onto my view?
I overrode the dragEnter function of the DropTargetAdapter in my view to check if the drag was being detected and it was. After printing the event.datatypes, I can also see the CF_TEXT type being supported. When I print the event.data, however it is null. Why?
Code is below:
viewDropTargetAdapter = new DropTargetAdapter()
{
#Override
public void drop(DropTargetEvent event)
{
addCodeSnippetAction.run();
}
#Override
public void dragEnter(DropTargetEvent event)
{
System.out.println("DATATYPE: " + event.currentDataType);
System.out.println("DATA: " + event.data);
System.out.println("DETAIL: " + event.detail);
TransferData[] td = event.dataTypes;
for(int i=0; i<td.length; i++)
{
System.out.println("Datatype of " + i + " is: " + td[i].type + " and " +getNameFromId(td[i].type));
}
super.dragEnter(event);
}
};
viewDropTarget = new DropTarget(viewer.getControl(), DND.DROP_COPY);
viewDropTarget.setTransfer(new Transfer[] {TextTransfer.getInstance()});
viewDropTarget.addDropListener(viewDropTargetAdapter);
Output is below:
DATATYPE: org.eclipse.swt.dnd.TransferData#77f5c2c7
DATA: null
DETAIL: 0
Datatype of 0 is: 13 and CF_UNICODETEXT
Datatype of 1 is: 1 and CF_TEXT
After some researching, I realized that it is necessary to set the event.detail variable manually in the DragEnter function. After adding the line:
event.detail = DND.DROP_COPY;
it works now.