4

I am creating an android button for reload. It want to have both icon and text in the button so I am using fontawesome. But how do I apply fontawesome font and custom font I am using in my app at same Time on the buttons text.

Sar009
  • 2,166
  • 5
  • 29
  • 48
  • Use spans, see http://stackoverflow.com/questions/6612316/how-set-spannable-object-font-with-custom-font – dnkoutso Apr 25 '14 at 06:16
  • please R&D on internet before post question here - save disk space - save earth. :P – Devendra Dagur Apr 25 '14 at 06:19
  • @dnkoutso i tried it but its not working its giving me fontawesome unicode. – Sar009 Apr 25 '14 at 06:24
  • @dnkoutso I had to convert the unicode code to string(http://stackoverflow.com/questions/11145681/how-to-convert-a-string-with-unicode-encoding-to-a-string-of-letters) then apply the your comment – Sar009 Apr 25 '14 at 06:36

2 Answers2

8

This is how I achieved it and used CustomTypefaceSpan Class from https://stackoverflow.com/a/10741161/992665

public class MainActivity extends Activity {

    Typeface tf_r, tf_icon;
    Button reload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int hexVal = Integer.parseInt("f021", 16);
        SpannableStringBuilder SS = new SpannableStringBuilder((char)hexVal + " Refresh");

        tf_r = Typeface.createFromAsset(this.getAssets(), "www/fonts/Roboto-Light.ttf");
        tf_icon = Typeface.createFromAsset(this.getAssets(), "www/fonts/fontawesome-webfont.ttf");

        SS.setSpan(new CustomTypefaceSpan("", tf_icon), 0, 1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SS.setSpan(new CustomTypefaceSpan("", tf_r), 1, 9,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

        reload = (Button) findViewById(R.id.no_openings_reload);
        reload.setText(SS);
    }
}
Community
  • 1
  • 1
Sar009
  • 2,166
  • 5
  • 29
  • 48
2

try below code:-

String text = "This is an example";

Spannable s = new SpannableString(text + " text");
s.setSpan(new TypefaceSpan("monospace"), 0, text.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new TypefaceSpan("serif"), text.length(), s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

((TextView)findViewById(R.id.my_text_view)).setText(s);

or

TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

or

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");
textfield.setTypeface(tf,Typeface.BOLD);

see below link for more info :-

http://www.androidhive.info/2012/02/android-using-external-fonts/

http://www.tutorialspoint.com/android/android_custom_fonts.htm

duggu
  • 37,851
  • 12
  • 116
  • 113
  • buddy please read the question i am asking to apply two different fonts on a string not about applying a single font on complete string – Sar009 Apr 25 '14 at 06:26