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");
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"
/>
i want use all the component in android which having the same font type face, for that i am creating a individual custom class for each component like CustomTextView, CustomEditText, etc,..
So instead of creating a individual class for each component can i create a view CustomView class that will automatically apply style for all the components in android
Just declare your own TextView and use it in your XML, it should appear in the custom Views
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(), "chalk_board.ttf"));
}
Oh dam u want it globally for all views, so this is the wrong approach.... Sorry for that
You have at least 2 ways:
create your own TextView class and set fontFace in constructor
you can use custom LayoutInflater. And every time view gets inflated check that it is textView (or other view not extending textView but having font settings) and set correct fontFace settings.
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
I have developed a very huge application and now i have a requirement of having custom font for all controls in the application. so I want to know the better way to change the font in one shot. The application has more than a hundred XML layout. and i cant change all controls to a custom component with custom font. Please provide a solution to Change the font without altering all the controls in XML.
Do something like this
pacage com.prac;
class MyFontedTextView extends TextView {
public FontedTextView(Context context) {
super(context);
init();
}
public FontedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
this.setTypeface(font);
}
}
Now replace this all over in xml file from your TextViews
<com.prac.MyFontedTextView .... instead of <TextView
This change you have to do all over for it to apply
also for the case of button text . Button is also subclass of TextView
So the same can work for button's too
Hope this help or can lead you to the solution you are looking