I have to display some text in textview.
I just want the a part of the text to be clickable and the rest of the text to be normal.
Here is my code:
TextView mTextView = (TextView) findViewById(R.id.txt);
String mText = "some text.Link to click.Some more text";
SpannableString sb = new SpannableString(mText);
sb.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com/"));
startActivity(in);
}
}, mText.indexOf("Link"),mText.indexOf("Link") + 13,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(sb);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
If you are putting the full url into the TextView you can use android:autoLink="web" to greatly simplify this process.
See the TextView docs for more
Another option is the Html object. Specifically the fromHtml() method I think will allow you to achieve what you want. You should be able to do something like this:
mTextView.setText(Html.fromHtml("blah blah some text to be linkified blah blah"));
You could also use the linkify option: http://android-developers.blogspot.com/2008/03/linkify-your-text.html - In this case, you can just build your string dynamically, and make only the part of it you want clickable.
I had never heard of the autoLink attribute - cool.
Another option, which might be closer to your original question - you could create two different TextViews next to each other (if the spacing is odd, use layout_marginRight="-8" - using a negative offset, which may require a bit of tweaking to get the spacing perfect). Then you could apply the autoLink attribute to only the TextView you want to be clickable.
Related
Let’s say you have the string:
"https://google.com to go to google. https://amazon.com to go to Amazon."
In a TextView, how would you replace the url’s with a link that shows “Click here” (or “Haz clic aquí” in spanish) and would take you to the correct url?
Keep in mind that the text is dynamic, it’s retrieved from the API, and there is no way to know if or how many links may be in any given post.
The finished product should look like this:
“Click here to go to google. Click here to go to Amazon.”
After many many hours…
Here is my solution.
I put this code inside the RecyclerView adapter's onBindViewHolder().
// replace url links with clickable link that says "Click here" (or "Haz clic aquí" in Spanish).
// link color is set in TextView in the xml.
// SpannableStringBuilder is mutable, so we can replace the link.
SpannableStringBuilder spannableStringBuilder = new
SpannableStringBuilder(newsFeedItem.getBody());
// use Linkify to automatically set all Url's in the string.
Linkify.addLinks(spannableStringBuilder,Linkify.WEB_URLS);
//do this process for each Url
for (URLSpan urlSpan: spannableStringBuilder.getSpans(0,spannableStringBuilder.length(),URLSpan.class)){
int start = spannableStringBuilder.getSpanStart(urlSpan);
int end = spannableStringBuilder.getSpanEnd(urlSpan);
// put whatever you want it to say into the next line where I wrote "Click here".
SpannableString customLinkSpannableString = new SpannableString("Click here");
customLinkSpannableString.setSpan(urlSpan,0, customLinkSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.replace(start, end, customLinkSpannableString);
}
// now set the fixed up string into the TextView and set LinkMovementMethod.
textView.setText(spannableStringBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());
In android xml how to underline the string with the spacing? If I using the underscore it will look not nicely because there are some gap between the underscore.
Example below are the underline that have gaps
Example below are the one that I looking for:
Make drawable for underscore line and set between that views.
Or use like this
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(Html.fromHtml("Go<u> </u>Travel"));
The output is :
Go_______Travel
String temp = "Go Travel"
temp=temp.replaceAll(" ", "_"); // return Go______Travel
Note that it is just the simple logic, you can obviously use many other solutions based on your application/business logic.
Hope it helps.
Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);
I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:
PageText = PageText.replaceAll("\\*([a-zA-Z0-9]+)\\*", "<b>$1</b>");
PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");
PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);
So, to bold a word, surround it with *'s, for italics, surround with _.
But, for strikethrough, Html.fromHtml does not support the "strike" tag, so it can't be done this same way. I've seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for - (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I'm wondering if there is a more elegant solution out there.
If it is just TextView you can strike through using paint flags
TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
#Suresh solution works if you want to strikethrough the entire TextView but if you want to strikethrough only some portions of the text then use the code below.
tvMRP.setText(text, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tvMRP.getText();
spannable.setSpan(new StrikethroughSpan(), 3, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Here text is the text which we want out TextView to display, 3 is the no. of characters (starting from 0) from where the strikethrough will start.
You can do it with a custom TagHandler such as the one on this SO question:
Spanned parsed = Html.fromHtml(PageText, null, new MyHtmlTagHandler());
And the TagHandler implements the methods:
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
processStrike(opening, output);
}
}
....
Are you sure Html.fromHtml doesn't support <strike>? It's listed in this Commonsware blog post
It looks like is not really supported, at least it does not work on Android 3.1.
#RMS2 if text is small you can split it into two or three separate text views and apply flag only to the one which you want, not perfect for long texts ;(
Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we essentially have some functions available for the SpannableStringBuilder class.
For example, after instantiating a Builder instance we can use the build methods to append some bold text:
textView.text =buildSpannedString {
strikeThrough {
append(
value ?: ""
)
}
}
Ok right , i asked how to create a random number from 1-100 for android and i came to this
TextView tv = new TextView(this);
int random = (int)Math.ceil(Math.random()*101);
tv.setText("Your Number Is..."+ random );
What this does is create the default kinda "hello world" style text view and says "Your Number Is.... [Then Random Number]
My problem is that i cant change the layout of this text , because it is not defined in XML, if someone could tell me how to change the style , or like make the random number into a string so i could use it for any Textview layout that would be great ..
Thanks :)
If by change the style you mean the text color, text size, and you want to change them programmatically, have a look at the setTextColor and setTextSize methods.
More info here
If you want more advanced formatting to set programmatically, see this link.
The below example demonstrates how to make your text bold and italic.
tv.setText("Your Number Is..."+ random, TextView.BufferType.SPANNABLE );
Spannable myText = (Spannable) tv.getText();
myText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,myText.length(),0);
Edit:
Try the below for the android:textSize="100dp" and android:gravity="center" :
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 100);
tv.setGravity(Gravity.CENTER);
Putting it into a string is easy.
String randomAsAString = Integer.toString(random)
You can then use the XML properties of the TextView to change its formatting, such as android:textSize="30dp" or android:textColor="#900".
By the way, if you're happy with the answer to your previous question, you should go back and mark an answer as "Accepted". That gives 'reputation' points to the person whose answer you accepted and closes the question so that people don't think you're still waiting for a better answer. You can read more about reputation in the FAQ.
Edit:
You can't reference the string entirely in xml while still giving it a random number. This is because the "#string/some_string" format only allows unchangeable strings. The execption to this is using parameters, e.g. setting the string as
<string name="random_number">The random number is %d</string>
Then you could call up that string using something like
yourTextView.setText(this.getString(R.string.random_number, random))
As for your other question about setting a background to a textView, that's also easy.
yourTextView.setBackgroundDrawable(R.drawable.....)
You should take advantage of Eclipse's autocomplete feature... it makes finding these commands a lot easier. For example, simply type the name of your TextView followed by a period, pause half a second for the list of options to come up, then "setB" and it should then filter the list to the three setBackground Drawable/Resource/Color options.
tv.setText(Html.fromHtml("Your number is: <b>" + random + "</b>"));
For basic HTML text-styling tags.
You could also do something like this.
Define your string in strings.xml like:
<string name="your_number_is">Your number is <xliff:g id="number">%s</xliff:g>.</string>
Create a TextView in a layout xml:
<TextView android:id="#+id/your_number_is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/your_number_is"
android:gravity="center"
android:textSize="100dip"
/>
Then your code would look like:
TextView tv = (TextView) findViewById(R.id.your_number_is);
int random = (int)Math.ceil(Math.random()*101);
tv.setText(getString(R.string.your_number_is, random));
This will make it a lot easier when you later on would like to change your text or maybe localize your app.
if you have thead troubles use this:
new Thread(){
public void run(){
TextView v = (TextView)findViewById(R.id.mytext);
v.setText("TEST");
}
}.start();