replace a character from only font style bold - android

I'm new to android and I'm trying to replace a character from bold style at a specific character in a arraylist string. What I'm doing is:
String myName = "76492";
This is my search string and i want to search this string in to the arraylist then i want to bold those character when getting on the arraylist.Arraylist string is 78758,3660,56798,6514,90679.
TextView txtf = (TextView)findViewById(R.id.textView4);
TextView txtS = (TextView) findViewById(R.id.textView5);
TextView txtT = (TextView)findViewById(R.id.textView6);
TextView txt1 = (TextView) findViewById(R.id.textView9);
TextView txt2 = (TextView) findViewById(R.id.textView13);
String str= myName.replaceAll("6", "<b>6</b>");
Spanned text = Html.fromHtml(str);
txtf.setText(text);
txtS.setText(text);
txtT.setText(text);
txt1.setText(text);
txt2.setText(text);
But this is replace all Textview String by the 76492 string with match charater bold style. 1879 search string this is replace old string but i donot want this type replace .iI want only specific position character change in bold style

<style name="boldText">
<item name="android:textStyle">bold</item>
</style>
Paste this code snippet in your style.xml which is in valuse folder under layout.
txtf.setTextAppearance(this, R.style.boldText);
This line of code is for your text view appear as a bold.

Use spans. Then you can specify parts of your string and assign any format or color you want. Have a look at this.
Edit: For clarification: With spans you don't replace the characters. Use some string method to find the position of the characters you want to change to bold and create a span on these positions and assign bold format to that span.

Related

Can i change span Text in android

Here is my code,i want to put two type of fonts in my Textview. I can successfully implement a code to do that.My problem is i want to edit the variable text without changing the arrangement.
String text = " my font1 , font 2 ";
Typeface font,font1;
font = Typeface.createFromAsset(getAssets(), "font.TTF");
font1 = Typeface.createFromAsset(getAssets(), "font1.TTF");
Spannable s = new SpannableString(text);
s.setSpan(new CustomTypefaceSpan("", font), curpos, endpos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new CustomTypefaceSpan("", font1), curpos, endpos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
.
.
.
.
//want to chage variable text here !
I reffered the following but i don't get the solution.
Set color of TextView span in Android
Android: Coloring part of a string using TextView.setText()?
TextView with different textSize
Try using the SpannableStringBuilder class. "This is the class for text whose content and markup can both be changed." It sounds like you need a mutable class.
It's unclear precisely how you want to change the text, but I use SpannableStringBuilder like this to insert additional text at the beginning of some text that is html. Once you have your string builder object there are sufficient methods available to modify things.
Spanned spHtml = Html.fromHtml(myHtmlString, Html.FROM_HTML_MODE_LEGACY);
SpannableStringBuilder sb = new SpannableStringBuilder(spHtml);
sb.insert(0, "text to be inserted");
Can you try something like this :
public void updateTextView(String toThis) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
// Put here your span, ...
}
And call this function when you want update your textView

Unicode to text in java

How can I convert a Unicode string like
"\u0646\u0627\u0645"
to a human-readable string that I can put in a TextView?
No need to do anything more than this
TextView text = (TextView) findViewById(R.id.yourTextView);
text.setText("\u0646\u0627\u0645");

How to set custom font for word in a TextView text

I want to set custom font for a word which is appears any where in a string.
Ex : Hai #abc how are you ? , Hello my dear friend where you #abc
In the above examples, i have to apply custom font for "abc". I tried with Spannable class, but i am unable to get it.
final TextView txtComments = (TextView)view.findViewById(R.id.tv_comments);
SpannableStringBuilder SS = new SpannableStringBuilder(alcomments.get(i));
SS.setSpan (new CustomTypefaceSpan("", tf), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
txtComments.setText(SS);
But it is affecting the entire string. Please guide me how to achieve it.
you can manage this using html or set custom font style in textview.
1) if this scenario is in rare case then go with set html text like this,
TextView txtComments = (TextView)view.findViewById(R.id.tv_comments);
txtComments.setText(Html.fromHtml("Hi<font color=""#FF0000"">abc</font>"));
else set custom font like this.
2)
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf");
txtyour.setTypeface(type);
First Part Not Bold BOLD rest not bold
String normalBefore= "First Part Not Bold ";
String normalBOLD= "BOLD ";
String normalAfter= "rest not bold";
String finalString= normalBefore+normalBOLD+normalAfter;
Spannable sb = new SpannableString( finalString );
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(normalBOLD), normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
sb.setSpan(new AbsoluteSizeSpan(intSize), finalString.indexOf(normalBOLD), normalBOLD.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//resize size
to show this in TextView
textview.setText(sb);
Refer : https://stackoverflow.com/a/10828482/2480911
If you want to apply custom text then
//Give Font path here. In my case i put font in asset folder.
String fontPath = "bankgthd.ttf";
// text view label
final TextView txtComments = (TextView)view.findViewById(R.id.tv_comments);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);

Trouble setting color with setSpan(new ForegroundColorSpan(),int,int,int)

everyone. I am trying to set the color of a few words in a string using spannablestring.But for some reason I cant get the color of the string to change.
I have attached the relevant code
String color = "some string"
SpannableString span_color= new SpannableString(color);
span_color.setSpan(new ForegroundColorSpan(Color.GREEN),0,11,0);
String print = "This is the whole string"+span_color;
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(reminder);
In the Text View, I want the "This is the whole string" to be in the default color and the "some string" to be in green. but this is not happening.I am getting the entire string in the default color.I seem to be missing something important , but I cant figure out what .
Any suggestion?
Using "+" operator you get just string, not SpannableString. You should do something like this:
String color = "This is the whole string, color string";
SpannableString spanColor = new SpannableString(color);
spanColor.setSpan(new ForegroundColorSpan(Color.GREEN), color.indexOf("color string"), color.length(), 0);
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(spanColor);

Any way to set span directly to spanable text?

This may be a blunder,But I need to know... :)
Iam develeoping an android application,In in I want to display two type face in a single textview and found this One very useful,A custom typeface span that extends typeface span,
As per my understanding , we are creating a spannable as follows using two words
String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
then sets two type face to that words using our custom span like this
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.
WHAT I NEED IS, DIRECTLY APPLY THE CHANGE TO TEXT WHAT THE SET SPAN DO TO THE TEXT
Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?
Thank you for all
Very late answer but what you asked for is pretty easy.
For instance, lets say we want to change everything between quotation marks into italic font.
String s = "\"Hello my name is Edward\" Hola, my nombre es Edward";
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(text);
Spannable spannable = new SpannableString(s);
while (m.find()) {
int textLocation = text.indexOf(m.group(1));
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);
}
textbox.setText( spannable );
Result of s would be
"Hello my name is Edward" Hola, my nombre es Edward
Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;
And you could also remove the tags afterwards with a simple .replace();

Categories

Resources