6

To be frank the title says it all really, I'm trying to change an input method service, KeyboardView key font. Of course.. it's not as simple as

android:keyTextFont="sans-serif".

Attaullah
  • 3,856
  • 3
  • 48
  • 63
Mike Armstrong
  • 174
  • 1
  • 9
  • Have a look at [this][1] and [this][2]. Should solve your problem! [1]: http://stackoverflow.com/a/4949412/4127441 [2]: http://stackoverflow.com/a/11718016/4127441 – rgv Jul 08 '15 at 20:27

3 Answers3

5
import java.lang.reflect.Field; 
import android.content.Context;
import android.graphics.Typeface;

public final class FontsOverride {

public static void setDefaultFont(Context context,
        String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typeface.createFromAsset(context.getAssets(),
            fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

protected static void replaceFont(String staticTypefaceFieldName,
        final Typeface newTypeface) {
    try {
           final Field staticField = Typeface.class
                .getDeclaredField(staticTypefaceFieldName);
           staticField.setAccessible(true);
           staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
  }
}

add this class to your code.

public final class Application extends android.app.Application {
@Override
public void onCreate() {
    super.onCreate();
    FontsOverride.setDefaultFont(this, "DEFAULT", "fonts/GeezEdit.ttf");
    FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/GeezEdit.ttf");
    /*FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
    FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");
    FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");*/
  }
}

..... here you can see that the some fonts/fontname are added. These are the external font files which you can use to overrite into your keyboard view/labels.

add this Application name into the android manifest file application name

example

 <application
    android:name=".Application"
    android:allowBackup="false"
    android:installLocation="internalOnly"
    android:label="@string/ime_name"
    android:theme="@style/AppTheme" >.......

now update the above overrited font name into your style. base theme or the theme which you are using at your manifest application.

example

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:typeface">monospace</item>
</style>
Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
0

Yes, it can be done pragmatically by overriding KeyboardView's onDraw() method.

Here is an example of someone adding text above keys: Customize the appearance of a <Key>

To change text font code would be something like this:

public class MyKeyboardView extends KeyboardView {
@Override
public void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    Typeface font = Typeface.createFromAsset(context.getAssets(),
        "fonts/Hippie.otf"); //Insert your font here.
    paint.setTypeface(font);

    List<Key> keys = getKeyboard().getKeys();
    for(Key key: keys) {
        if(key.label != null)
            canvas.drawText(key.label.toString(), key.x, key.y, paint);
        }
    }
}

Code not tested

Community
  • 1
  • 1
Phillip
  • 255
  • 1
  • 12
  • Sorry I'd like to re-iterate, I\m explicitly using InputMethodService, for which there isn't an override of onDraw instead there's just onCreateInputView which cannot take Canvas canvas. – Mike Armstrong Jul 09 '15 at 14:33
  • Are you inflating a KeyboardView within the onCreateInputView? – Phillip Jul 09 '15 at 16:34
0

You have to create a new class that extends KeyboardView.

package com.example.xyz;
...
...
public class CustomKeyboardView extends KeyboardView{
    @Override
    public void onDraw(Canvas canvas) {
        Paint mPaint = new Paint();
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTextSize(40);
            mPaint.setColor(Color.BLACK);

            Typeface font = Typeface.createFromAsset(mContext.getAssets(),"normal_font.ttf");
            mPaint.setTypeface(font);

            if (key.label != null) {
                String keyLabel = key.label.toString();
                if (caps) {
                    keyLabel = keyLabel.toUpperCase();
                }
                canvas.drawText(keyLabel, key.x + (key.width / 2),
                        key.y + (key.height / 2) , mPaint);
            } else if (key.icon != null) {
                key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
                key.icon.draw(canvas);
            }
    }
}

Where the .ttf file is the font file from the Assets folder of your project.

Now, in the keyboard layout (.xml) file, replace the <KeyboardView/> with <com.example.xyz.CustomKeyboardView/>

Prasad Pawar
  • 1,606
  • 1
  • 15
  • 30