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.
Related
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.
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"/>
Is it possible to set an Icon in ActionBar "Text" using FontAwesome?
I have tried this way...
My menu item like that..
<item
android:id="#+id/action_chat"
android:orderInCategory="100"
android:title="#string/action_chat"
app:showAsAction="always"
android:actionLayout="#layout/chat_menu_icon"/>
My 'chat_menu_icon.xml'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.bitmakers.techmonster.textview.FontAusomeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:id="#+id/test"
android:gravity="center|bottom"
android:text="#string/action_chat"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
My Custom Font Class
package com.bitmakers.techmonster.textview;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontAusomeTextView extends TextView {
public static Typeface m_typeFace = null;
public FontAusomeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
public FontAusomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
public FontAusomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode()) {
return;
}
loadTypeFace(context);
}
private void loadTypeFace(Context context) {
if (m_typeFace == null)
m_typeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/fontawesome-webfont.ttf");
this.setTypeface(m_typeFace);
}
}
It is working when i using it in normal textview
You can use a nice library called android-iconify to achieve your goal.
Here is how you implement it on Toolbar/Actionbar items:
your_menu_layout.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="#+id/item_settings"
showAsAction="ifRoom"
android:title="#string/title"/>
</menu>
Inside your onCreateOptionsMenu method:
menu.findItem(R.id.item_settings).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_cog)
.colorRes(R.color.your_color)
.actionBarSize());
And you are good to go :)
Use Custom ActionBar
create your own layout and include that in xml using
<include.../>
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 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)