When there is any address in string, showing in TextView, need to be clickable and open on maps.
I've tried tv.setAutoLinkMask(Linkify.ALL). It is not working for all addresses.
Any other way that's make clickable and link all addresses in TextView?
you can use simple on click listener.
textView.setOnClickListener {
//open google maps
}
and for text to make it look like a link you can use:
<string name="underline_text"><![CDATA[<u>underlined text</u>]]></string>
Also, you can use blue text color (#0D6EFD) to make it more professional.
you can use Intent.ACTION_VIEW to open google map from your app. Something like this:
tv.setOnClickListener {
val address = tv.text
val content = "geo:0,0?q=$address"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(content))
startActivity(intent)
}
Here I'm using Kotlin, convert java if you want
Related
I am trying to improve accessibility for a text view used for terms and privacy policy which looks something like this:
As mentioned on the Google Support page, the user will need to access the Local Context Menu via a TalkBack gesture (default gesture is Swipe up then right) to activate a TextView link.
But I want to use a swipe gesture to navigate between links something like:
Currently, I am using clickable span to add the links, it is working fine with Local Context Menu but not with a swipe gesture.
My code looks something like this:
val clickableSpan: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
// handle link click.
}
}
val spannableStringBuilder = SpannableStringBuilder(text).apply {
setSpan(clickableSpan, 99, 109, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
}
tv_term_and_privacy_policy.apply {
setText(spannableStringBuilder)
movementMethod = LinkMovementMethod.getInstance()
}
Please help me with this.
I am using the "setMovementMethod" to make clickable an url on a TextView. Better look the follow code:
1. String value = "By signing up, you agree to our My App Term and confirm that you have read our Privacy Policy";
2. TextView text = (TextView) findViewById(R.id.text);
3. text.setText(Html.fromHtml(value));
4. text.setMovementMethod(LinkMovementMethod.getInstance());
The problem is about the slash just after ".com" of the url. If I remove that slash and I write the url like that https://app.mywebsite.com then it works perfectly but when I write the url like that https://app.mywebsite.com/terms then the link isn't clickable. I can see the link highlighted but when click on the link then it does not work
How I could resolve this? Thank you very much.
Firstly,
String value = "By signing up, you agree to our My App Term and confirm that you have read our Privacy Policy";
is illegal because - you are not allowed to use double-quote i.e. " inside another double-quote.
So, the correct form should be:
String value = "By signing up, you agree to our <a href='https://app.mywebsite.com/terms'>My App Term</a> and confirm that you have read our <a href='https://app.mywebsite.com/privacypolicy'>Privacy Policy</a>";
When I do this change, I am able to click the link, or else, the code will not even compile.
Secondly, have a check on the Build version and use the two parameter version of the Html.fromHtml(string, int)
More information on it is here:
Create a function look like:
fun applyHtmlToTextView(tv: TextView?, html: String) {
if (tv == null)
return
tv.movementMethod = LinkMovementMethod.getInstance()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tv.text = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
tv.text = Html.fromHtml(html)
}
}
Add your string to the string.xml file as below:
<string name="lb_your_string"><![CDATA[<a href="https://google.com”>Google.</a>]]></string>
And using it by adding the code:
applyHtmlToTextView(tv, getString(R.string.lb_your_string))
OR you can edit the value variable to:
String value = "By signing up, you agree to our <a href=https://app.mywebsite.com/terms>My App Term</a> and confirm that you have read our <a href=https://app.mywebsite.com/privacypolicy>Privacy Policy</a>"
Removed \"
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"
Basically, i have a list of Nepali Unicode strings something like {"युनिकोड १ ","युनिकोड २","युनिकोड ३"}.
Now, Firstly, I have a text view in Xamarin (Android) and tried to set the text property using couple of methods:
UnicodeTextView.Text="युनिकोड १"; //direct method
var font = Typeface.CreateFromAsset(_activity.Assets, "kantiput.TTF");//kantiput.TTF Is a Nepali font.
UnicodeTextView.Typeface = font;
var font = Typeface.CreateFromAsset(_activity.Assets, "kantiput.TTF");
UnicodeTextView.SetTypeface(font, TypefaceStyle.BoldItalic);
and none of them worked.
When using the first option nothing was displayed, and on working with last two
and there were some BOX character visible.
For first case when i directly tried to set the value:
Before setting value:
After setting value:
Samething with the ListAdapter.
Can anyone suggest me how can we display unicode sentences in TextView, EditText, Toast ?
I want result something like this :
with TextView :
and here is the weird behavior :
and i tried all those code that are in comment too. Still didn't find any luck.
I am working in android.
In toast I checked like the following:
Toast.makeText (this, "\u0c05 \u0c06", Toast.LENGTH_SHORT).Show();
For first two letters of Telugu alphabet s. It worked.
If you have the font file you can obtain unicode codes for the various glyphs in the online software available at
https://opentype.js.org/index.html
Under page glyph inspector.
I tested your code, the first direct method UnicodeTextView.Text="युनिकोड १"; works fine by my side both with single TextView or TextView in ListView.
Or you may try this code:
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.Build.VERSION_CODES.N)
{
UnicodeTextView.Text = Android.Text.Html.FromHtml("युनिकोड १", Android.Text.FromHtmlOptions.ModeLegacy).ToString();
}
else
{
UnicodeTextView.Text = Android.Text.Html.FromHtml("युनिकोड १").ToString();
}
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);`