How to add hyperlink for Label in Xamarin.Forms - android

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"

Related

Displaying Unicode string in Users control in Xamarin (Android)

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();
}

How could I create a custom textview which has specifed distance to its left when meet some characters,for instance ,like '#'

I don't know how to solve this issue,there are some strings needed to fill in the textview,"#Tony#Tom#James#Brown...",what I want to do is like following "#Tony #Tom #James #Brown..." this will be displayed in the textview,the spacing is also specified,for example,"20dp" is the distance between '#Tony' and '#Tom'.I don't know how to create a textview likes above,I have a another question,when the content is out of range,the content should start from next line,so do I deal with this problem?Thank you anyone who gives an answer. If I didn't discribe this question in detail,forgive me.
You can use https://android-arsenal.com/details/1/2242 .
<com.apradanas.simplelinkabletext.LinkableEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
// find username
Link linkUsername = new Link(Pattern.compile("(#\\w+)"))
.setUnderlined(false)
.setTextColor(Color.parseColor("#D00000"))
.setTextStyle(TextStyle.BOLD)
.setClickListener(new Link.OnClickListener() {
#Override
public void onClick(String text) {
// do something
}
});
more information is available on the android arsenal page

how to add an action to part of a string 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);`

AutoLink link square brackets of URL in TextView

I have a url:
https://<site name>/pallavi/[Songs.PK]%2002%20.mp3
I have a text view, with property: android:autoLink="all"
If I simply set the text to the text view, my text view simply highlights the portion preceding the [. It looks something like this:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What i want is, the whole link should be highlighted like:
https://< site name >/pallavi/[Songs.PK]%2002%20.mp3
What I have tried till now:
Used the < pre > tag and Html.fromHtml, but it doesn't seem to work! (I don't even know if the < pre > is supported in android though.)
Used Jsoup.parser. But that too doesn't seem to work for me.
UPDATE
I have tried this answer too: https://stackoverflow.com/a/12376115/1320263
Please let me know if the issue is with android that the text view's linkAll property itself does not consider parenthesis as a valid character or not? If it is supported, how do i hyperlink that too?
Also NOTE:
The text(or link) I have written in the question is just a sample text. In reality, I am getting a block of text, from where it would be very difficult to identify where exactly the hyper link starts and where it ends. Also, the number of links present in the block would be un-known. Hence I cannot use the < a href = "" > thing...
If some one else happens to have the same issue, following is the solution which worked for me:
Pattern pattern = Pattern.compile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");
SpannableString spannable = new SpannableString(html);
Matcher matcher = pattern.matcher(spannable);
// Create ActivitySpans for each match
while (matcher.find())
spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Create a new TextView with these spans and enable the clickable links
mTxtEventDescription.setText(spannable);
You can try this:
((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));`

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 .

Categories

Resources