Android detect and clickable web address in textview - android

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"/>

Related

Multiple clickable links in TextView

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" />

Why my TextView is Scrolling in android?

Why my TextView is Scrolling?
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rl_logo_header"
android:layout_marginTop="5dp"
android:autoLink="web"
android:descendantFocusability="blocksDescendants"
android:ellipsize="end"
android:focusable="false"
android:maxLines="3"
android:text="Should my school be focused on -- Teaching and Learning come as words which are very closely related, But they convey very different meanings and can not be used interchangeably."
android:textColor="#color/black" />
The content for this view could be text and may also contain HTML content. Hence I have set autoLink to true. The issue is, if either the autoLink or the textIsSelectable is true, then the textview starts to scroll similar to(MovementMethod) when its content is more than 3 lines. I am looking for a way to stop/disable this textview scrolling.
I tried to disable the scrolling using setEnable(false) for the text view, however all the links in the textview could not be clicked thereafter.
I think there has to be a straight forward way to achieve "non-scrollable textview" which may contain html content in them.
autoLink controls whether the TextView will parse URLs and highlight them as such. It has nothing to do with rendering HTML.
Your TextView is scrolling because you've set android:maxLines="3"

Android autoscrolling text view

I would like to implement a auto scrolling text view in a native Android app.
Something like a vertically scrolling marquee. The idea being that it looks something similar to an autocue, the text will remain static and scroll once more text has been provided.
I am happy to work out the logic of scrolling the text when new text arrives. I am just looking for ideas on how to have that text feed/scroll effect animation.
Thank you
Maybe start here to make the textview scrollable:
Making TextView scrollable on Android
After that, you should be able to use an object animator, such as:
ScrollTo(0,250) with animation Android ScrollView
Do like:
in XML:
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0.0dp"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:maxLines="5" />
And in java:
textView.setMovementMethod(new ScrollingMovementMethod())
You Just check it and refer this answer.
Android automatic horizontally scrolling TextView
I think you want solution like this. for auto scrolling TextView.
there is many other solutions also which working with programmatic to scroll String in your TextView area.
auto-scrolling TextView in android to bring text into view
Automatic horizontal scroll in TextView
Refer this links and get Solution as per you want.
Thanks for this question's owners and accepted Answer givers.

Non scrollable textview in android

I have a textview configured in my XML as below.
<RelativeLayout>
<!-- more views here like ImageView and other TextView-->
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textIsSelectable="false"
android:autoLink="web|phone|email"/>
</RelativeLayout>
The content for this view could be text and may also contain HTML content. Hence I have set autoLink to true. The issue is, if either the autoLink or the textIsSelectable is true, then the textview starts to scroll similar to(MovementMethod) when its content is more than 4 lines. I am looking for a way to stop/disable this textview scrolling.
I tried to disable the scrolling using setEnable(false) for the text view, however, all the links in the textview could not be clicked thereafter.
I think there has to be a straight forward way to achieve "non-scrollable textview" which may contain HTML content in them.
I think you can add the attribute android:nestedScrollingEnabled="false" to any View child. At least, I use android:nestedScrollingEnabled="true" to make my TextViews scrollable. Hope this solves your problem.
BTW, why are you trying to display a clickable link when you can't even scroll to look at the entire link? Just asking out of curiosity.
set ellipsize to the text view so text can't be greater than 4 lines
may be this is solution for you
Try changing 'Relative Layout' to 'Table layout'... If this doesn't work then try to set appropriate padding and margin for Textview and other widgets.

Set ImageButton text from the code?

In examples found on the net I saw that the text is set from XML file only. I need to attach the text from another View, and I tried to find any setter that I can use to set text to ImageButton. I didn't succeed. I even tried using this
<ImageButton
android:background="#ffffff"
android:text="setText()"
/>
hoping that I can use setText() in the code, but it did not work as well.
How can I set the text for ImageButton programmatically?
Thanks
PS. This is a custom ImageView which inherits ImageView.
ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).
You cannot set text to ImageButton because it has no method as setText() or android:text property.
Here is workaround, using a Button and android:drawableTop / Left / Right or Bottom like this :
<Button android:layout_height="wrap_content"
android:drawableTop="#drawable/icon" android:text="Button"
android:layout_weight="1" android:layout_width="fill_parent"
android:textSize="11sp"
android:layout_margin="1sp" />

Categories

Resources