hi i want set font but i have a problem
i use calligraphy library for changing font
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/IRANSansMobile.ttf")
.build()))
but this get font from Assets folder!
now i want change bottom navigation font but now i cant access Assets folder!
<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">#myfont/iransansmobile</item>
</style>
so this is my question:
1.how can i set font in setDefaultFontPath from res/myfont folder?
2.how can i set font in from Assets folder?
Hello i found this solution from How to use custom font in android xml?
package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
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();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
XML:
<com.vins.test.MyTextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
Good luck.
Related
I changed font in my project for custom one. It's working ok on emulator and many devices, but on some devices it doesn't work in some cases, for example, when I create TextView from code.
I have a sample project that demonstrate this problem.
Here my style file
<resources>
<style name="TextStyle.Caption" parent="TextAppearance.AppCompat.Caption">
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">#font/eesti_pro_display_bold</item>
<item name="android:letterSpacing">0.03</item>
</style>
</resources>
This is my xml. Here I have TextView with textAppearance
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textAppearance="#style/TextStyle.Caption"/>
</androidx.constraintlayout.widget.ConstraintLayout>
My Activity where I create another TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layout: ConstraintLayout = findViewById(R.id.container)
createItemTitleTextView(this.baseContext).apply {
layout.addView(this)
text = "Hello World from code!"
}
}
}
private fun createItemTitleTextView(context: Context) = TextView(context).apply {
TextViewCompat.setTextAppearance(this, R.style.TextStyle_Caption)
}
Result on emulator(Expected result):
Result on Samsung Galaxy J5. Italic font is Galaxy system font. Note that my font is working from xml, but not from code
EDIT
I know that i can use setTypeface and everything is working as expected, but I want to know why setTextAppearance doesn't work. Because I just want to change my styles file, not the whole project and add setTypeface to every TextView from code
TextViewCompat.setTextAppearance(tv, R.style.TextStyle_Caption) doesn't work because I create TextView. However, I need to create AppCompatTextView and everything works as expected
Before Android 8.0, fontFamily only supports default values:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
For Custom Fonts, you need to download ttf file and use it as follows:
Typeface typeface = Utils.getTypeface(context, textStyle);
if (typeface != null) setTypeface(typeface);
and the function in Utils file is:
public static Typeface getTypeface(Context context, int textStyle) {
try {
String font = "fonts/font-file-name.ttf"
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), font);
return myTypeface;
} catch (Exception e) {
Log.d(Constants.TAG, "OpenSansTextView init: ");
e.printStackTrace();
}
return null;
}
The Location of my ttf file:
app\src\main\assets\fonts\font-file-name.ttf
EDIT (Alternate Solution):
If you already have too many TextViews which you cannot add "setTypeface" everywhere in java,
Then, just press Ctrl + Shift + R and replace <TextView with <com.your.package.app.MyTextView.
you can create your custom TextView as:
package com.your.package.app;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
public class MyTextView extends android.support.v7.widget.AppCompatTextView {
Context context;
Integer textStyle = 0;
String textOS;
public MyTextView(Context context) {
super(context);
this.context = context;
init(null);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init(attrs);
}
private void init(AttributeSet attrs) {
try {
String font = "fonts/font-file-name.ttf"
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), font);
if (typeface != null) setTypeface(typeface);
} catch (Exception e) {
Log.d(Constants.TAG, "MyTextView init: ");
e.printStackTrace();
}
}
}
In xml code i can use fontfamily like below :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:fontFamily="monospace"
android:typeface="monospace"
/>
and i can set type face in my acivity as below :
Typeface typeface = Typeface.createFromAsset(getAssets(),"font.ttf");
textview.setTypeface(typeface);
is there any way to set font in xml from Assets() folder to set custom typeFace?
i want to reach some thing like this...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="#assets/font.ttf"
/>
i searched for some thing like that, but i didn't find useful soulution.
STEP 1 :
Create assets folder and in this folder create another folder fonts see below image. After creating these folders paste your ttf files in fonts folder.
STEP 2 : Create attrs.xml file in values folder see below image and paste this code.
CODE
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTextView">
<attr name="font" format="string" />
</declare-styleable>
</resources>
STEP 3 :
Create CustomTextview.class and paste this code
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_font);
try {
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(myTypeface);
}
} catch (Exception e) {
e.printStackTrace();
}
a.recycle();
}
}
}
STEP 4 :
Declare your customTextview in xml file like below.
Use this attribute app:font="your ttf file"
Note -> com.javacodegeeks.androidcustomfontexample.CustomTextView indicate that your package name and class name.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<com.javacodegeeks.androidcustomfontexample.CustomTextView
android:id="#+id/custom_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is a custom text view!"
android:textSize="30dp"
app:font="arial.ttf" />
</LinearLayout>
YOU CAN GRAB DEMO FROM HERE
I hope it help
after launching o , You will define your custom font like this:
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/lobster_italic" />
</font-family>
In XmlFile Look Like This:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/lobster"/>
I wanna use custom font file. For that below is my code
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/custom_font"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the Chantelli Antiqua font." />
</LinearLayout>
Java code:
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
Its working. But I want to use this custom fonts for whole project.
So i have to put in a place where all Class can use it.No need to write for every TextView.
Where and how can I use this custom font.
You can make a custom TextView extend TextView to set custom fonts.
TextViewPlus.java:
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml: (in res/values)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TextViewPlus
android:id="#+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="#string/showingOffTheNewTypeface"
foo:customFont="Chantelli_Antiqua.ttf">
</com.example.TextViewPlus>
</LinearLayout>
You would put "Chantelli_Antiqua.ttf" in the assets folder.
EDIT: Look at these Questions Using a custom typeface in Android
Android: Want to set custom fonts for whole application not runtime
Custom fonts and XML layouts (Android)
Tutorials:
Custom XML Attributes For Your Custom Android Widgets
Using Custom fonts on Android
I'm trying to create a digital clock widget with custom typeface font.
I got clues from here
Solution was by extending TextView and put new attribute for custom font typeface. This is a good solution compared to drawing Canvas and Bitmap solution then pass it to RemoteViews.
I followed every step,
1. create custom class CustomFont.java
2. define style in attrs.xml
3. then put it in main.xml
but I got the following errors
WARN/AppWidgetHostView(18681): updateAppWidget couldn't find any view, using error view
WARN/AppWidgetHostView(18681): android.view.InflateException: Binary XML file line #17: Error inflating class upper.duper.widget.circular.clock.CustomFont
...
...
...
WARN/AppWidgetHostView(18681): Caused by: java.lang.ClassNotFoundException: upper.duper.widget.circular.clock.CustomFont in loader dalvik.system.PathClassLoader
Something missing?
Here I attach the codes
This is CustomFont.java
package upper.duper.widget.circular.clock;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class CustomFont extends TextView {
private static final String TAG = "CustomFont";
public CustomFont(Context context) {
super(context);
}
public CustomFont(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFont);
String customFont = a.getString(R.styleable.CustomFont_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
And this is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:updup="http://schemas.android.com/apk/res/upper.duper.widget.circular.clock"
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<AnalogClock android:id="#+id/AnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="#drawable/widgetdial_white"
android:hand_hour="#drawable/widgethour"
android:hand_minute="#drawable/widgetminute"/>
<upper.duper.widget.circular.clock.CustomFont
android:id="#+id/TIME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:layout_gravity="center"
android:singleLine="true"
android:ellipsize="none"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
updup:customFont="ALPNMAIN.TTF" />
And I put my custom font (ALPNMAIN.TTF) on assets folder already.
This is attrs.xml
<resources>
<declare-styleable name="CustomFont">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Now I feel this is not possible to have something "custom" for app widget.
Look here
upper.duper.widget.circular.clock.CustomFont
You appear to be attempting to use a custom class in a layout for an app widget. This is not supported.
I was trying to make an app with a custom view, and i kept getting "error inflating class".
It must be that I am missing some of the basics when it comes to custom views, but I am not sure what. Here is a very simple program with a custom view, what more is needed to make it work?
(Notes: For the sake of this question, I put SurfaceView class inside of the Activity Class. This was not the situation in the larger application. I do not show the AndroidManifest.xml file here, but it is just what was generated by the wizard in eclipse.)
Here is the java:
package com.mypackage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
public class SimpleCustomViewActivity extends Activity {
class TheView extends SurfaceView{
private static final String TAG = "TheView";
public TheView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG,"TheView(" + context + "," + attrs + ")");
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
TheView v = (TheView) findViewById(R.id.myview);
}
}
Here is file res/layout/simple_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
When u call your own surfaceView class from the xml file u need to add the following public surfaceView creating methods:
public GameView(Context context) {
super(context);
init(context);
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
If u are using the function setContentView(gv) you only need the first one.
in xml it should be:
<com.mypackage.SimpleCustomView.TheView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.mypackage.SimpleCustomView.TheView>
I believe something like this may work though I haven't tested it:
<View
class="com.mypackage.SimpleCustomView$TheView"
id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
: declare two methods and it should be public!!
public TheView(Context context)
public TheView(Context context, AttributeSet attrs)