I initially thought that this would do the trick:. I used the ResourcesCompat. The safest way to avoid this is to load the font programmatically, if you are unable to set the style through XML. Here is the official documentation on how to do that. There is also this crash with FontsContractCompat in Before the release of Android O, fonts can be used in the following 2 ways :.
To work with Custom Font, you need to install the latest version of Android Studio 3. This is important as some of the features are not supported on Android Studio 2. Once you are done with the installation of the latest version of Android Studio, create a project and add a text view in any of the activity This is the text view on which we are going to apply custom font.
Then follow the below steps :. Note: A really important thing to note is that we had to define attributes using both android and app namespaces. The app namespace is what ensures that the feature is backward compatible. Now that we have seen how custom fonts work, let's jump onto another novality - downloadable font. Save to shared storage. Save data in a local database. Sharing simple data.
Sharing files. Sharing files with NFC. Printing files. Content providers. Autofill framework. Contacts provider. Data backup. Remember and authenticate users. User location. Using touch gestures. Handling keyboard input. Supporting game controllers. Input method editors. Performing network operations. Transmit network data using Volley. Perform network operations using Cronet.
Transferring data without draining the battery. Reduce network battery drain. Transfer data using Sync Adapters. Bluetooth Low Energy. Wi-Fi infrastructure. Discover and connect. Runtime API reference. Web-based content. Android App Bundles. Google Play. Play Asset Delivery. Play Feature Delivery. In-app reviews. In-app updates. Google Play Instant. Get started with instant apps.
Get started with instant games. Integrate with Firebase. Play Install Referrer. Play Install Referrer Library. Application Licensing. Android GPU Inspector. System profiling. Analyze a system profile. GPU performance counters. Frame profiling. Analyze a frame profile. Frame Profiler UI. Customize or port game engines. Process input events. Support game controllers. Achieve proper frame pacing. Frame pacing in Vulkan. Integrate Android Performance Tuner.
Output audio. Manage memory. Use prebuilt or turnkey game engines. To request a new font use requestFont method from FontsContractCompat. Add following to the end of showFont :. Requesting a downloadable font is an asynchronous operation.
Method requestFont passes the result through a FontsContractCompat. FontRequestCallback interface. Use the Typeface instance passed to set the font on a View. Enable all the buttons and hide progress indicator:. Use it to display an error message by calling showError and passing it error code:. FontContractorCompat uses it to execute retrieving a font on a Thread associated with that Handler. Make sure you provide a Handler that is not associated with a UI thread.
For convenience, create a private field that will hold the handler and a property that will initialize and retrieve it:. Using the handlerThreadHandler property will initialize the handler on the first use and return it.
After you answer a question on the quiz, it would be cool to display a simple fact about the font in question. So your next task will be to retrieve information about a font family.
To get information about available fonts use fetchFonts from FontsContractCompat. Like in the previous task, create a FontRequest first:. This will return the information about the requested font family if there is one available with the given name.
The string R. Fetching font data is a blocking operation that should execute in the background. Use doAsync and uiThread blocks from Kotlin to execute it on a background thread:.
Finally, add error handling and hide the progress indicator. The final code should in loadFontFact look like:. For the name type acme in the dialog. All this looks familiar. Only this time using XML. Refer to the created font resource like you did with font family and. You should see the FontQuiz labels in fonts now.
As a bonus — you can specify fonts that Android should preload before your app starts!
0コメント