Android Studio - Set custom fonts with asset manager in fragment - android

I'm rewriting a project of mine to incorporate fragments for use on tablets. I also decided to start using Android Studio with the conversion. Pretty straight forward but I've run into an issue on the pretty simple task of setting a custom font.
I started with these:
Custom fonts in android and
List of files in assets folder and its subfolders and this Set custom font for Android fragments
In Eclipse my class extended Activity and I did the following in the OnCreate with no problems.(Having a directory and file "assets/FUT_R.ttf")
TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
now trying to convert this Activity to an ActionBarActivity (fragment with V7 support library) and I've changed the above code to this. (where view is the inflated layout with one Framelayout for phones)
TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
and it crashes at run time with the lovely java.lang.RuntimeException: native typeface cannot be madeeven though i created and 'assets' directory on my new project with FUT_R.ttf in its root.
To confirm the asset manager I tried this code:
String[] f = null;
try {
f = getActivity().getAssets().list("");
}
catch (IOException e){
e.printStackTrace();
}
for(String f1:f){
Log.i("names",f1);
}
And got the following output:
07-26 07:40:40.134 2114-2114/com.myapp I/names: images
07-26 07:40:40.134 2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134 2114-2114/com.myapp I/names: webkit
I'm confused because no where in my 'assets' directory or project do I have these files and/or directories. Obviously the error is the system can't find my font file. WHAT AM I DOING WRONG? Any direction would be greatly appreciated, I've wasted far too much time on this stupid problem.
Thanks!

I finally stumbled upon this: Load a simple text file in Android Studio
which points out the assets folder in Android Studio has apparently been changed. Instead of its normal location in the root app folder (same level as libs, src, ect) it needs to be created under yourapp/src/main/assets. Hopefully this helps someone else. I wasted a lot of time on it!

Related

Where to place the assets folder in AIDE?

I'm using AIDE (A code editor / IDE & compiler which runs on Android) to write an Android app, and I want to add some assets to my project, specifically a HTML file plus associated CSS and JavaScript files, to be displayed in a WebView.
I've put my assets (a HTML file) in projectDirectory/app/src/main/assets/htmlfile.html, but, when I use the code below to load it in WebView, it shows "File not found". The exact same thing works fine in Android Studio.
Where do I put this assets folder for AIDE to pick up its contents ?
Here is my code where I'm loading the WebView, although I don't think there's anything wrong here, as it works in Android Studio.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("file:///android_asset/htmlfile.html");
}
What I've tried:
Placing it in /app/src/main/assets/htmlfile.html (The usual location, which works in Android Studio).
In /app/src/main/res/assets/htmlfile.html
In /app/src/assets/htmlfile.html
Pretty much everywhere else.
However, wherever I put it and recompile, the WebView always shows "File not found". So where do I put my assets folder for it to work with AIDE, or how do I make AIDE pick it up when compiling ? Does AIDE support assets at all ?
Doh: All I needed to do was menu > more > project > refresh build, with the assets in /app/src/main/assets/htmlfile.html and it worked. Somewhat non-obvious, but hey, solved my problem.
Leaving this self answer in case anyone else has the same problem.
As for an html keep it in app->src->main->assets
And later on you can acces it like "file:///android_asset/error.html"
Lets take example of a webview wv1. You can write
wv.loadUrl("file:///android_asset/error.html");
Dont forget to rebuild your project. This is a safety measure in such case. Many times files are not included automatically

Integrate a specific font with Android apk

I have built an application which displays the news in Nepali (Devnagari) font.
Everything is fine but the problem is that the font is not displayed properly in some Android phones.
Here I have posted two images showing the variation of the same content in different phones. The first one is perfect but the second one is incorrectly displayed.
1st Image with correct fonts
2nd Image with incorrect fonts
The problem I realized is that the second phone does not contained the correct font installed in it.
How can we get rid of this problem?
I have got no proper idea regarding this, but a solution that came in my mind is that - What if we integrate the necessary font along with the apk file and the font too gets downloaded with the apk.
Please pass me suggestion for this and would be grateful to know how to implement any solution that exists.
If you are using the native Android Textview Component you could use the method:
public void setTypeface (Typeface tf)
And create the typeface this way :
static Typeface createFromAsset(AssetManager mgr, String path)
Create a new typeface from the specified font data.
Typeface.createFromAsset(getAssetManager(),"RELATIVE_PATH_TO_YOUR_TYPFACE_IN_THE_ASSET_FOLDER");
If you are using Android Studio the asset folder should be created under src/main/assets.
Make 1 folder in assets named "fonts", put your font files inside that folder. e.g. see following image
import android.graphics.Typeface;
Then declare Typeface object
Typeface allFontType, allFontTypeNormal, allFontTypeLight;
try
{
allFontType = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Bold.ttf");
allFontTypeNormal = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Medium.ttf");
allFontTypeLight = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-ExtraLight.ttf");
}
catch (Exception e1)
{
e1.printStackTrace();
allFontType = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
allFontTypeNormal = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
allFontTypeLight = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
}
To apply font in TextView use following code
TextView lblDaysName = (TextView)view.findViewById(R.id.lblDaysName);
lblDaysName.setTypeface(allFontTypeNormal);

Android Studio, using Assets Folder

So, I've seen many posts on this, but none has helped. I want to work with the assets folder, for organizational purposes. The images below show where the 'assets' folder is, along with the app.imi options of where it should be. I have tested this, and in the >exploded-aar/com../appcompat-v7/19.1.0/assets ... the files inside this seem to work. I can call on the .mp3 file and load and play it correctly (Though a few times it did not recognize it. Even when it was inside a folder, inside of the directory.)
In the src/main/assets folder, all the files are ignored. Is there a specific way I have to call this? In other words, must I also link the actual folder it is it? src/main/assets/music to load up the riseofcc.mp3?
I do not have 10 reputation to post images, so sorry for only links.
EDIT:Also I am using Android Studio 0.5.7
ANSWERED:
Thanks for Xavier Duchrohet for explaining exploded-aar is for the application's dependencies. I have learned that when calling upon a file inside your asset's folder using the code:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
AssetFileDescriptor descriptor = assetManager.openFd("sound/explosion.ogg");
You must include the folder's parent inside the arguement.
Android Studio's app.iml has the asset's directory (src/main/assets) inside of its configuration's options.
Normally, to include sound files, I create a folder called raw, inside the app / src / main / res.
That is, I right click on the folder res and from the menu that appears, choose: New / Android Resource Diretory. Renaming in the window that appears, the Directory name field values for raw.
How it works for me, I hope this might have helped him.

How do you integrate custom fonts in an app?

I am trying to integrate custom fonts (Imperator.ttf and goodfish.ttf) from the following website into my corona app:
http://www.1001freefonts.com/top-fonts.php
I am following carefully the tutorial found here on Corona's official website:
http://www.coronalabs.com/blog/2013/01/16/faq-wednesday-custom-fonts/
I follow the whole procedure for macosx integration (installing the font by double clicking etc) and do not get any error. I also added the font file name to my build.settings file:
UIAppFonts =
{
"Imperator.ttf"
},
Whenever i launch the simulator and debug the currently loaded fonts, the new one doesn't appear.
Is there an easy way to debug the issue on corona simulator, on osx, to troubleshoot why the font doesn't load ?
Thanks
While installing custom fonts to your corona application, you have to follow the following steps:
Install the font in your system.
Copy and paste the font file(such as: Imperator.ttf) to the application folder where your main.lua exists.
Then for iPhone(only for iPhone, Android doesn't need this), add the following lines to your build.settings:
iphone =
{
plist =
{
UIAppFonts =
{
"Imperator.ttf" -- Font file name
},
UIApplicationExitsOnSuspend = true
},
}
Debug and get the supported Font Names by the system using the following:
local fonts = native.getFontNames()
for i,fontname in ipairs(fonts) do
print(fonts[i])
end
From the above debugging, you can get the exact Font Name (here: Imperator) that you have to use while creating the text/label. Sometimes this may differ from the name of the font file. You can also get it from applications like photoshop(it's text tool font name), etc.
local myText = display.newText("Label with custom font",150,100,"Imperator",20)
Then finally, you will get the output as:
Keep coding................ 😃
Put Imperator.ttf font file in Assets folder
In class use following code:
Typeface imperator_typeface =Typeface.createFromAsset(getContext().getAssets(),"Imperator.ttf");
your_textview.setTypeface(imperator_typeface);

Assets folder in Android Studio?

Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app crashes when I place the assets folder in src/main. Im using this code to load my fonts.
Typeface fontRegular = Typeface.createFromAsset(getAssets(), "fonts/DroidSans.ttf");
Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/DroidSans-Bold.ttf");
myDeviceModelTxt.setTypeface(fontRegular);
What am I doing wrong?
I am not sure if there has been any bug fixes since this was asked, but I am using the current structure for a project in Android Studio 0.5.2:
root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------SomeFont.ttc
|----------AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle
And then obtain it by calling
Typeface.createFromAsset(mContext.getResources().getAssets(), "SomeFont.ttc");
Word of caution though, there is a bug (https://code.google.com/p/android/issues/detail?id=9904) that prevents typefaces from being garbage collected properly. Use a singleton!
Create Assets folder Right click on app->>new->>Folder->>AssetsFolder like below image
Put your font inside this folder by just copy and paste. and use below code for example..
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "YourFontName.ttf");
setTypeface(tf);
Simply follow this path:
File > New > folder > assets Folder
Here App must be selected before creating folder.
For more information see this answer
The assets folder should be placed under the root of the project.
See here for more examples.

Categories

Resources