Hyperlink in android studio - android

There is a button "CONTACT ME". New activity opens when this button is clicked. I want to provide my 'linked in' page, 'quora' page, and 'gmail id' in that. How can I do that?

In Contrast to the three texview method you could also try it with a single textview
that has some text with the clickable words in it.
SpannableString ss = new SpannableString("My Linkedin page , quora page ");
ClickableSpan span1 = new ClickableSpan() {
#Override
public void onClick(View textView) {
// Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse ("https://your.link.com"));
// yourActivity.startActivity(browserIntent);
}
};
ClickableSpan span2 = new ClickableSpan() {
#Override
public void onClick(View textView) {
// do another thing
}
};
Now set the clickable parts in the text like:-
ss.setSpan(span1, 3, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(span2, 20, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
by passing the starting index and last index of the clickable texts.
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Welcome to Stack Overflow, you may need to provide more info in the future for you questions.
If you just need to know how to open a link in android you can do it like this:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://your.link.com"));
yourActivity.startActivity(browserIntent);
So you'd want to call that block of code in the onClickListeners of whatever buttons you want to link to something.

What you have to do is that in the activity you are navigating to, you provide three Textviews with the full links to the respective accounts and in each of those Textviews, you set the property 'android:autoLink="web" '. Those views will open the links to your accounts when clicked.

Related

How to make links among other text focusable for accessibility

I have a CheckedTextBox whose text is made up of two SpannableStrings, one of which is a URLSpan.
My question is, how do I make so that a user can move the accessibility focus through each span, eventually focusing on the URLspan itself? setMovementMethod and setLinksClickable doesn't seem to work for me.
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString label = new SpannableString(getString(R.string.label));
label.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.text_color_primary)), 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(label);
SpannableString link = new SpannableString(getString(R.string.link);
link.setSpan(new URLSpan(mUrl), 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(link);
builder.append(".");
mCheckedTextBox.setText(builder);
//The following two methods do not work for what I'm trying to accomplish:
mCheckedTextBox.setMovementMethod(LinkMovementMethod.getInstance());
mCheckedTextBox.setLinksClickable(true);
I've checked on the Android Accessibility Help documentation and it seems that as long as the user hears a chime and can see the link in the local context menu, it is sufficient. However, I wanted to see if I can go that extra mile to enable the user to scroll and focus to the link portion of the text.
https://support.google.com/accessibility/android/answer/6378148?hl=en
Any help would be appreciated.
This isn't supported by the platform. Android's own documentation directs users to open the Local Context Menu to access links in TextViews:
https://support.google.com/accessibility/android/answer/6378148?hl=en
Maybe this can be the easiest solution..it worked for me.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ClassroomLog.log(TAG, "Textview Click listener ");
if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1){
// Perform your action here
}
}
});

Setting clickable string - Textview (Android)

My code looks like this,
String content = "Hi this is #Naveen. I'll meet #Peter in the evening.. Would you like to join #Sam??";
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(content);
Before setting the text in the textview, I would like to add click event for #Naveen, #Peter and #Sam.. When the user taps on these texts I want to open a new intent.. Is that possible? Any pointers would be quite helpful.
You can try to use Linkify with custom patterns.
However, if that doesn't suit your needs you can try this:
SpannableString ss = new SpannableString("Hi this is #Naveen. I'll meet #Peter in the evening.. Would you like to join #Sam??");
ClickableSpan clickableSpanNaveen = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Do Stuff for naveen
}
};
ClickableSpan clickableSpanPeter = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Do Stuff for peter
}
};
ClickableSpan clickableSpanSam = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Do Stuff for sam
}
};
ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(ss);
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
You can use the Linkify class, which provides methods for adding links to TextViews. You can use the addLinks(TextView textView, Pattern pattern, String scheme) method, which needs you to specify the TextView, a Pattern for the text you want to match and a custom scheme that will be used to match Activities that can work with this kind of data. The Activity that you want to be opened when clicking on links must declare this scheme in its intent-filter. Hope this helps.

Launch an activity from portion of text by url

I'm new to android. I was wondering how can make a portion of text act like a HTML link tag but instead of a webpage, it launches an activity.
In my text view i have need to put link to other activities, the problem is my text view can have different text depending on the user request and each text has its own links.
U can use this code for adding click listener to the text view and then you can do whatever you want in the onClick() method.
SpannableString content = new SpannableString("this is spannable string");
content.setSpan(new ClickableSpan() {
#Override
public void onClick (View widget)
{
Toast.makeText(Activity.this, "my text", Toast.LENGTH_SHORT).show();
}
}, 0, content.length(), 0);
text.setMovementMethod(LinkMovementMethod.getInstance());
text.setFocusable(true);
text.setText(content);

Android: add "internal" links to part of a TextView, that links to action in my code

As the title explains, I'd like to add links to my TextView, with these two caveats:
I want the link to act on a part of the TextView, not the full one (something like an A anchor in HTML).
I want the link to point to an action in my code, not a website. I could define a method in my activity, or implement an OnClickListener, and execute that when that specific link is clicked.
So far, I succeeded to turn phone numbers, addresses, web sites and emails into dedicated external links using:
Linkify.addLinks(message, Linkify.ALL);
I'd like something similar for internal links (to my method), with the possibility to define custom ones.
Also, using a web page with internal link and a web view is not really an option, as I already have several complex layouts defined, and having to modify the whole application and concepts would be quite a pain...
Any idea?
EDIT: Kabuko gave me a very good solution, here is exactly how I implemented it:
final TextView descriptionTextView = (TextView) findViewById(R.id.description);
final Spannable span = Spannable.Factory.getInstance().newSpannable("the full text for the view");
span.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(StartEventActivity.this, "LINK CLICKED", Toast.LENGTH_SHORT).show();
}
}, 1, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // 1 and 20 to be replaced with actual index of start and end of the desired link
descriptionTextView.setText(span);
descriptionTextView.setMovementMethod(LinkMovementMethod.getInstance());
If you wanted to actually go to URLs you could use Html.fromHtml, but if you want your own click handlers you can use a ClickableSpan.
In completion of previous post this might help some one
TextView textView = (TextView) getView().findViewById(R.id.textView);
textView.setText(Html.fromHtml(getString(R.string.html_)) , TextView.BufferType.SPANNABLE);
String tmp = ((Spannable) textView.getText()).toString();
String linkText = getString(R.string.html_link);
int index = tmp.indexOf(linkText);
if(index>=0) {
Spannable spannable = (Spannable) textView.getText() ;
spannable.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
try {
/// do what you must
}catch (Exception ex){
handleException(ex);
}
}
}, index, index+getString(R.string.html_link).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}

Android - make whole area hyperlinked instead of just text?

Would really appreciate some help with this.
I have an app which consists of a gallery, text and icons. What i want to do is similar to the android market (example can be seen below). In the android market a whole area is hyperlinked rather than just the text. Hold your finger on one of the apps and it will light up green. That whole area is a hyperlink. Like below:
Looking to get something like this: http://img269.imageshack.us/img269/691/09droid3295x440.jpg
Is there anyway i can do that to my app? I have a very similar app, the gallery is right at the top with icons down the left and text besides each icon. I'm a bit of a newbie so i'm using nested linearlayouts with textviews and imageviews in each one.
Is there a way that it can be done? Thanks for any help in advance!
As i misunderstood the question i'll give the correct answer here. If i understand right, you want to have an clickable View (in this case a LinearLayout) :
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutId);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Or any view for that matter (could be a Button, TextView, etc).
you could do the following to achieve this:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"
+ url));
}
public void updateDrawState(TextPaint ds) {
// override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Where text is your CharSequence.
Link not working
To answer ur query, create the link on the layout rather than text

Categories

Resources