2

I want to set style to selected text from EditText using custom typeface. I am getting below error at compile time. The constructor StyleSpan(Typeface) is undefined.

Below code I am applying.

int start=editbox.getSelectionStart();
int end=editbox.getSelectionEnd();
Spannable span=(Spannable)editbox.getText();
StyleSpan f = new StyleSpan( 
                            Typeface.createFromAsset(getAssets(),
                             "fonts/genbkbasr.ttf"));
span.setSpan(f, start,end, 0);

Thanks.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Aamirkhan
  • 5,746
  • 10
  • 47
  • 74
  • 1
    Have you looked at http://stackoverflow.com/questions/7338697/android-development-how-to-replace-part-of-an-edittext-with-a-spannable or http://stackoverflow.com/questions/8191414/using-spannablestring-builder-and-rich-text ? – Alin Jan 04 '12 at 12:31
  • Anyone have an answer to this question yet? Alin - those questions and answers do not address using a custom font from the assets folder. – Kyle Clegg Apr 13 '12 at 20:34
  • Hi AamirKhan,please help me how you solve this issue.I try selected answer but not success. – mukesh Oct 31 '12 at 09:10
  • @mukesh i accepted this answer bcs it was a good one but sorry it is not working for me also,i finally applied font style to the entire text not the selected portion of the text,now i have unaccepted that answer,thanks. – Aamirkhan Oct 31 '12 at 09:52
  • thanks AamirKhan.Is any way to set typeface on string not textview or editview. – mukesh Oct 31 '12 at 10:05
  • @mukesh you can use java spannable method,if you still have doubt you can catch me here http://chat.stackoverflow.com/rooms/13436/smart-phones-developer – Aamirkhan Oct 31 '12 at 10:08

2 Answers2

1

I wrote a class to work around this limitation. It appeared to work in limited testing, but I haven't yet written the application that I needed it for. Note that it assumes that the custom font is available as an asset, and it makes a static call to retrieve the application's context (which it needs to access the resource). A better approach would be to pass in the Context to the constructor..

import android.content.Context;

public class TypefaceResourceSpan extends MetricAffectingSpan implements ParcelableSpan {

private String resourceName_;
private Typeface tf_;

public TypefaceResourceSpan(String resourceName) {
    super();
    resourceName_=resourceName;
    tf_=createTypeface(resourceName_);
}

public TypefaceResourceSpan(Parcel src) {
    resourceName_ = src.readString();
    tf_=createTypeface(resourceName_);
}

private Typeface createTypeface(String resourceName) {
    Typeface result=null;
    Context c=TikunKorimMain.getAppContext();
    if (c==null) {
        Log.e("TypefaceResourceSpan", "Application context is null!");
    }
    AssetManager am=c.getAssets();
    if (am==null) {
        Log.e("TypefaceResourceSpan", "AssetManager is null!");
    }
    result=Typeface.createFromAsset(am, resourceName);
    return result;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(resourceName_);
}

@Override
public void updateMeasureState(TextPaint p) {
    Typeface old=p.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        p.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        p.setTextSkewX(-0.25f);
    }
    p.setTypeface(tf_);
}

@Override
public void updateDrawState(TextPaint tp) {
    Typeface old=tp.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        tp.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        tp.setTextSkewX(-0.25f);
    }
    tp.setTypeface(tf_);
}

public int getSpanTypeId() {
    // TODO does this work???!?
    return 123456;
}

public int describeContents() {
    return 0;
}
}
shmuelp
  • 1,296
  • 13
  • 14
  • Mr. shmuelp,please tell me how modify getSpanTypeId() method for setting custom typeface on selected text of editview. – mukesh Oct 31 '12 at 09:36
  • mukesh, I don't know what the method does, but the other classes inheriting MetricAffectingSpan seem to supply it. Returning a placeholder value like I do above appears to work. – shmuelp Nov 01 '12 at 17:50
  • 1
    You should implement a cache for your Typefaces _expecially_ if you're going to create them each time you add a span. Here's an implementation for you: [github gist](https://gist.github.com/kemallette/6837502) – KMDev Oct 05 '13 at 06:44
  • KMDev, thanks for the suggestion and implementation! I never got past using the code above in a proof-of-concept, but I was hoping to get back to that effort soon. – shmuelp Oct 07 '13 at 04:50
-2

Accepted values for this constructor are documented here:

Values should be style constants, like Typeface.BOLD.

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
clemp6r
  • 3,665
  • 2
  • 26
  • 31
  • that means span only can be use with BOLD ITALIC UNDERLINE?and if so than is there any other way to change type face of selected portion of edittext from asset folder – Aamirkhan Jan 04 '12 at 09:16
  • Yes, StyleSpan cannot change the font of a single part of a text, it is made for changing the style only (bold, italic, etc.). A TextView can have only one typeface. – clemp6r Jan 04 '12 at 09:30