F1! i am working on another activity of android app and in this activity i applied custom font in 8 text views in urdu language, but it take 12 to 15 seconds for displaying this activity and if i remove custom font then it take 1-2 seconds for displaying this activity with by-default font but by-default android urdu language font is not pretty good. Remember that i never used any activity of font in .xml file. Any idea ?
My Java code:
public class JismPics extends Activity {
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jism_pics);
txtView = (TextView) findViewById(R.id.textResult);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/noori.ttf");
txtView.setTypeface(myCustomFont);
}
}
I use custom font in many of the apps I develope and never have that problem. Maybe you can override the views so you don't have to change the font on the onCreate() method
Something like this:
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
public MyTextView(Context context)
{
super(context);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NeoSans.otf");
this.setTypeface(font);
}
}
Don't forget to call your custom class on the layout.
<com.myproject.util.guicomponents.MyTextView
android:id="#+id/txt_name"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/txt_reg_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
/>
Related
I want to change my android app font to custom font and change font size. include all of activities and objects and alert dialog.
How to do this?
figured it out by my self. This is the code I used. I create custom TextView which has custom font as default font.
use Custom Textview to your Andrid Application
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
Custom Font
If you want to change the font in your app then you require
font file i.e roboto.ttf
custom textview where the font has been applied to the text.
I would recommend this library Fontify
Just make a class that extend Application and also mention that class in the manifest of the app using name attribute in application tag.In that class onCreate use the below code.
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "Roboto-Thin.ttf");
I'm developing an android widget app targeting for above Lollipop (Android 5.0) users.
I have to use a font which is called "noto sans"(https://www.google.com/get/noto/#/) in my app for some reasons.
I heard it is contained in Lollipop, but i don't know how to use it in xml.
Anyone knows about it?
No, you cannot use custom fonts in layout file (XML). You have to use the custom fonts programmatically as shown below
String fontPath = "fonts/calibrib.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);
TextView txtView = new TextView(this);
txtView.setTypeface(tf);
Place your font inside assets folder. Here is for what above code is working.
You could create your own TextView by overriding the TextView like this, you have to put the font in the assets folder:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(),
"foo.ttf"));
this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
}
}
And use it like this:
<com.your.project.package.MyTextView
android:id="#+id/oppinfo_mtv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Player 1"
/>
Hi I am using a custom Typeface in my app with the following code:
public Typeface font;
//Activity on create:
font = Typeface.createFromAsset(getAssets(),"DINOT-Medium.otf");
TextView tv = (TextView)vi.findViewById(R.id.textView1);
tv.setTypeface(font);
The problem is after a while it gets buggy: the text can no longer be read and only squares are visible. Do you know why this happens? How can it be fixed?
Thanks
Create a Custom TextView like this :
public class DINOTMediumTextView extends TextView {
public DINOTMediumTextView(Context context) {
super(context);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context);
}
private void setCustomFont(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/DINOT-Medium.otf");
setTypeface(tf);
}
}
put the font's file in assets/fonts/ ( create a folder inside the assets folder)
and then in your layout xml :
<com.yourapp.views.DINOTMediumTextView
android:id="blabla"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
com.yourapp.views is the name of the package that contains your DINOTMediumTextView class.
On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts
EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So
assets/fonts
Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myFont.ttf
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to set font for entire Application?
I need to set a custom font (.ttf format) for whole app, how can i do it?
From Manifest or XML would be the best option if it is possible
EDIT Sept 2014:
For anyone still seeing this old terrible answer, the real, good answer is here:
https://stackoverflow.com/a/16883281/1154026
OLD:
Your best bet would be this:
Customize Android Fonts
So
TextView text = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
With your .ttf in the root of the "assets" folder.
You could do it like this.
Create a custom textview and use that one everywhere
public class MyTextView extends android.widget.TextView
{
public MyTextView(Context context)
{
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
if (this.isInEditMode()) return ;
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
final String customFont = a.getString(R.styleable.SomeStyle_font);
//Build a custom typeface-cache here!
this.setTypeface(
Typeface.createFromAsset(context.getAssets(), customFont)
);
}
}
Add this to attrs.xml
<declare-styleable name="SomeStyle">
<attr name="font" format="string" />
</declare-styleable>
Then in your theme, do this:
This will make sure all the textviews will use the style MyTextView
<item name="android:textViewStyle">#style/MyTextView</item>
And now we can define your custom font using your custom attribute in this style.
<style name="MyTextView" parent="#android:style/Widget.TextView">
<item name="font">MyPathToFontInAssets.ttf</item>
</style>
So whenever you use MyTextView in the project, it will have your custom font.
IMPORTANT: Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache so you can re-use the custom typeface for all textviews. This will significantly speed-up the application!
UPDATE: As Amir was saying, this is almost the same as Custom fonts and XML layouts (Android) but i also use android styling to automatically use it on all textviews in the app.
Cerate a TextView subclass and use it everywhere
public class RobotoTextView extends TextView {
public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public RobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RobotoTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(tf);
}
}
}
usage
<com.test.RobotoTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
in java code you can cast it to TextView
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
put the font file in the fonts folder in /res/
Like my answer if it is helpful...
You can extend the TextView as suggested here and set your typeface on that then use it in whole app.
Due to widget development there is a requrement in definition of my own font (wich can be changed sometimes via SharedProperties).
One fo the soultion i saw here is to use:
TextView tv=(TextView)findViewById(R.id.yearthree_view);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic.ttf");
tv.setTypeface(font);
But the problem is: onUpdate() method dosn't familiar with
findViewById
My approach using RemoteViews to get to TextView in order to update text.
Thanks in advance.
Create a custom TextView extends TextView and set the Typeface when initialize.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initTextFont(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initTextFont(context);
}
private void initTextFont(Context context) {
//Set font here.
}
}
use context.getassets() ;
context is passed in onUpdate
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Century_Gothic.ttf");