Android - make whole area hyperlinked instead of just text? - android

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

Related

Hyperlink in android studio

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.

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

EditText inside a TextVIew?

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

EditText's selectAll() does not select the text, instead cursor is moved to position 0

My situation is: I have a EditText, and under it I have a button, called "select all". It's purpose is to let the user select all the text by pressing it.
When the button is clicked, I invoke selectAll() on the EditText, but instead of selecting all text, in some cases (generally, when the cursor is already positioned within the text somewhere), the cursor is moved to position 0 (start of text) and text remains unselected. Second click on the button will then select all. But of course it should happen on first click.
From this issue: Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only it seems that this is a bug in android 4.0 and above. (Found no mention in Google issue tracker).
Does anyone know about a way to overcome this problem? It seems that the probelm is also in other selection methods and not only selectAll().
(p.s. This question is sort of duplicate of the issue I mentioned above. But I opened another question, because the author of that issue was satisfied and selected a partial answer (of setting android:selectAllOnFocus="true"), while in my case and scenario it does not help me and does not solve the problem.
Thanks.
Problem caused by IME. If showed cursor drag pointer then selection must be zero width.
You need cancel drag pointer. It can be doned by change text. For example replace:
Editable text = edit.getText();
if (text.length() > 0) {
text.replace(0, 1, text.subSequence(0, 1), 0, 1);
edit.selectAll();
}
We replace first symbol of text with same symbol. It cause cancel drag pointer and allow make selection without bug.
I was having a similar issue. I solved it (using Xamarin in C#, but the equivalent Java will work) with the following code:
private void InitFocus()
{
Java.Lang.Runnable rable = new Java.Lang.Runnable(()=>
{
EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
maleCount.RequestFocus();
maleCount.SelectAll();
});
new Handler().Post(rable);
}
I have 2 controls: One is a dropdown that lets you select a chicken house and then a set of buttons for each day of the week. Whenever you change the day of week or the chicken house, I want to set focus in the txtMaleCount EditText and then I want the value in that box selected (since it's a number and they're presumably going to replace it).
Clearly, the non-intuitive part was the need to Post it. Doing it directly (on the UI thread) didn't seem to have any effect.
Try this:
yourEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});
Or This:
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});
Hope this helps .. :)

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

Categories

Resources