I'm trying to add a TextView with the classic you accept our terms and conditions and privacy polcy text with web links to the ToS and Privacy Policy but got stuck trying to get the links to work. I am able to create the links but they do nothing when clicked. I have tried many solutions, but none seem to work.
How would you go about making multiple links work properly?
Make sure to set the correct MovementMethod to make links clickable.
Java
textView.setMovementMethod(LinkMovementMethod.getInstance());
Kotlin
textView.movementMethod = LinkMovementMethod.getInstance()
You can add android:autoLink="web" in TextView attributes
<TextView
android:id="#+id/textview"
android:autoLink="web"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Related
I know there has been A LOT of talk here regarding justifying text in Android. But Android O just came out and a new feature came out with it, Text Justification. They say use the final JUSTIFICATION_MODE_INTER_WORD included with the TextView class. Along with setJustificationMode(). it does work when I use it with the English language, but it fails to work when I use it with the Hebrew language. I have it set up to:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:text="#string/ahahvaRabba"
android:textDirection="rtl"
/>
I also tried to put android:gravity="right" but that didnt work either.
I cant imagine that google didn't think of using it with other languages.
I'm a newbie to Android and I have looked at similar questions asked by others without a definite answer, since, I think, my problem is a bit different.
I'm using a TextView in my program in which there can be links, text, numbers etc. following is the TextView I'm using.
<TextView
android:id="#+id/viewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:autoLink="web"
android:linksClickable="false"
android:ellipsize="end"
android:maxLines="7" />
Although this is working for normal texts, the ellipsize does not work whenever links are present in the TextView. I'm using "autoLink" in order to show the user that it is link but had set android:linksClickable to false. Right now, I've added the ellipsize from the code but I want to know whether I can do it from the XML file itself.
Thank you.
I am working on textview, I want to do that if there is any web link in textview so it will detect that and also clickable. How can I achieve that?
There are two main ways - xml which does most work for you (see #Massimo answer) and code which is very flexible, it allows you to make some text clickable and intercept link clicks (see LinkMovementMethod)
Set the autoLink and linksClickable attributes in your xml's TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:autoLink="web"/>
I've read through both of these questions, which seem to have the same problem i'm struggling with, namely I have text for my buttons that i'd like to display as a superscript. In my fragment class in the onCreateView method I have added
if (rootView.findViewById(R.id.squared) != null) {
((TextView) rootView.findViewById(R.id.squared)).setText(Html.fromHtml("X <sup><small> 2 </small></sup>"));
rootView.findViewById(R.id.squared).setOnClickListener(this);
}
if (rootView.findViewById(R.id.cubed) != null) {
((TextView) rootView.findViewById(R.id.cubed)).setText(Html.fromHtml("X <sup><small> 3 </small></sup>"));
rootView.findViewById(R.id.cubed).setOnClickListener(this);
In my fragment_main the buttons are coded as
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="#+id/squared"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="#+id/cubed"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
However, when I run my application the layout doesn't any superscripted text.
Even when I change the text size from 15sp to 10sp, the text only gets smaller, but not superscripted. What am I not doing correctly?
I'd comment if I could, but at this point I can't. I JUST posted something similar to this, an issue I'm having as well. I had my problem solved halfway. The method Html.fromHtml works for me only on a textview, but not on my butons. I just wanted to say this in case it helps you or anyone else figure out how to employ this method on the buttons. I'd very much like the answer myself.
Basically:
textview.setText(Html.fromHtml("x<sup>2</sup")); displays properly
BUT button.setText(Html.fromHtml("x<sup>2</sup")); displays x2 (without superscript).
EDIT: AlanV helped me out. Here's a link to the solution he provided someone else. https://stackoverflow.com/a/29056795/4535064
All you need to do is add this to your button:
<button
android:textAllCaps="false";/>
The new version 5.0 (I think) forces buttons to produce all caps. This is something I noticed when I produce the same string in a textview and button, and the button was all caps whereas the textview was not. Because of this, you don't see the HTML formatting such as and . Setting the textAllCaps feature to "false" allows the HTML to be utilized.
Again, giving credit where it's due, AlanV is the man! :-)
This is a known issue, as shown here.
I think the best thing to do is to set android:textAllCaps="false" , and then to set the text to be capitalized by yourself.
I have looked through many of the threads on this site to find the solution to my problem but none seem to work. At the moment I am able to create a hyperlink while displaying the website eg - Please go here - www.google.com (The www.google.com is hyperlinked). What I am trying to do is have the link as - Please go here (Here is the link). Below is the code i have tried but once I remove the link itself, the 'here' still highlights like a link but has no function.
Strings.xml:
<string name="goog">Please go here</string>
fragment_home.xml:
<TextView
android:id="#+id/npd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:autoLink="web"
android:fontFamily="sans-serif-light"
android:linksClickable="true"
android:text="#string/npd"
android:textColorLink="#FF0000"
android:textSize="12dp" />
If i change 'here' for www.google.com within Strings.xml, the link takes me directly there. Has anyone been able to figure this problem out yet? Thanks in advance.
Try this and let me know is it what you are looking for
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));