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!!!
Related
I have the fallowing text: "By clicking OK you will disable the service. Learn more".
i want to make "Learn more" clickable, however i want a popup menu to appear instead of directing to a website
i have used the fallowing stack question :
How to set the part of the text view is clickable
which worked great. i found the index of learn more by ". ". this solution crashes the application in Chinese and Hindi languages (in Hindi a point is written -> |).
How can i make the "Learn more" clickable in a generic way to show a popup menu?
Is there maybe a way to define the click action in strings.xml, like calling a link? (instead of calling a link -> launch popup menu?)
You can use WebView and anchor. Create new WebViewClient (especially you need this method: shouldOverrideUrlLoading()) and do everything you want when user will click your anchor.
You can create a click event based on the text as you defined.. Check this library.. it may help you.. https://github.com/klinker24/Android-TextView-LinkBuilder
solved it, might be a hack but it works fine.
in strings.xml i have added
//<a></a> tags to be removed later on
<string name="learn_more">By clicking OK you will disable the service. <a>Learn more</a></string>
in the code :
TextView textView= (TextView) findViewById(R.id.textViewInLayout);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.learn_more);
//indexes of the clickable text
int start = textView.getText().toString().indexOf("<a>");
int end = textView.getText().toString().indexOf("</a>");
//set the text as html to make the tags disappear
textView.setText(Html.fromHtml(getString(R.string.learn_more)));
//make the text clickable
Spannable spannable = (Spannable) textView.getText();
ClickableSpan myClickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
yourActionHere();
}
};
// end - 3 beacuse of </a>
spannable.setSpan(myClickableSpan, start, end - 3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);`
I have a ScrollView containing a TextView. I linkify parts of that TextView with
Pattern pattern = Pattern.compile("MyLink");
Linkify.addLinks(textView1, pattern, "mylink://");
I habe an Intent filter in my Manifest for mylink:// so a new Activity is opened when MyLink is clicked (as described in this question).
This works most of the time, sometimes though a click on the MyLink portion of the TextView doesn't open the Activity but only scrolls the ScrollView in which my TextView resides to the top.
Where does this behaviour come from and how can I fix it?
If you are attempting to open a link that leads to your current activity, it might be recreating the same activity and gives the sensation that it's scrolling up. Probably you want to modify your manifest and set the activity's launchMode attribute to singleTask
Why dont you use TextViewWithLinks?
With that you can have two types of click on the textview,
1. On TextView
2. On Link TextView
String text = "bananas on www.bananas.ba and Google";
TextViewWithLinks textview = new TextViewWithLinks(this);
textview.setText(Html.fromHtml(text));
textview.linkify(new TextViewWithLinks.OnClickLinksListener() {
#Override
public void onTextViewClick() {
// Do whatever you want
}
#Override
public void onLinkClick(String url) {
// Do whatever you want
}
});
//SET Colors
textview.setLinkColors(Color.RED, Color.BLACK);
setContentView(textview);
Let me know if this is not resolving your issue.
Enjoy Coding... :)
I want to have a portion of my TextView invisible. In detail, I present a math problem to the user:
9 * 9 = 81
and I want 81 to be invisible. As user types correct answer, I make digit "8" and then "1" visible.
I tried to use TextAppearanceSpan and ForegroundColorSpan with transparent color (both #00000000 and Color.TRANSPARENT) and they work perfectly for every device and emulator except KindleFire device and emulator.
On Kindle Fire, "81" appears colored in sort-of-dark-grey. It seems that rasterizer tries to make it invisible against some "sort-of-dark-grey" background, not against background (which looks like grid paper image) that I've set in the root LinearLayout of my Activity.
My question is: how can I make a portion of text in TextView invisible using neither TextAppearanceSpan nor ForegroundColorSpan? Does there exist another "span" that measures text correctly, but does not paint it?
Update
I've discovered that there exist some mysterous RasterizerSpan and MetricAffectingSpan, which could (judging by the title) help.
As I can see, the only meaning of RasterizerSpan is to set a Rasterizer to the TextPaint object:
public void updateDrawState(TextPaint ds) {
ds.setRasterizer(mRasterizer);
}
I found it promising: if I were able to set a kind of "void" rasterizer (that just does nothing), it would make this text invisible. Unfortunately
new RasterizerSpan(null); // (set mRasterizer to null)
did not help.
My another idea is to implement a custom ShaderSpan that would set a "void" shader:
public void updateDrawState(TextPaint ds) {
ds.setShader(myCustomVoidShader);
}
but I do not understand how to create a shader that produces "nothing".
See if overriding updateDrawState() on your ForegroundColorSpan and calling setXferMode() will work:
public class TransparentSpan extends ForegroundColorSpan {
private final PorterDuffXfermode MODE = new PorterDuffXfermode(PorterDuff.Mode.DST);
public TransparentSpan(int color) {
super(color);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setXfermode(MODE);
}
}
Sorry, I didn't have a Kindle Fire to test with :)
You can use AbsoluteSizeSpan, set the size to 0. So it will invisible
span = new AbsoluteSizeSpan(0, true);
spannableString.setSpan(span, wordPosition.start, wordPosition.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Notice:
The next word of the invisible word will take place the invisible word position.
Why not using the android visibility (true or false). It will do the same job you need (or there is a specificreason you would lie to use Span) ?
How to implement selecting text capability on Android 2.2? I searched Google but can't find a solution.
This is the only way I've found (from google) to support it for android 1.6+, it works but it's not ideal, I think in stock android you can't hold down a webview to highlight until v2.3, but I may be mistaken..
By the way this is for a webview, it might also work on textview, but I haven't tried it
(Note: this is currently what my shipped app is using, so it works with all the phones I've had it tested on, but the only reason I found this question was because I was searching and hoping that someone had come up with a better way by now)
I've just got a menu item called "Select text" which calls the function "selectnCopy()"
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//..
switch (item.getItemId()) {
case R.id.menu_select:
selectnCopy();
return true;
//..
}
}
Which looks like this:
public void selectnCopy() {
Toast.makeText(WebClass.this,getString(R.string.select_help),Toast.LENGTH_SHORT).show();
try {
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(wv);
} catch (Exception e) {
throw new AssertionError(e);
}
}
Notice I've put the select_help string there as a toast, that's just because it's not immediately clear to the user how it's supposed to work
<string name="select_help">Touch and drag to copy text.</string>
The Link #Stefan Hållén provided only worked after API Level 11.
And Android 2.2 is API Lv.8, that's the reason why you cannot get a resource identifier.
Have you set the text to be selectable? like this:
android:textIsSelectable="false" //OR true
See the Documentation for further reference.
After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you :
new Selectable TextView in android 3 (API <=11) component
An interesting workaround:
You could try displaying your text in a webview.
You just have to write the HTML tags and all of that into your string to display, and it should be selectable using the WebKit browser.
This should be fairly lightweight and transparent to the user, and I think it would solve your problem.
Let me know if you need a code example, it should be fairly simple. Just check out the WebView docs on http://developer.android.com/resources/tutorials/views/hello-webview.html
Best of luck!
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
}
};