How to change android keyboard key font? - android

How can I change the default font for keys of keyboard I am writing in android (Eclipse)?
Thank you

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.

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);
}

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.

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.

Related

I can't apply Typeface programmatically for Google font in Android Studio

I added a Google font using Android Studio,
It created a 'font' folder and under it is an XML file as follow:
res -> font -> font_name.xml
I'm trying to apply it programmatically but I can'y find how to do it,
I've tried several codes but nothing worked, examples:
Typeface font = Typeface.createFromFile("font/font_name.xml");
or
Typeface font = Typeface.createFromAsset(getAssets(), "#font/font_name");
Please note,
The 'font' folder along with the 'font_name.xml' file,
Were created automatically by Android Studio.
Your help would be appreciated,
Thank you.
I am certainly not an expert, but I managed to get my Typefaces working.
Firstly, this thread contains a lot of useful info:
Custom fonts and XML layouts (Android)
Now specifically to your problem, the way I managed to get Typefaces to work is by downloading a .ttf file either somewhere online, or when using android studio and clicking more fonts, set the radio button to add font to project. That will download the .ttf to res\font\font.ttf Move it to your assets directory into fonts folder and then create the typeface like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
If you want to use your font often and don't want to bother setting it every time, a good idea might be extending a TextView or whatever class you need and using the .setTypeface() method in the custom constructor.
Just solved this myself, here is what I have put together.
Typeface font = Typeface.create("cursive", Typeface.NORMAL);
Log.e("Setup", "Font: " + font.getStyle());
signatureButton.setTypeface(font);
The log just checks if you have selected an acceptable font, it should return 0 if not. The hardest part was finding a font family that worked. The key is to go to your XML file and type "android:fontFamily=" from here a suggestion list should pop up with choices at the bottom.
To change the google font family
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);
Add that to your fonts folder then use them in the XML or programmatically.

Custom ttf fonts are not showing properly in TextView on Android 4.4 KitKat

I have some text which has some bolded parts. Until KitKat this strategy (as mentioned in this post) worked perfectly well
My strings resources file:
<string name="multi_style_text">NON-BOLD TEXT \n<b>BOLD</b></string>
My application code in fragment:
txtView.setTypeface(FontUtils.getOstrichRegular(this.getActivity()));
...
public static Typeface getOstrichRegular(Context context) {
return Typeface.createFromAsset(context.getAssets(),
"fonts/ostrich_regular.ttf");
}
Currently (in KitKat), the bolded part is not shown in the custom font, the non-bolded part is shown in the custom font. In previous versions of Android, all of the text was shown in the custom font.
What gives?
So, after being frustrated by this bug, I searched around and found a solution to the problem.
In my current project we use calibri.ttf font. that was working fine up to 4.4. Once i got the update to my nexus 4, All the TextViews with Calibri font were showing "ff" instead of the entire text.
THE FIX - get an .otf (open type font) version of your font, and put in the project, works like a charm. Too bad google didn't inform the developers on this and there's very little documentation on the matter.
Apparently this is a bug in KitKat and has been fixed in an internal tree.
Put your custom font in android assets under folder name "font" or whatever you want
Try this
myTypeface = Typeface.createFromAsset(this.getAssets(),
"fonts/<<your font>>.ttf");
in onCreate() then
[use youcontroll].setTypeface(myTypeface);
Best of Luck...
I resolved the problem by converting my file.ttf to file.otf
remplace :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.ttf");
yourTextView.setTypeface(typeface);
by :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.otf");
yourTextView.setTypeface(typeface);
FYI : the .otf format work for all android version (not only on kitkat)
After many hours searching for roboto.otf (2014 year) I understood that it was a mistake. Simply download a normal ttf font from https://www.fontsquirrel.com/fonts/roboto-2014 and copy to assets folder, then use setTypeface.
Also you may convert it to otf with any web-site.

Android - Fonts in Application

I have added an external font in /assets directory, and manually doing setFacetype(font).
Isn't there a general way to set the whole application to use a specific font if you have added it external? Or do you have to use Android's selected fonts in order to achieve this?
You cannot use your custom fonts through to whole application in a general way.
You cannot set your custom fonts through xml files.
You have to use the Typeface functions in your code to use your custom fonts within your application.
tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "MYFONT.TTF");
tv.setTypeface(font);
This also how to use it in a textview.
For whole application go to Using a custom typeface in Android.
and go to Manish Singla answer
Typeface mTypeface = Typeface.createFromAsset(getAssets(), "YOUR FONT NAME");
textview.setTypeface(mTypeface, Typeface.NORMAL);

How to return to default font in TextView

I know how to change the font of a TextView to my own font:
catTextView = (TextView)findViewById(R.id.CatText);
catTextView.setTypeface(Typeface.createFromAsset(getAssets(), "my_font_in_assets"));
However at a point during the app execution, I would like to set it back to the default font. I don't want to hardcode the font (write "Droid Sans") since the default font could change in future versions of Android (I think it actually did in ICS).
Is there any way to get the default font name of a TextView, and set it back to this after changing it?
Also, can I be sure the default font will always be able to show Russian or Japanese characters?
Thanks!
Use setTypeface(Typeface.SANS_SERIF) on a TextView
Have you tried calling getTypeface() in your TextView before you change it and store the result somewhere?
http://developer.android.com/reference/android/widget/TextView.html#getTypeface()
Use this:
textView.setTypeface(null);
You can restore default typeface using
Typeface.defaultFromStyle(R.style.YourTheme);

In android How to applying font for whole application?

I want to apply some font say Times New Roman to only my application.
Not to the whole system and not to specific view. As far I know
to apply font to specific view we store font file in asset folder and get into the application as follow.
1] Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/samplefont.ttf");
textView.setTypeface(mFace);
2] To apply font to whole application I can replace the DroidSans.ttf file with my font file.
I can use first way to apply font to my application but It won't be a good solution because I need to modify everywhere and I don't want to do that if there is any better way is available.
I made a custom textview widget and in the constructors made a call to this code:
public static void SetCustomFont (TextView t, String fontName, Context c) {
Typeface tf = Typeface.createFromAsset(c.getAssets(),
fontName);
t.setTypeface(tf);
}
I'm using the same font over the whole application so I put the fontName in the constuctor and then did a global Find/Replace for TextView
Android does not provide much in the way of support for applying fonts across the whole app (see this issue). You have 4 options to set the font for the entire app:
Option1: Apply reflection to change the system font
Option2: Create and subclass custom View classes for each View that needs a custom font
Option3: Implement a View Crawler which traverses the view hierarchy for the current screen
Option4: Use a 3rd party library.
Details of these options can be found here.
Check this to define fonts styles. In addition, you can refer to this style from your manifest file at the app level
<application
android:theme="#style/Theme1">

Categories

Resources