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);
Related
I'm trying to make an array of strings individually clickable within a text view that is within a RecyclerView (each tag would pass different data, which is fetched from the api on load). I've created the string using SpannableStringBuilder as below within the bindView method.
fun bindView(link: PostsModel)
val tags = link.topics
var spans = SpannableStringBuilder()
for (tag in tags) {
val string = SpannableString(tag.name)
string.setSpan(ClickableTags(tag.name), 0, tag.name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spans.append(string)
}
}
Then I set it to the text view.
view.headerTags.setText(spans, TextView.BufferType.SPANNABLE)
If I println() the contents of spans and view.headerTags.text, I can see it contains a string of tags, so it seems to be working. However, when testing in the app, it's not appearing in the text view.
If I set view.headerTags.text = "Tags should appear here", it works, so I'm not sure there's a problem with the text view.
Can't see to work out why it wouldn't appear, especially if the console is printing out the contents of text view? Can anyone let me know what I might be missing here?
Please use
Spannable word2 = new SpannableString("By signing in, I agree to The xxxxx\nxxxxxxx Terms of Service and Privacy Policy.");
word2.setSpan(clickableSpan, 44, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
word2.setSpan(clickableSpan1, 65, 80, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(word2);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent = new Intent(SignInActivity.this, ReadTermsOfServiceActivity.class);
intent.putExtra("FROM", "termsservices");
startActivity(intent);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
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.
I am making a Mad Libs game of sorts for practice, and so I will have things like EditTexts so the user can input data.
However right now the only way I can get this to sort of work is if I use a horizontal LinearLayout and then do something like a TextView followed by an EditText followed by another TextView, but then if the user enters something long in the EditText, the text does not "wrap nicely" but rather it squishes the rightmost TextView. This is to be expected, but it is not my end-desired behavior.
Is there a better way to put EditTexts inside of TextViews?
Just have one TextView and then you can use a ClickableSpanand even add multiple ClickableSpan(s) to a given TextView. This gives you the effect of multi-line TextView with certain spots acting like an EditText.
If an object of this type is attached to the text of a TextView with a
movement method of LinkMovementMethod, the affected spans of text can
be selected. If clicked, the onClick(View) method will be called.
SpannableString madLibString = new SpannableString("Our school cafeteria has really adjective food");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Toast.makeText(getApplicationContext(), "The text 'adjective' has been clicked. Do something.",
Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
// NOTE: set styling here to give UI feedback that this is text that can be clicked and edited.
ds.setColor(getResources().getColor(android.R.color.holo_red_dark));
}
};
madLibString.setSpan(clickableSpan, 32, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(madLibString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
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.
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());
}