How to set a custom font in the button in the XML code? For example comic sans MS

StackOverflow https://stackoverflow.com/questions/19786732

  •  04-07-2022
  •  | 
  •  

سؤال

I am a noob. I've seen some of the solutions here, and that just bounced above my head. Can anyone please help me setting up a font in the button? Also, if I can make it italic, bold etc. Here's my XML code

  <Button
    android:id="@+id/button1"
    android:layout_width="60dp"
    android:layout_height="40dp"
    android:layout_alignBottom="@+id/tableLayout1"
    android:layout_alignRight="@+id/relativeLayout1"
    android:background="@drawable/crea"
    android:textSize="30sp"
    android:textColor="#ff0000"/>

لا يوجد حل صحيح

نصائح أخرى

You cannot use comic sans MS without purchasing a license:

https://docs.microsoft.com/en-us/typography/font-redistribution-licensing

There are free similar fonts available:

https://fontlibrary.org/en/font/comic-relief

Basic operation in using a different font is to add the font file to the assets folder in the project then use setTypeface on the widget:

Typeface font = Typeface.createFromAsset(getAssets(), "ComicRelief.ttf");
Button bt=(Button) findViewById(R.id.button0);
bt.setTypeface(font);

Full info on using fonts here:

https://tekeye.uk/android/examples/android-textview-edittext-font

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top