Intercept link LinkMovementMethod with a Yes/No dialog - android

I have a standard LinkMovementMethod established in my TextView to push a web Activity of some sort when the user touches a link. However, I want to establish a "do you want to see the link" dialog rather than taking the user straight to the webpage. I've tried overriding the touch methods but it all gets a little convoluted. A little help?

You can accomplish it in two ways:
Create custom Spans: more complicated, but you can accomplish more customised text consisting of clickable parts (or bold, differently coloured etc). To know more, check out ClickableSpan and SpannableStringBuilder
Extend LinkMovementMethod to accept custom click listener
In my opinion second solution is better in basic cases like yours. Here is how you can do it:
Copy this java class: InternalLinkMovementMethod to your project
Add set the link movement method of your TextView to this custom one, providing a click listener:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
#Override
public boolean onLinkClicked(String linkText) {
// here you can handle your click, eg show the dialog
// `linkText` is the text being clicked (the link)
// return true if handled, false otherwise
}
}
yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener));

Related

Android. How to get substring from Spannable which was clicked?

I have html string:
htmlString = "This is a link and another link link2"
I set this String to my TextView. Here is my code:
tvText.setText(Html.fromHtml(htmlString));
And also I set LinkMovementMethod, links to be clickable.
tvText.setMovementMethod(LinkMovementMethod.getInstance());
Everything is fine, but links open in the browser by default, and I need to open them inside the application in web view.
Can I somehow get the link from the clicked String ? So that I can load the link in web view.
Or do I need to manually search for all links in the String and put clicks on them (ClickableSpan)?
Please, help me.
Here is how you can do it:
Extend LinkMovementMethod to accept custom click listener
Copy this java class: InternalLinkMovementMethod to your project
Add set the link movement method of your TextView to this custom one, providing a click listener:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
#Override
public boolean onLinkClicked(String linkText) {
// here you can handle your click, eg show the dialog
// `linkText` is the text being clicked (the link)
// return true if handled, false otherwise
}
}
yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener)
Base on: Intercept link LinkMovementMethod with a Yes/No dialog

Android web link in textView inside ListView

I want to create a link in text view.
My link look like so:
The text to the link I get from array.xml
<item>Icons made by Freepik</item>
I already read set movement method
textView.setMovementMethod(LinkMovementMethod.getInstance());
This has no impact
and
android:autoLink="web"
This works if the text is http://www.freepik.com, but not if I want to have a custom text as link.
viewHolder.textView.setClickable(true);
viewHolder.textView.setText(text);
viewHolder.textView.setMovementMethod(LinkMovementMethod.getInstance());
This is a code which I am using to fill textView
I want in the end text looking like so:
Icons made by Freepik
I think you can't accomplish what you want in this way.
I think the simplest solution is to separate your links in differents list items. Keep in mind that you could use different TextView with different heights for example
Alternatively you could pass to a custom view approach. If you create a custom view (for example MultiLinkView), then you could add this view to the ListView.
I suggest this solution because this approach allow you to add a powerful logic to the view item.
I can't give you the complete code because it should be too long, but I can put you in the right way.
A custom view is a real Java class that extends some Android view class. So when you instantiate a CustomView you can pass to its constructor all the params you want (references, links, arrays and so on).
Start here
My idea is to find a way to pass all the parameters you need to your custom view and then find a way to represent your data, mapping them to your links.
I think you should abandon html solution in favor to ClickableSpan.
This is a piece of code that I used in a project to make clickable a single part of my string:
String text = "Hello <b>click me!</b> to go to internet!";
// create Spanned
Spanned spanned = Html.fromHtml(text);
// create SpannableString
SpannableString spanString = new SpannableString(spanned);
// set clickable part
ClickableSpan clickablePart = new ClickableSpan() {
#Override
public void onClick(View textView) {
if (connectionDetector.isConnectedToInternet()) {
// open browser or webview fragment
}
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(Color.WHITE);
}
};
int startClickMe = spanString.toString().indexOf(text);
spanString.setSpan(clickablePart, startClickMe, text.length() + startClickMe, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Obviously in the onClick you should find a way to get the right link, but, as I said before, in a custom view you can put as many variables as you want. I'm sure that you can find a solution.
Let me know if it helps

Listview Custom Adapter with Button Tags

I have been experiencing a problem with my custom adapter and have located the problem solution. However I do not know how to go about it. I have researched and seen a few examples of a listview custom adapter at which buttons are given tags like viewHolder.button.setTag(Tag) and I understand what the tag does but I am unsure as to how to use it. My questions are: When I set the tag on a button, how does the application differentiate my buttons from another if all the tags are set the same? Also, say I have an onClick method in my custom adapter, how do I use the tag that I set to the button to identify the button that was clicked? I've kind of seen similar adapters on the internet but not exactly , a link to an example would be greatly appreciated also.
I am unsure as to how to use it
tag is a mechanism to make your views remember something, that could be an object an integer a string or anything you like.
My questions are: When I set the tag on a button, how does the
application differentiate my buttons from another if all the tags are
set the same?
i do not understand this question but i think if you notice that your button has a memory and it calls tag you can use it in a better way. if all of your buttons memory(tags) are the same so you can not use tags to distinguish the buttons you must use ids.
say I have an onClick method in my custom adapter, how do I use the
tag that I set to the button to identify the button that was clicked?
you must set different tags for your buttons or grouped them logically and set different tags for each group then in your onClick method use the tags to identify your buttons group:
OnClickListener myButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Object obj = arg0.getTag();
if(obj instanceOf groupOneTagObject){
// do action for group 1
}else if(obj instanceOf groupTwoTagObject){
// do action for group 2
}
}
});

AlertDialog vs Spinner vs ListView

I've got what I thought was a simple android UI design problem but I've been going around in circles for a couple of days. I have a REST service that I'm downloading XML from and displaying the XML in a form in an android app. I have a web page built and am mimicking this with android, same options, same URLs being sent to the REST service whether from android or the web pages. With HTML I can easily create checkbox groups and radiobutton/dropdowns for various id/display items, so for instance, I can display a planet option as:
<select name="planet"><option value="0">Mercury</option></select>
I wanted to do something similar in android where I had a pair of values, one an id and the other the user-friendly text to display. So I decided to create an adapter using android.util.Pair:
public class PairView extends Pair<String, String> {
public PairView(String first, String second) {
super(first, second);
}
public String toString() {
return second;
}
}
public class PairAdapter extends ArrayAdapter<PairView> {
}
So now I can put my id in pair.first and what to display to the user in pair.second.
My problem comes in that some of these options will be single-selects and some will be multi-selects. In html, that's not an issue, just use a checkbox group for multi, and radio buttons/dropdowns for single selects. In android however, it seems it's not so straight forward. I tried using Spinners for the adapters, but Spinner seems to only allow single selection. AlertDialog.Builder allows for single and multi-selections, but curiously I don't see an option for using an adapter for the multi-selection, just for single selections.
I guess what I really want is a consistent look for all my options, with radio buttons displayed for single selections and checkboxes displayed for multi selections, via an adapter so I can get the id's from the Pair for the items selected.
What approach should I use? A custom spinner with code added for multi-selections? AlertDialog.Builder and somehow make it use an adapter for multi-selections? Just create a plain Alert and wrap a ListView in it? Another option that is (hopefully) simpler?
I feel like I'm missing something very basic here.
I had a similar situation in an app I was making so would share what I opted for. I had different type of questions and depending on that I removed and added things in my activity. For radio buttons I used with elements in it. For multiple choice questions I wanted a checkbox based view so I added an empty within my layout and in code added CheckBox(s) to it.
As for the caption and value, for radio buttons and checkboxes you can set display text by setText and add any object/value as a tag. So what I used to do was something like this:
CheckBox option = new CheckBox(MyActivity.this);
option.setText("Option 1");
option.setTag(10);
Later on when you get the selected option, you can simply get its tag and use its value.
This is just one way of doing it which I found simple. Hope this helps

Text selection listener in Android(API level 7)

I need a listener which is called every time the selection in an EditText changes.
I googled around but I couldn't find anything useful for API level 7.
I'm writing a Text Editor and I want the bold/italic/underlined button appear selected every time the user selects bold/italic/underlined text.
Pretty old question, but someone might still need this, so here's my solution : since the text selection accomplished with long press on the text, I simply used the following :
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
// do whatever you need to do on text selection
}
});
This allows for custom behavior on text selection and doesn't prevent the user from copying/pasting.
The better way to do it would be to extend the EditText and then based upon how you would want to manage the changing text, you could override one of the 2 methods to work out your customized behavior.
If you want the selection changed then you could use the onSelectionChanged() method and implement your code there.
In case you want to implement something when the text changes in your editor then you could use, onTextChanged().

Categories

Resources