android webview text selection listener - android

hey all
i am using a webview in my android app. once i select text on a page i know i can use clipboardmanager to "getText()". But is there a listener that polls whenever a new text is selected?
i want to trigger an event everytime a user selects a text.
thanks a lot :)

For anyone struggling with the same situation, i found the solution in using android 3.0 api 11 , which has an event handler:
ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener
= new ClipboardManager.OnPrimaryClipChangedListener() {
public void onPrimaryClipChanged() {
// Your code in here
}
};

Related

How to make spannable text clickable with Accessibility mode on

I have a problem statement where i need to run my application with Accessibility setting on, to have talk back feedback, but the problem here is when i click on a TextView which have Spannable link in it, then it reads the full text but dose not allow me to click on that Spannable text separately while disabling the accessibility allows to make string multi spannable or link clickable.
here is my code to make String clickable :
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
If you are using Android X library you should be able to handle accessibility and clickable spannable strings by:
ViewCompat.enableAccessibleClickableSpanSupport(yourView);
Also make sure you have the latest dependency:
com.android.support:appcompat-v7:28.0.0
It should work back to API 19.
Note: To enable Android X library go to your gradle.properties and add these lines:
android.useAndroidX = true
android.enableJetifier = true
I'm afraid there is no way in android to implement that (I had the same issue for months). the only way is using the local context menu. Looks like Talkback is trying to make the ADA users to get use to the menus using there gestures, which will fix too many issue in our dev side. There might be another way, which is creating a WebView and then add html elements which will handle everything, BUT this will be bad for the app performance :(
As mentioned here: Clickable links (Google support)
you have to access local context menu to activate any clickable span by Swiping up and then right, and then click on Links submenu.
Hope this helps :)
Try this one, worked for me. An alternate working solution.
private void setUpBottomSheetAccessibility() {
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
#Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_LONG_CLICKED) {
//Put your work to be done on double click;
}
super.onPopulateAccessibilityEvent(host, event);
}
});
}
Above code view is your Textview or any view. And this event work on double click.
Working fine at my end.
Nothing to do.Just you click the Spannable link with two finger on the screen, and one of them must be on the Spannable link. Try some times!!!

android webview - change clipboard data when clipboard changed

I am developing web browser using webview. (Android API level 21)
My goal is when user copy certain text in webView, getting that text value and change it.
I used clipboard change event listner, so when event listener capture copy event, getPrimaryClip, change clip data and setPrimaryClip are processing.
But, event listener is still alive so after setPrimaryClip is called, this process is called again and again...
How can I control just one time?
Or Could you suggest other event listener?
Edit
I referenced this -> made custom contextual action bar.
And with JavaScript code taught by DevTest(answer for this question), I success to make my goal.
The only way to get text selection from a WebView is based on javascript. This is not specific to the action mode, this is how WebView text selection is supposed to be retrieved according to WebView developers' point of view. They deliberately decided to not provide an API to access text selection from Java.
The solution comprise 2 approaches.
With Android API >= 19 you can use evaluateJavascript:
webview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>()
{
#Override
public void onReceiveValue(String value)
{
Log.v(TAG, "SELECTION:" + value);
}
});
On older builds your only resort is a custom javascript interface with a single method accepting String, which you should call via webview.loadUrl passing the same thing:
webview.loadUrl("javascript:js.callback(window.getSelection().toString())");
where js is the attached javascript interface:
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(), "js");
and
public class WebAppInterface
{
#JavascriptInterface
public void callback(String value)
{
Log.v(TAG, "SELECTION:" + value);
}
}
Answer from Here
UPDATE:
For modifying and copy data (as your comment), Store the selected value in a member variable like "Seleted_Value" and when on clicking copy button (which is handling action mode - search about custom action mode) modify the "Selected_Value" variable and copy to clipboard using :
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text",Selected_Value);
clipboard.setPrimaryClip(clip);
Details here :

Android Swipe Images Intro Screen on First Run

I've been looking around a lot lately but haven't really found much on this.
I'm making my third Android app and I'm looking to implement an intro screen on first run of the app where a series of images are shown explaining the apps functionality and the idea behind it; you can swipe the images left or right and at the last image you get to the app by swiping.
I really like the sort of thing they have done with the CamScanner app but despite my searching I have no idea how to implement it other knowing a little bit about some people referring to Fragments. Any help would be appreciated greatly and since we need better UI on Android, a good answer would help a lot of developers take the cue! :)
create a method to show popup window. show images in a scroll view in that popup. at last image set touch listener to dismiss that popup.
and call that method from onResume method of your Activity like this
protected void onResume(){
super.onResume();
SharedPreferences pref = getSharedPreferences(MyPrefs, MODE_PRIVATE);
boolean b = pref.getBoolean("FirstTime",true);
if(b)
{ new Handler().postDelayed(new Runnable() {
public void run() {
showIntroPopup();
}
}, 100);
}
}
in that popup set "FirstTime" boolean to false in SharedPreferences.

Run multiple performClick() with UI update

button.performClick();
For software demonstration purposes, I want to show the user interface updating after each button performClick(). For example, if the Activity was a calculator I can currently simulate the pressing of buttons [1], [2] and [3] using
btn1.performClick();
btn2.performClick();
btn3.performClick();
However, these updates to the EditText too quickly with no visible pause, i.e. it appears "123" are written to the screen simultaneously. What I want is:-
btn1.performClick() updates UI so people can physically see only button press updated to the EditText before the next button does. Similarly with btn2.performClick() and then btn3.performClick().
You may want to use a library like Robotium, and use Solo.waitForText Method to do what you want.
The problem is that we can not determine in advance the time that it will take to display the text, as it depends on the content of your onClick method.
It's why Robotium may be useful for what you want.
You can either use
Thread.sleep(delay);
or use
handler.postDelayed(Runnable,delay);
Use handler for btn2.performClick() and btn3.performClick() like...
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// do something after 100ms
btn2.performClick();
}
}, 100);

Facebook android scrumptious tutorial ListView not clickable on Google TV

I have just recently built an android tablet app (looking to port it to Google TV now) which is in close analogy to the Facebook Android Scrumptious App tutorial. In particular the issue is in the section of "Show Friends" of that tutorial: http://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/show-friends/
On the tablet when I touch the Select a Friend part of the ListView, it responds to the onClick. But on the Google TV using Dpad remote, this doesn't work. In particular, (after doing some Log) the onClick never gets called. I have written the part of the code from the tutorial where the onClick doesn't get called.
Just to summarize: This onClick DOES GET CALLED on android tablet and mobile. BUT DOES NOT GET CALLED when on Google TV and clicking using the Enter button on the remote control.
Any help on getting this onClick to get called on the Google TV platform would be most helpful.
private class PeopleListElement extends BaseListElement {
public PeopleListElement(int requestCode) {
super(getActivity().getResources().getDrawable(R.drawable.action_people),
getActivity().getResources().getString(R.string.action_people),
getActivity().getResources().getString(R.string.action_people_default),
requestCode);
}
#Override
protected View.OnClickListener getOnClickListener() {
return new View.OnClickListener() {
#Override
public void onClick(View view) {
// Supposed to do something here BUT ON GOOGLE TV DOES NOT GET CALLED
}
};
}
what happens when you use the touchpad pointer to click the item? I suspect that it may be related to the layout hierarchy and that something higher up is grabbing the dpad event. Conversely it could be that dpad focus is not on the element you think it is.
There are some bugs amongst the various Google TV devices not sending the right key codes for things like OK/Enter or the D-pad center. You might want to add a key listener to see what codes are getting sent to your view.

Categories

Resources