Android fontFamily sans-serif-light - android

I need some clarification regarding sans-serif-light font for android. The following line of code
<item name="android:fontFamily">sans-serif-light</item>
in custom style say <style name="custom_style"/> gives an error if minSdkVersion is less than 16.
but the same property directly on a textview
android:fontFamily="sans-serif-light"
gives a warning if the minSdkVersion is less than 16.
Q1 What is the difference between the 2 approaches?
Q2 If i set the property directly in textview (2nd way) will the application be compatible with sdk version below 16 with the default font?
(I have only one device with android 4.4.4 and it works great on it with both ways)

If i set the property directly in textview (2nd way) will the
application be compatible with sdk version below 16 with the default
font?
The behaviour should not change even if you set the font family in the styles. The difference is probably due to your Lint's settings.

As blackbelt said, it must be due to difference in lint settings.
San-serif light font will NOT work in devices below API 16. To fix this, you need to upload a custom font (.ttf file) file in asset.
Here is how to do it -
First copy the san-serif light font .ttf file to your asset folder which you can download from here. The file will contain many fonts but you just need san-serif light file.
Then in your code, do this -
Typeface light = Typeface.createFromAsset(getContext().getAssets(), "san-serif-light.ttf");
//Make sure that the font file's name is actually san-serif-light
Then you can set this to a textview this way -
textview.setTypeface(light);

Related

android - How to change the font of the Switch in xml?

I can change the textview's fonts with using android:fontFamily="#font/test2"
but this is not working on switches, so i created a custom style includes font but still no action. Also I tried with android:textAppearance="#font/test2"
still no action. I can change the font just programmatically. How can I change with using xml attributes?
android:fontFamily atribute in xml is not available for API levels below 16.
For API below 16 use app namespace.

setting font for a project

currently practicing making an application using android studio.
I have this customized font and saved into res/font directory for the later use.
Inside the activity, i am trying to call getfont(R.font.my_customized_font) with the typeface object. ex) Typeface font = getResources().getfont(R.font.mycustomizedfont)
However, when i try to run the project with the actual device which has the API level 24, it won't activate since the getfont() method requires the min sdk level with 26.
I know that you can set the font of text view in the xml file by doing android:fontfamily but i am using the textswitcher instead of textview.
Emulator works fine by setting up min sdk with 26 but I would like to figure this out by using the actual device.
I found the solution by reading through the developer's website
I just added Typeface font = ResourcesCompat.getfont(context, R.font.mycustomizedfont)
I use a Min API of 21 and this works for me:
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/mycustomizedfont");
Then, to set the font to a the child TextViews:
text_view.setTypeface(type);
Hope this helps!
You can use via xml layout simple is that just use like this:-
android:fontFamily="#fonts/avenir"
Instead of avenir use your font name under res/font folder.
Hope it will help you.
I found the solution by reading through the developer's website I just added Typeface font = ResourcesCompat.getfont(context, R.font.mycustomizedfont)
to apply the font programmatically

How use Android supplied (custom) fonts?

According to the Android Typography page Roboto font was introduced in Ice Cream Sandwich. I can download the .ttf files there or I can find it (and many others) in the <android-sdk>/platforms/android-x/data/fonts directory (where x is ICS version and higher).
If I want to actually use this font in my app do I still need to copy this to my assets/fonts directory and setup the font like this or is there some other way of accessing it in my layout XML files?
EDIT:
To clarify the question I really mean any of the Android supplied fonts. So if you look in Android Lollipop's font folder (android-sdk/platforms/android-21/data) there are lots of new fonts.
Assume on a single layout I want to use Roboto-Italic in one TextView and (say) NotoSerif-Bold in another. Can I specify that in my XML layout file using android:typeface="..." or do I need to manually copy the required .ttf files to my font folder and subclass the TextView widget?
Hi you would need to set the typeface on a TextView like below:
Typeface yourTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName + ".ttf");
yourTextView.setTypeface(yourTypeface);
The Roboto font is already used by default on android 4.0 and up, so you don't need to do anything.
If you want set Roboto as your font, you must verify what is your version, if you have Android < 4.0 you must do something like this
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/roboto.ttf");
TextView text = (TextView) findViewById(R.id.text);
text.setTypeface(font);
But if your version is 4.0 or higher as JonasCz says, is not necessary, you can read more about this in https://developer.android.com/design/style/typography.html
Note: copy the fonts to assets/fonts. This code is useful also to put any font in your views.

How to Display Unicode font in eclipse layout designer for android

I have the following resource in my strings.xml
<string name="punjabi">ਪੰਜਾਬੀ</string>
This gets displayed in the Android string resource editor and my application just fine.
However in the eclipse layout editor, the characters display as boxes, similar to □□□□□□
The box characters indicate a font glyph look up failure.
Any ideas on how to get Eclipse to display unicode in the layout designer?
There are two things you need. The font itself, and the fallback mapping.
First, copy the font needed, such as Lohit-Punjabi.ttf into the platform specific fonts directory:
sdk\platforms\android-###\data\fonts
Second, add the fallback font into the font definition file in that directory:
For android SDK version up to 13 add the following line in fonts.xml
<fallback ttf="Lohit-Punjabi" />
For android SDK version 14 and up add the following line in fallback_fonts.xml
<family>
<fileset>
<file>Lohit-Punjabi.ttf</file>
</fileset>
</family>

How do I use native Android OS 4.1 Roboto font in Eclipse, API 16 (fontFamily)

With 4.1 jellybean, Robot has started to make appearances throughout the system UI. I would like to use the new attribute 'fontFamily' within Eclipse as documented in the Font Families section of this page:
http://developer.android.com/about/versions/android-4.1.html#UI
The goal would be to use Roboto on all devices that support it (without including it in /assets) and have the typeface degrade gracefully to Droid sans on all non 4.0+ devices. Again, don't want to have to include in my .apk, understand that is easy enough to do.
Would love to see sample code or feedback on successful use of fontFamily with API 16.
Its pretty easy, since older versions of Android will ignore xml attributes they don't understand you can just set it on your text views or your app's styles. Or you could set it manually in code but thats more work :p
And if you're using the regular version of Roboto you don't need to do anything special, its the default in 4.0+ (you might need a holo based theme, I can't remember), and Roboto bold and italic are accessible using TextView's TextStyle attribute.
In xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
/>
Or in code
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
textView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
}

Categories

Resources