How do I prepend a String to an Android TextView? - android

There's TextView.append(), but that adds the text to the end of the TextView. I want what I append to go in the beginning (ie, show up on the top of the TextView box).

Have you tried this
textview.setText(" append string" + textView.getText());
Though the spannables will get lost by this method.

If you're concerned about spannables, you can use something like this:
textView.getEditableText().insert(0, "string to prepend");

The getEditableText() returned null to me. Instead, to prepend text to a spannable, I set the text buffertype to SPANNABLE:
in code:
myTextView.setText(myText, TextView.BufferType.SPANNABLE);
or using xml property android:bufferType on your TextView.
Then cast the getText() to Spannable, after which I concat the extra text with existing text:
Spannable currentText = (Spannable) tvTitle.getText();
CharSequence indexedText = TextUtils.concat(String.format("%d. ", index), currentText);
myTextView.setText(indexedText);
As far as I can tell all functions are available from API level 1.

Then use the string you want to append say "hello" to the textview as
textview.setText("hello"+textView.getText())

Related

Extracting and replacing text with a Spannable clickable

#mention functionality like whatsapp
This is a message for :
#9170211268628 hi how are you
Expected :
#Raviraj hi how are you
In onBindViewHolder of your RecyclerView.Adapter<T> implementation, use a SpannableStringBuilder to create a new spannable string with the replaced text then call setText on the text view.
you want to remove number and comma,
first of all you find out index of comma and then separate string using substring(stratindex,endindex) then put name of user after #.

Setting spannable string in android with combination of non-spannable strings

I have several strings uploaded to a String[]. These strings are set on a textview. However, I set the previous strings on my textview, as well. I need some of the strings to be red colored. I've heard of the SpannableString class which does exactly what I want, but the following call defeats my purpose of setting all the previous strings.
if(string from String[] contains ("R2")){//pseudo code
//build spanstring with all its properties
//combinedstrings = combinedstrings + spanstring;
}
else{
//string = string from String[]
//combinedstrings = combinedstrings + string;
}
textview.setText(spanstring, BufferType.SPANNABLE);
As you can see, this call only allows to set the spanstring not the combination of the spanstring including the non-spannable strings. I need the combination of both to be set at the same time.
I need a call that would set the text on the textview of both types of strings
the non-spannable and spannable ones.
thanks for any suggestions
Build the CharSequence you pass to setText with a SpannableStringBuilder. See https://developer.android.com/reference/android/text/SpannableStringBuilder.html This class allows you to append either string or spans and can be built into a single CharSequence.

TextView: set Html and own SpannableStringBuilder

I have a string that contain html tags. So for set text into TextView I use:
textView.setText(Html.fromHtml(myString));
It's works good.
Also I have own method convertText() with type SpannableStringBuilder. In which method I convert text, where I have something like [text="information"]. I use next for set text:
textView.setText(convertText(myString), TextView.BufferType.SPANNABLE);
For example: myString = "<h1> This is [text="information"]. </h1>"
The result must be:
This is INFORMATION. (with h1 formatted text. caps lock of word information works in method convretText() good)
It's also works good. But how I can use these two setText approach in one time?

Hide only part of text in TextView

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);

Specifying "strikethrough" on a section of TextView text

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 ?: ""
)
}
}

Categories

Resources