how to add an action to part of a string android - android

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);`

Related

How to add hyperlink for Label in Xamarin.Forms

FormattedString t = new FormattedString();
t.Spans.Add(new Span() { Text = "About my Self\n\n" });
t.Spans.Add(new Span() { Text = "1. My website\n\n" });
t.Spans.Add(new Span() { Text = "2. My Linkedin profile\n\n" });
t.Spans.Add(new Span() { Text = "3. Twitter\n\n" });
I have a Text like above, I am able to set hyperlink whole text but I want to set a hyperlink for website and Linkedin words with some URL.
How can I set hyperlink for particular word in Paragraph for Label
Please help me on that.Thanks!!
As far as I know, there is no way to do that, your best choice is to create multiple labels or add a gesture recognizer for the whole text, wich would be a better idea since clicking on a small word is difficult on certain devices and the gesture recognizers on Xamarin are not the best ones around.
This might help for you: AwesomeHyperLinkLabel . I myself looking way to implement this and will start with this link, but I want to improve it a bit and replace links text with friendly text so instead direct link to page it would say "my page"

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!!!

Click on linkified TextView scrolls ScrollView

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... :)

Highlight Text feature like google play book

I want my android application to have text highlighting within a WebView. Similar to the functionality found in Google play book. Does any one have an idea how to achieve this?
I'm using a WebView because my content is in html form.
basically talking about this effect:
Starting from API level 11, you can use TextView's textIsSelectable flag.
Edit: Even though the question has now been edited to specifically mention WebView, OP #Suhail's comment "my content is in html form" does not fully disqualify TextView as it can also render some basic HTML.
The blue selection you see is part of the standard android environment when selecting text. So that should work on your standard webview without need of any custom code. => I'm no longer convinced this is true. Looks like it's not.
The green (yellow, orange, red, ...) selection however is custom.
You could read out the selected text from your selection event and use that information to update the html content, wrap the text in a span with a background color set.
Alternative approach is using javascript and enabling javascript in your webview. Not sure however how to get that done.
Some sources to check for that approach are https://github.com/btate/BTAndroidWebViewSelection and Android: How to select text from WebView, and highlight it onclick
Text selection from WebView details
To get the text selection working on a WebView you can use the following snippet (from this question). Trigger this on a button click (or other event) from your (context) menu.
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
If your using WebView Try to integrate Mozilla PDF.JS where you can render PDF . which can contain Images also .

Android handle HTML links and typed links in a TextView

I am trying to handle both HTML and typed links in TextViews and I am unable to find a combination of the built in tools to do this. I can make one or the other work but not both.
Given the following formats
http://google.com
Google!
Using .setMovementMethod(LinkMovementMethod.getInstance()) I can make the anchor tag turn into a link and open a webpage on click. Using .setAutoLinkMask(Linkify.ALL) I can make the typed link work as expected. The problem is setAutoLinkMask disables the setMovementMethod functionality and removes the highlighting it creates on the html link as well as its clicking functionality.
I tried searching for others with this issue and I believe I am being blocked by a lack of proper terms for this situation. Has anyone else come across a solution to handle both cases seamlessly?
This is what I have currently, only the typed link is linked in the TextView, the anchor just displays the text it wraps.
mTextViewBio.setText(Html.fromHtml(htmlstring));
mTextViewBio.setAutoLinkMask(Linkify.ALL);
mTextViewBio.setMovementMethod(LinkMovementMethod.getInstance());
mTextViewBio.setLinksClickable(true);
TextView output:
http://google.com
Google!
Problem is that when Linify.addLinks() is called first thing what this method does is removing all spans. When you use Html.fromHtml() Spanned is return, so when Linkify parse text again firstly remove "html links". I wrote a simply class LinkifyExtra. It has one extra method
public class LinkifyExtra extends Linkify {
public static Spanned addLinksHtmlAware(String htmlString) {
// gather links from html
Spanned spann = Html.fromHtml(htmlString);
URLSpan[] old = spann.getSpans(0, spann.length(), URLSpan.class);
List<Pair<Integer, Integer>> htmlLinks = new ArrayList<Pair<Integer, Integer>>();
for (URLSpan span : old) {
htmlLinks.add(new Pair<Integer, Integer>(spann.getSpanStart(span),
spann.getSpanEnd(span)));
}
// linkify spanned, html link will be lost
Linkify.addLinks((Spannable) spann, Linkify.ALL);
// add html links back
for (int i = 0; i < old.length; i++) {
((Spannable) spann).setSpan(old[i], htmlLinks.get(i).first,
htmlLinks.get(i).second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spann;
}
}
and use it like that
String htmlstring = "http://google.com Google!";
textView = (TextView)findViewById(R.id.textView1);
textView.setText(LinkifyExtra.addLinksHtmlAware(htmlstring));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
And it works. But you can't use mTextViewBio.setAutoLinkMask(Linkify.ALL);because it triggers addLinks() and remove "html links". Depends of what you want to do in bigger picture, this method may need some changes. I skip checking if spans are overlapping because I suppose it can't happen but if I am wrong you can simply copy this method.

Categories

Resources