I got stuck in getAssests() in the below code - android

public class XYZ extends LinearLayout{
TextView text = (TextView) findViewById(R.id.kid_name);
Typeface font = Typeface.createFromAsset(getAssets(), "eng111.ttf");
text.setTypeface(font);
}
I just try to use the other format for the text and the problem is occurred "create the getAssets() method "
Where do I mistake ? please sort out this problem

You need a Context, since you are not in an Activity you need to call:
getContext().getAssets();

Related

Assigning Custom Font to TextView [duplicate]

This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 5 years ago.
I'm new to programming and I'm looking to add a custom font I download to Android Studio. I was able to follow instructions how to add the font but when I run the app I can only get one TextView of my two two TextViews to use this font. This is my code, can someone please tell me what I'm doing wrong here. Thank you!
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
}
}
I assume there's a typo. You wrote tfc1 but you set tfc2. From your code:
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
As both of the tfc1 and tfc2 fonts are same. You can use one typeface to both of the textviews as #Sagar Patel showed.
STEP 1/
Start by creating a folder named assests then inside that folder create another one named folder and import your *.ttf files to that folder
STEP 2/
Now import this before start writing the code given below:
import android.graphics.Typeface;
Now implement the following code to your class:
// Font path
String fontPath = "fonts/Face Your Fears.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
I suggest you follow this tutorial right here it will guide step-by-step through using external fonts in Android Studio
This tutorial link helps to understand on configuring the custom font in android.
You can use android:fontFamily attribute in xml file.
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
// tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
//you get error in this line text2.setTypeface(tfc2);
text2.setTypeface(tfc1);
}
}

SearchView custom font in android

I have been trying to set custom font to the android.support.v7.widget.SearchView query hint and the text entered in the View.I did try setting the font dynamically from the assests to the searchView by creating a TypeFace object, but the problem occurs that "SearchView doesn't contain a setTypeface(Typeface tf) method." I did try for a custom SearchView class but couldn't find one.
private void setFont(String font1, String font2, String font3)
{
Typeface tf = null;
tf = UiUtil.changeFont(MainActivity.this, font1);
btn1.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font2);
btn2.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font3);
btn3.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font3);
// no set typeface method..
}
The workaround here is to first get the searchView's textView and then change the typeface for the textView instead:
TextView searchText = (TextView)
searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
searchText.setTypeface(myCustomFont);
Or, if you're not using appcompatv7:
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchView.findViewById(id);
then set the typeface as usual.
To change the font family in searchview programmatically from xml font folder:
Typeface tf = ResourcesCompat.getFont(dis,R.font.montserrat_regular);
TextView searchText = (TextView)search_view.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTypeface(tf);
For Kotlin and Androidx
val face: Typeface = Typeface.createFromAsset(context?.assets, "font.otf")
val searchText = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as TextView
searchText.typeface = face
Thanks to all the previous answers, I found my best solution witch I hope that you find it the cleanest answer, too.
This is working for SearchView class inside the package android.widget
First, create an extension like below:
import android.graphics.Typeface
import android.widget.SearchView
import android.widget.TextView
fun SearchView.setTypeFace(typeface: Typeface?) {
val id = context.resources.getIdentifier("android:id/search_src_text", null, null)
val searchText = searchView.findViewById(id) as TextView
searchText.typeface = typeface
}
Then use this extension wherever you like:
val typeface = ResourcesCompat.getFont(requireContext(), R.font.sans)
searchView.setTypeFace(typeface)
You may want to get typeface from assets or other ways. but the rest is the same.
If anyone looking to implement the same in xml
Try the solution mentioned this answer able to customise
{textSize, fontFamily, textColor, hideBottomLine} in SearchView - https://stackoverflow.com/a/73997430/3020859

cannot resolve symbol 'setTypeface'

// Font path
String fontPath = "fonts/jcc.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
In Android Studio it comes up with an error, "Cannot resolve symbol 'setTypeface'" and "Unknown class: 'tf'". I do not know why, I defined 'tf' and I have looked at many tutorial's that use setTypeface.
Please Help!
Edit, here is a screenshot, I am using this exact code and my fonts are under 'assets/font/jcc.tf'.
http://i.imgur.com/fcDdVRz.png
Sorry do not have enough reputation to post images :(
After much trial and error I found the solution!
My code was after the onCreate method, once I placed it inside all the errors went away!
The code with errors:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
The code without errors (notice the code is now inside onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Font path
String fontPath = "fonts/SECRCODE.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.java1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
}
If anyone could explain why this works it would help greatly!
Thanks!
The reason you need to set the font (typeface) in the onCreate method is because your Java code in the onCreate method is automatically executed when an activity is loaded. Without putting it inside the onCreate() method there is no reference/call to execute your code.

How to use custom font in android application

For implementing custom font i seen few examples here issue is different,I am taking custom font in one abstract class which is used in all over the application, Here is my code
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
}
tried by debugging Here i am getting null pointer exception
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
how to resolve this problem give some suggestion .
create Typeface object in onCreate method of activity
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf ;
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
tf = Typeface.createFromAsset(getAssets(), fontPath);
}
Assign value of tf in onCreate() then it resolve your problem.
You can use a library; Calligraphy
In android , you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.
After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First , get the reference of the text view in the code. Its syntax is given below:
TextView tx = (TextView)findViewById(R.id.textview1);
The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below:
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below:
tx.setTypeface(custom_font);

Change FontFace of application [duplicate]

This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Closed 9 years ago.
I have one application in Which I want to change font of text in whole application.
Is there anyway to change font of application Programmatically or with xml in manifest.?
Try this
1.place your ttf file in assets folder and add these lines to your java file
Typeface font = Typeface.createFromAsset(activity.getAssets(),"fonts/androidnation.ttf");
tv.setTypeface(font);
2.To set it through xml
XML Typeface
Place you fonts in fonts folder and then use the following code.
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);
This is the way to set font programmatically.
Create fonts folder in asset and paste the fonts whatever you want to paste.
Create one class. Name it Typesafe.java
public enum TypeSafe {
HELVETICANEUELTCOMBD,
HELVETICANEUELTCOMBDCN,
HELVETICANEUELTCOMCN,
}
After that create one method in your Activity or if you have the Utility class.
public void setTypeface(TextView textView, TypeSafe type, AssetManager assetManager){
if (TypeSafe.HELVETICANEUELTCOMBD.equals(type)) {
final Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-Bd.ttf");
textView.setTypeface(typeface);
} else if (TypeSafe.HELVETICANEUELTCOMBDCN.equals(type)) {
final Typeface typeface1 = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-BdCn.ttf");
textView.setTypeface(typeface1);
}
}
Call these method in your activity.
setTypeface(yourtextView, TypeSafe.HELVETICANEUELTCOMLT, getAssets());
The easiest way to do it is creating your own TextView:
public class MyTextView extends TextView {
public DMTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// you need your TypefaceFile "myTypeface.ttf" in the assets folder of your project
setTypeface(Typeface.createFromAsset(context.getAssets(),"myTypeface.ttf"));
}
// add the other constructors if you want
}
now, you can use it anywhere in your xml:
<com.your.package.MyTextView ... > just like normal textviews
It might be improved by caching the Typeface, so you don't have to create it again with every Reference to your TextView.

Categories

Resources