I want to create a hyperlink to textview in android. So in my layout I have given android:clickable="true" to the textview. Previously it worked for my other layouts. But now it is not working. When I am clicking on the textview it remained as it is.What may be the problem?
Please help me.
Have you tried Linkify class? Here is explanation from android developers. Also android:autoLink property of TextView might be helpful/
You can use textview.setOnClickListener. This is useful for you may be.
Related
I have used textView.setTextIsSelectable(true) or android:textIsSelectable="true" on textview. It is working somewhere but on some textviews it is showing the below error.
W/TextView: TextView does not support text selection. Action mode cancelled.
I have tried
android:focusable="true"
android:focusableInTouchMode="true"
Also on textviews but it is also not working, can you please help me to figure out why it is working on some textviews and not working on others.
Thanks a lot in advanced.
As answered by Yojimbo here:
If TextView, have you tried textView.setTextIsSelectable(true);
Or if EditText, editText.setSelectAllOnFocus(true);
Hope this is Help you.
What I would like to achieve is an effect that looks like this with a TextView:
Basically having a background, but keeping the space between the lines. The only solution I came up with was using one TextView for each line of text, but I would prefer a cleaner one using only one multiline TextView.
Any ideas?
Use spanned text for each line. Read the Spannable API.
please refer to this answer here as it describes how to implement spacing between multiple lines in a TextView , using the following properties :
android:lineSpacingMultiplier
android:lineSpacingExtra
Hope that Helps .
Regards
What you can do is use mark tag in html to textview.
myTextView.setText(Html.fromHtml("<mark>heading highlighted</mark>"));
The official explanations of TextView is Here
i tried to find the XML attribute: android:layout_width of TextView, but i cannot find it. Also, all the android:layout_xxx attributes are missing on the web page. does android website forget to put it here? or i miss something on the web page? i know that i can search for the attribute with google. anyhow, the "android:layout_xxx" is a part of TextView, and the web page should refer to it on the page of TextView. or i would think that "android:layout_xxx" isnt a part of TextView XML attributes.
You can find it here: https://developer.android.com/reference/android/R.attr.html#layout_width
Google doesn't forget them, but put them into another place. In the TextView page, there is a link to the xml attributes.
It's in the ViewGroup.LayoutParams documentation. And all the layout_ attributes are documented in corresponding LayoutParams classes.
I need to put a link in a TextView, I have a string that contains the tag Text for link and some other text.
The problem is that if I run the project I can see the text but it's not clickable. I tried with the <b> tag too to see if that works and it seems that it doesn't work too.
How can I make this to work without the Linkify usage?
Thank you for your help all.
I have managed to make this work, after I have found some examples in the android samples.
here is the code:
textView.setText(Html.fromHtml(
"<b>text3:</b> Text with a " +
"link " +
"created in the Java source code using HTML."));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Hope this help others...
Getting links working from html is kind of tricky:
Apply your text via xml android:text="#string/… or via setText() (see other answers)
Use textView.setMovementMethod(LinkMovementMethod.getInstance()) to make links clickable (see other answers)
Do NOT add android:autoLink="web" to you XML resource (section TextView), otherwise A-tags are not rendered correctly and are not clickable any longer.
Remark 1:
The OnClickListener can be helpful, if your TextView contains only one link and you want to trigger the navigation even if the user clicks aside your link, but inside the TextView.
Remark 2:
android:linksClickable="true" still does not work (as of Android 3.2), use p. 2 instead
Linkify is the class you must use to create links. BTW, what is the reason for not using Linkify?
You can linkify all text in your textview for actions like visiting a website or calling a phone number based on the schema. Android provides the easiest way to do it. Consider the below code.
TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);
For creating custom links, the same Linkify class provides various options.
Google has published a blogpost on this .
I couldn't figure it out, but finally it started working when I did something like:
tvTermsOfUse.setText(Html.fromHtml(getString(R.string.tv_terms_of_use_html)));
Linkify.addLinks(tvTermsOfUse, Linkify.ALL);
tvTermsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
Text view looks like:
<TextView
android:id="#+id/tv_terms_of_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textAlignment="gravity"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="15sp" />
and string res:
<string name="tv_terms_of_use_html">
<![CDATA[This is link to Google.]]>
</string>
Important part:
The Linkify.addLinks has to be done before tvTermsOfUse.setMovementMethod, otherwise it won't work.
No other settings are necessary in XML.
It took me around hour to figure it out myself, hope it helps someone.
EDIT:
According to #rfellons comment
Thanks. Also for me works ... BUT only with
<uses-permission android:name="android.permission.INTERNET"/>
on Manifest.xml. –
rfellons Sep 7 at 13:31
Make sure you check it as well.
Use
android:linksClickable="true"
android:autoLink="web"
I can't reply to your answer for some reason; I just wanted to say that you can omit the textView.setText and just put it in a string resource, and set that using android:text. You just need to keep the textView.setMovementMethod(LinkMovementMethod.getInstance());; unfortunately android:linksClickable="true" by itself does not work.
To add the links dynamically (fetched from the server), this works:
textView.setText(Html.fromHtml(
"<a href=" + response.getLink()
+ ">" + context.getString(R.string.link_from_server) + "</a> "));
and in XML add this:
android:linksClickable="true"
If your strings.xml has this:
<string name="link_from_server">Dynamic Link</string>
This will add "Dynamic Link" to your text view and if you touch on this, it will go the link provided by your server.
The Solution : Linkify.addLinks(chatText,Linkify.ALL);
This works pretty correcty:(In textview properties,inside xml file)
android:autolink="web"
I would like to create an android widget with a scrollable textview.
The solutions given to this question
Making TextView scrollable on Android
cannot be applied because it is a widget:
1.
This
findViewById(R.id.textview).setMovementMethod(new MovementMethod());
does not work, since findViewById is not available in AppWidgetProvider but only in Activity.
2.Putting a ScrollView around the TextView also does not work, because I get an
InflateException:
android.view.InflateException: Binary XML file line #23: Error inflating class ScrollView
Can anybody give me a hint, how to make a TextView in a Widget scrollable?
I solved this problem by putting the text view inside ListView with single node, which is supported in App widget.
http://developer.android.com/guide/topics/appwidgets/index.html#collections
It looks like this is not possible.
More on this can be found here: http://code.google.com/p/android/issues/detail?id=9580
and here:
How to make a scrollable app widget?
So, probably it is possible to do make appwidgets scrollable in the HTC sense environment but not for normal android environments.
A way around this is to add buttons that go up and down in a list.
I have been working on this with two buttons which increment and decrement through an array. Just having trouble accessing the global variables. Did you make any headway on this?
Another solution is:
Add to textview any web link - for example: www.google.com.
Setting text value with HtmlCompat.fromHtml method:
textView.setText(HtmlCompat.fromHtml("some text" + "\nwww.google.com", HtmlCompat.FROM_HTML_MODE_COMPACT));
After that vertical scrollbar is appeared.
But it's not elegant and full solution. It's temporary workaround maybe...
The complete full solutiion is bat-el-g 's answer - with adding ListView.
Current marked solution (which just tell: "it's not possible") - is wrong.
In mainactivity.java:
after code text view:
TextView txt = (TextView) findViewById(R.id.textView id);
Enter this code:
txt.setMovementMethod(new ScrollingMovementMethod());
and you will be ok.