How can I change the default font for keys of keyboard I am writing in android (Eclipse)?
Thank you
4 Answers
One solution is to use keboardView.java instead of android.inputmethodservice.KeyboardView
.
You also need to change paint.setTypeface(Typeface.DEFAULT_BOLD)
to paint.setTypeface(my font)
and you must add attrs.xml to your project.

- 10,749
- 6
- 46
- 77
I found an answer : Implemented onDraw...
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
try{
onBufferDraw();
}catch(Exception ex){
}
if (mBuffer!=null)
canvas.drawBitmap(mBuffer, 0, 0, null);
}

- 498
- 1
- 17
- 35
-
Could there be a better way? This approach looks like too much work and involves modifying a lot private vars/methods which could easily break in future Android versions. Might as well go through route of using images through "keyIcon" as it appears to be the cleaner way? – pm_labs May 10 '13 at 23:55
-
@paul_sns I wished there was a better way but now I am used to it. I haven't seen too much change in newer versions. – Heidar May 11 '13 at 04:37
-
could you extend the answer please? I quite don't get how the onDraw would help change the font. – Raykud Feb 03 '15 at 17:20
-
@Heidar can u share a sample project where u have used this method to overcome the issue – Fay007 May 18 '15 at 09:15
if you are thinking to change the font style of android custom keyboard keys font style with an external .ttf font style then you can check my answer at a link answer to change the font style of key label of android custom keyboard and also to change the font style throughout the android application
this answer is verified by me personally so you can trust and check this.

- 1
- 1

- 1,862
- 1
- 15
- 32
Well that's a very broad question. I can tell you how to set a different Typeface; how you work that into your keyboard application is up to you.
Place a font (.ttf or .otf) into your assets folder, and use the following code (assuming a font called "myfont.ttf" and a TextView with an id of "key"):
Typeface myFont = Typeface.createFromAsset(getAssets(), "myfont.ttf");
TextView key = (TextView)findViewById(R.id.key);
key.setTypeface(myFont);
Reminder: Don't forget to check the license for the font you are using. Most do not allow redistribution without compensation. One freely licensed font you can use is Bitstream Vera Sans.

- 133,643
- 45
- 263
- 274
-
Thank you for the response, but I am changing SoftKeyboard sample and I don't have any TextView to set Typeface to it. I just have an InputMethodService, a Keyboard, a Key and a KeyboardView. – Heidar Jan 28 '11 at 13:12
-
Interesting. Yeah, looking into it more, I don't *see* a way to modify it. However, apps like SwiftKey have a different typeface for their keys, so it's possible. Unfortunately I don't have a better answer for you. – Kevin Coppock Jan 28 '11 at 16:03
-
One solution is to place an image for each key, but it's time consuming. Thank you anyway and please tell me if you found anything. – Heidar Jan 29 '11 at 17:10