Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change the android keyboard text font programmatically?

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".

like image 239
Mike Armstrong Avatar asked Jul 08 '15 20:07

Mike Armstrong


People also ask

Which API can be used to change the text font?

Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio.

How do I change the writing style on my mobile keyboard?

Tap where you can enter text. Your keyboard will appear at the bottom of the screen. the language you want to use. Swipe right and turn on the Handwriting layout.

Can Android customize keyboard?

Go to Android Settings > Languages and input > Current keyboard > Choose keyboards. You should see your Custom Keyboard on the list.


1 Answers

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>
like image 96
Ankush Bist Avatar answered Oct 30 '22 11:10

Ankush Bist