I am working on android app, I downloaded font awesome ttf files and added to android studio project. It is not working but if I try with icoomon working fine. Can anyone please help me.
code:
<TextView
android:text="aws"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
app:fontFamily="#font/fa_brands_400"/>
First of all make sure you place your .ttf dowloaded file in your Asset folder.
Then create a class called FontAwesome that extends TextView like this
public class FontAwesome extends TextView {
public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesome(Context context) {
super(context);
init();
}
private void init() {
//Font name should not contain "/".
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fontawesome.ttf");
setTypeface(tf);
}
}
Finally you create your TextView as follow
<PACKAGE_NAME.Fontawesome
android:id="#+id/userLogin"
android:text=" Login Now"
android:clickable="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I hope this helps you.
Related
I have a big library of custom components that I want to use in my Android app.
All components use custom attributes to customize its content.
This is a sample component declared in a XML file:
<myapp.CustomTextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="8dp"
custom:textContent="This is my text" />
This is the custom attributed declared:
<declare-styleable name="CustomTextView">
<attr name="textContent" format="string" />
</declare-styleable>
And this is the implementation of the custom component:
public class CustomTextView extends TextView {
private Context context;
public CustomTextView(Context context) {
this(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
readAttrs(context, attrs);
}
private void readAttrs(Context context, AttributeSet attrs) {
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView,0, 0);
try {
String content = array.getString(R.styleable.CustomTextView_textContent);
initContent(context, content);
} finally {
array.recycle();
}
}
private void initContent(Context context, String content) {
...
}
}
The problem is that I'm using data binding in my app, so if I do this, the app doesn't compile:
<myapp.CustomTextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="8dp"
custom:textContent="#{mainView.content}" />
After reading a lot, the solution seems to be to create a bindingadapter for that custom attribute, but in my case, create a binding adapter for every custom attribute would be a huge work because there are tons of components and attributes.
Is there a way to reduce this amount of work or to adapt data binding to these custom attributes?
How can i change those font attributes of a SwitchPreference?
I already tried to use app:layout attribute with the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#android:id/title"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Title" />
<TextView
android:id="#android:id/summary"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Summary" />
</LinearLayout>
That doesn't work well, because the switch was missing and everything looks a bit messy.
What is an easy (and working) way to customize the fontfamily and textSize?
You need to create a Custom Switch Preference by extending its main class
public class CustomSwitchPreference extends SwitchPreference {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
#Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView title1= (TextView) holder.findViewById(android.R.id.title);
title1.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
TextView title2= (TextView) holder.findViewById(android.R.id.summary);
title2.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
}
}
and use this is in your preferences xml
from
<SwitchPreference
.....
/>
to
<com.example.myapp.CustomSwitchPreference
....
/>
This Worked for me
To change the font family in android first you need to have the font in your app something like this
Your font goes in the font directory under res
Now to use it, your going in the right way
<Button
...
android:fontFamily="#font/nameOfFont"
... />
Let me now if it works for you
like the title says, I need to use a new font. I currently use Android Studio 2.3.3.
I have a file .ttf (a font file) and I would like to add this font in the res directory (/res/font).
I saw other questions, but it seems that things are different because a lot of people use Android Studio 3.0.
So, for us poor people, which is the right process?
1.create a new fonts directory in the assets directory and put the your.ttf font file here.
2.You can change to this .
TextView tv = (TextView) findViewById(R.id.tv);
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/your.ttf");
tv.setTypeface(tf);
Edit
You also can use android:typeface="sans" 、android:typeface="serif" 、android:typeface="monospace" in your xml code .
Use android:fontFamily in the xml code
From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
Also use in the style
fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>
you can use CustomTextView
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();
}
}
}
You need to create a attrs in values folder
<resources>
<declare-styleable name="CustomTextView">
<attr name="font" format="string" />
</declare-styleable>
after for textview in xml
<com.packagename.view.CustomTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:textColor="#313131"
android:textAllCaps="true"
android:text="Title"
android:textSize="12sp"
app:font="#string/monte_semibold"
/>
the font name you mention in xml should be available in assets/fonts/ folder
I need to create my own ImageView.
This is my class:
public class Brick extends ImageView implements Serializable{
public Brick(Context context) {
super(context);
}
public Brick(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Brick(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
When I try to use my ImageView inside a xml layout file (as you can see below) I have a problem. I can see a black shape, but there is not the image (the drawable called d) inside it.
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000"
android:layout_width="300dp"
android:layout_height="300dp" />
What's my error?
You should call android:src="#drawable in your XML Section .
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000" // showing Black Shape Background
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/add_image /> // Add android:src
You should add app:srcCompat or android:src in your XML Section
Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString());
You could create your own TextView by overriding the TextView like this:
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"
/>
You could create a custom class that extends TextView, let's say FontTextView.
Define a special string attribute for that class, let's say "font".
Then, in your FontTextView constructor based on the value of the font attribute, choose the appropriate Typeface from your assets.
See:
Creating a View Class Google Tutorial
Defining custom attributes SO Post
Extending TextView just for setting font looks so expensive and not good.
The most clear way is to use Android Data-Binding Framework and BindingAdapter:
#BindingAdapter("bind:font")
public static void setTypeface(TextView textView, int index) {
Typeface myTypeface = //retrieve typeface from cache, based on some font index
textView.setTypeface(myTypeface);
}
declaration in xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:font="#{R.attr.Proxima_Nova_Regular}"
/>
attrs.xml:
<attr name="Proxima_Nova_Regular"/>
<attr name="Proxima_Nova_Black"/>
<attr name="Proxima_Nova_Bold"/>
or use resources integers same way
In your cache/creator helper determine dependencies between R.attr.Your Font and instance of typeface.