android custom fonts in xml - android

We can change fonts using java code as described here:
Roboto font in my android app
My question: Is there a way to specify the fonts in the xml layout file itself? Or better still, can we specify the font in the AndroidManifest.xml, so that it reflects across the app?
Thanks,
Rahim.

You can use a theme that contains all the styles you want to use in your app (included fonts). The use of the schema is declared in the manifest
http://developer.android.com/guide/topics/ui/themes.html

If you want to manage custom fonts without headache, I suggest you to use a library that handles the hard part, like caching the fonts in memory and allows to preview the display in the Layout Editor. Then you can focus on creating the styles for your titles or texts.
I have done such a library called Smart Fonts for Android.
It is easy to install with gradle, and it is well documented.
It is available on GitHub:
https://github.com/smart-fun/SmartFonts
enjoy!

Related

Android - When and for which purpose we use the following prefixes in android xml file?

I am new to android development and confused about the following prefixes:
xmlns:android
xmlns:app
xmlns:tools
android:something
app:something
tool:something
I am confused about them because sometimes I use one, and android studio marks it as red underline which means i should use another one, and when sometimes i use another one android studio marks it with red line which means i should use the other one and so on, which makes me confused. And even in some cases i noticed that we are not allowed to use any one of them for example style attribute, layout attribute etc.
What each represents?
Which one is used for which purpose?
Which one is the parent, child and sub child, or may be i am wrong about this logic?
Is there anymore than them? If yes please share the resource or link
Why sometimes we don't use these prefixes for example style, layout etc?
I want to know about them to use them with understandable and proper way.
Thank you very much !!!
as Henry said, these are called namespaces.
the tools namespace is used to make changes to components without it being applied at runtime. it is useful for designing and testing different layouts or components without it affecting your code when you run the app.
the android namespace is usually used for an android attribute, meaning that it comes from the Android SDK itself.
the app namespace is used for support library components, so that it works for various different versions of android.
xmlns is simply a namespace specifying that it is an xml document, these are used for making changes to the xml document itself.

Xamarin: How and where to i specify design aspects?

I'm new to xamarin forms (but familiar with WPF) and want to create a rather simple material-design app (currently android-only, but want to add IOS support later).
Even after hours of googling I have no idea where to specify the app design.
I basically started with a Xamarin Forms Crossplatform Mobile App Project and used the Master-Detail template in Visual Studio. This provides me with three projects, the "xamarin", android and IOS projects (I will call them like this from now on). Background- and logic code is currently located in the xamarin project, aswell as the basic layout for the views (button definitions, etc.).
So how do I edit the styles of the buttons, content pages, etc. to match the material design (Background colors, shape, etc.)?
There seem to be two options:
1) Define colors, styles and themes in the Android project (Resources/values/colors.xml and styles.xml files).
2) Define colors, styles and themes in the Xamarin projects xaml files (App.xaml, [pages].xaml files).
Which of these options is the way to go? What do you recommend?
I assume to have more styling options available if going for 1), but then I will have to specify every style separately for android aswell as for IOS. If specifying it in the shared xamarin project, I will only have to do it once for all platforms.
Am i right?
What are pro's and con's / is there completely other way of doing this?
Setting your colors, styles, and themes in the App.xaml file of your "Xamarin" project is the recommended way when doing Xamarin.Forms projects and will probably be the best way to go. This will put all of your information in one place to allow easy editing and will allow you to reference the styles easily in your code/XAML.
I don't think it's noted in the docs, but you can also use Xamarin's OnPlatform setters to set different iOS and Android values.
Look here for info about global styles and here for information about OnPlatform.

Do Custom Fonts on Android decrease performance?

I'm planning to make an android app and want to use custom fonts to match the style guide provided by the brand who wants the app.
I have found this library Caligraphy to use custom fonts.
Is there an issue regarding performance of the app by using custom
fonts with this library?
Is there another option to add custom fonts painlessly?
Also, is there a guideline that prevents from using custom fonts in android apps, or is it a bad practice?
Is there an issue regarding performance of the app by using custom fonts with this library?
There generally isn't any issue with custom fonts, there is no reason this library would hit the performance of the app
Is there another option to add custom fonts painlessly?
Yes you can easily achieve this with simple coding
TextView txt = (TextView) findViewById(R.id.textview);
Typeface font = Typeface.createFromAsset(getAssets(), "your_font.ttf");
txt.setTypeface(font);
Also, is there a guideline that prevents from using custom fonts in android apps, or is it a bad practice?
No it is not a bad practice. Infact google recently added an option to use custom fonts with support library v26. You wont have to add ttf file to the apk, google will download that for you itself

Set fallback fonts android app

I'm developing an app that needs to support multiple languages, including some for which fonts are not included in some older Android versions.
The same TextView can have content in multiple languages, so setting a single TypeFace does not help, I need to have a list of fall-back fonts to use when some characters are not available in preferred fonts.
Extending to this, I also need to use the same set of fonts for other elements also (not only TextViews) if possible.
I had a workaround for this. I extended the Application class for the app and set the fonts globally there.
For the fallback fonts, I read the Android API code for Typeface and going from there, I found some hidden APIs. Doing all sorts of hacking (reflections, getting private/hidden classes or methods, setting private fields etc.), I finally made it working for Android API versions 21 to 23. Not sure about the newer APIs and it doesn't work for the older ones.
You can check my code at https://gist.github.com/nisargjhaveri/47acc83e66dcb347e05685ccfd3038e8
It is working, but even I don't recommend using it!
The better solution for this may be to use TypefaceSpans, after detecting the best typeface for each character, everytime you show text to user. I did not try this.
Also, I just have been playing with android for past two months, so think twice before you take my advice! :P

Preview custom fonts in Android Studio

Currently I'm using the pixlui library to use custom fonts, and it's great!
https://github.com/neopixl/PixlUI
Is there any way to preview the custom fonts in Android Studio?
I have created a Library called Smart Fonts for Android. It allows you to preview the custom fonts in the Layout Editor of Android Studio.
The fonts are cached in memory so that they are loaded once.
Smart Fonts is easy to integrate with gradle, it is documented and available on GitHub:
https://github.com/smart-fun/SmartFonts
enjoy!
Unfortunately no. The way that PixelUI is setting the font doesn't work in the previewer.
They are only setting font in one constructor as is visible here:
https://github.com/neopixl/PixlUI/blob/master/Library/src/com/neopixl/pixlui/components/textview/TextView.java
The constructor the editor is using is the second one, and the font is not set when the TextView is constructed this way.

Categories

Resources