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
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 have Helvetica Neue.ttf in asset Folder , How to set the Helvetica Neue textStyle on My Entire Applcation.
There is currently no way to do this with the Views that come with the Android SDK. You can set your View to use any of the Roboto fonts as per this answer, but you cannot set a custom font.
The way I typically tackle this problem is to create my own TextView that uses my font, like so:
public class MyFontTextView extends TextView {
public static final String FONT_PATH = "fonts/MyFont.ttf";
public MyFontTextView(Context context) {
super(context);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
/**
* Set up the font.
*/
private void initFont() {
if (!isInEditMode()) {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), FONT_PATH);
setTypeface(font);
}
}
}
You replace all of your TextViews with this TextView, and then you will have your font. Note that other UI elements (e.g. Buttons) will still use Roboto unless you also customize those.
If you have a View that you only use once in your application, you could call setTypeFace() on that View instead of creating a custom View. The custom View method works well for Views that you use a lot in an application such as TextViews.
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.
So there are different ways to set your font in Android.
What i need to do is to set the font in combination with a custom font and thereby over the entire app.
Now after some research i have 2 options.
Override EditText and set font, use this class in my layout xml.
Add a font to my AppTheme in my styles.xml.
Now i prefer the last option, this is obviously in my position the best way to go since i use the same font in my entire app.
Now the problem is, in my Assets/font/ folder i added the font i want to use but i can't use this in my styles.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface"> Here? </item>
</style>
How to add my font as typeface. Is that possible?
Try this code:
public class CustomTextView extends EditText {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode())
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode())
init();
}
public CustomTextView(Context context) {
super(context);
if (!isInEditMode())
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), Constants.FONTPATH);
setTypeface(tf);
}
}
Then Use in XMLwith ;
<pkgname.CustomTextView android:id="#+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" />
As far as I know there is no way we can access external assets in xml files.