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
}
}
});
Related
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 trying to use this library for my emojicons https://github.com/ankushsachdeva/emojicon.
I have implemented it with my soft keyboard but it's not doing anything as in the code (MainActivity) of the given link, it uses EmojiconEditText instead of normal text view of any android app. Is there any way so that I can use edit text of any app instead of this custom one. My current source code for emoji is this.
popup = new EmojiconsPopup(mInputView, this);
popup.setOnEmojiconClickedListener(new EmojiconGridView.OnEmojiconClickedListener() {
#Override
public void onEmojiconClicked(Emojicon emojicon) {
if (emojicon == null){
return;
}
emojicon.getEmoji();
}
});
In this, I cannot figure out how to fetch Unicode for that particular emoji. If someone can help me how do I fetch emoji Unicode?
The code for emoji Unicode is in this https://github.com/ankushsachdeva/emojicon/tree/master/lib/src/github/ankushsachdeva/emojicon/emoji
I have got the answer after some struggle. I just need to call that string of unicodes in it with current input connection.
String text = emojicon.getEmoji();
mComposing.append(text);
commitTyped(getCurrentInputConnection());
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 .. :)
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());
}
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