6

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.

Cilan
  • 13,101
  • 3
  • 34
  • 51
koti
  • 1,071
  • 3
  • 15
  • 35
  • sorry my mistake, i misunderstood your question. – InnocentKiller Apr 04 '14 at 13:05
  • You can use spannable string http://stackoverflow.com/questions/15408225/text-with-custom-font-and-bold-style and this http://stackoverflow.com/questions/16335178/different-size-of-strings-in-the-same-textview/16335416#16335416 – Raghunandan Apr 04 '14 at 13:09
  • Please check this [answer](http://stackoverflow.com/questions/6612316/how-set-spannable-object-font-with-custom-font) – Rethinavel Apr 04 '14 at 13:10

2 Answers2

1

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);
Krunal Indrodiya
  • 782
  • 2
  • 7
  • 19
1
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);
Community
  • 1
  • 1
kaushik parmar
  • 805
  • 6
  • 19
  • Just change the content while copy and pasting, what do you mean by `You can do this either as @Rajesh suggested or by this.` no need of this line here. – InnocentKiller Apr 04 '14 at 13:13
  • @InnocentKiller . I was editing my answer and by mistake pressed submit. :) Hope this will work for this question. It works in my code. – kaushik parmar Apr 04 '14 at 13:19