I am trying to modify the default Xamarin Forms (Flyout) App template to use Material Design Icons for the FlyoutItem icons, instead of the supplied .png files. I have tried to follow this blog post by James Montemagno, and tried to use his Hanselman.Forms project as a reference... but I'm missing something.
Note: At this point, I don't have access to an iPhone or a Mac, so I'm just concentrating on the Android project.
I have done the following steps:
Imported the materialdesignicons-webfont.ttf file into the Assets folder and double-checked that its Build Action is set to AndroidAsset.
Added the following to the App.xaml file:
<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
<x:String x:Key="IconAbout"></x:String>
Modified the AppShell.xaml to change the icon:
<FlyoutItem Title="About">
<FlyoutItem.Icon>
<FontImageSource Glyph="{StaticResource IconAbout}"
FontFamily="{StaticResource MaterialFontFamily}"
Color="Black"/>
</FlyoutItem.Icon>
<ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</FlyoutItem>
This is a direct copy from the Hanselman.Forms project's TabItem - I thought the FontFamily would have to be a DynamicResource, but apparently not, because it works as is in that project, but doesn't work in mine either way - the icon is always blank (actually, if I change the Glyph to a letter A I get an "A" for an icon, but that's not really helpful).
I'm missing some dumb little detail that I just can't see.
I've gotten it to work.
First of all, go to this GitHub repo and download this material font family as a .tff file. Click on the 'font' file in the repo and download MaterialIcons-Regular.ttf.
Link: https://github.com/google/material-design-icons
Place the .tff file in your shared project.
Now reference the font in the assemblyinfo.cs file like so:
[assembly: ExportFont("MaterialIcons-Regular.ttf", Alias = "Material")]
Set the build action as 'embedded resource' on the .tff file. Do not forget this step!
Go this page https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.codepoints - to view all of the material codepoints.
In this case I will use the 'alarm-add' icon as an example (because why not). If you want to use the 'alarm-add' icon - find the 'alarm-add' codepoint. In this case the code for the 'alarm-add' icon's codepoint is e856.
Paste the following code into your shell:
<ShellContent Title="About"
ContentTemplate="{DataTemplate local:AboutPage}"
>
<ShellContent.Icon>
<FontImageSource FontFamily="Material"
Color="White"
Glyph="">
</FontImageSource>
</ShellContent.Icon>
</ShellContent>
If you follow all of the steps - the result should be like so:
If - by any chance - you want to use material icons outside of your shell you can create a Label which has the font family set as Material and as the Text property you can set the appropriate codepoint. Remember to include &#x before the codepoint - and always end it with a semicolon.
You can customise the colours and icon to whatever you like - just search the codepoint document for the icon you want and paste the codepoint which identifies that particular icon.
The source of the error is here:
<x:String x:Key="IconAbout"></x:String>
Changing that character code to got everything working - one missing zero and the result is chaos. Many thanks to #shaw and #tommy99 - trying to implement their answer (and it still not working) led me to the solution. I've upvoted their solution/comments in thanks.
Related
I added a customized font to my react native project. The font only has a single ttf file (e.g. FontFamilyName.ttf), instead of multiple files for each style (e.g. FontFamilyName-Regular.ttf, FontFamilyName-Bold.ttf, FontFamilyName-SemiBold.ttf, FontFamilyName-Thin.ttf). But it is a font collection that contains multiple styles like Bold, Thin, SemiBold, etc. I can see all the variants with the Mac App FontBook.
For iOS, I can successfully use every style of the font by specifying "fontFamily" with the postscript name of each style (e.g. FontFamilyName-Thin).
But for Android, I can only use the default style - "regular" by specifying "fontFamily" with the name of the ttf file (i.e. FontFamilyName). I can't find a way to use other styles. Specifying "fontFamily" with the postscript name of a style like I do for iOS doesn't work for Android.
Create a new directory called "fonts" in the "assets" directory of your React Native project.
Copy the custom font file (e.g. TTF or OTF file) into the "fonts" directory.
In your React Native app's "android/app/build.gradle" file, add the following line to the "android" section:
assets.srcDirs = ['assets/fonts']
In your React Native app's "android/app/src/main/res/values/styles.xml" file, add the following line to the "resources" section:
style name="CustomFont" parent="android:TextAppearance.Material.Body1">
item name="android:fontFamily">#font/YOUR_CUSTOM_FONT_NAME /item> /style>
In your JS code, you can apply the custom font to a specific text by adding the fontFamily style, like so:
<Text style={{ fontFamily: 'CustomFont' }}>Hello World
Lastly, rebuild your app using "react-native run-android" command.
Note: If you have multiple custom fonts, you will need to repeat step 4 for each font, using a different style name each time.
ps: for point 4. style code need to remove < before style tag and item tag as stackoverflow was discarding the text in the comments
For those who have the same problem using variable fonts, which are single-filed, currently the best solution I've found is to use an app called 'Slice' to slice the font variations from the single file and add the separate font files to your project.
For how to use Slice, check out https://medium.com/timeless/adding-custom-variable-fonts-in-react-native-47e0d062bcfc
For how to add custom fonts to your project, check out https://mehrankhandev.medium.com/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4
1.) Is there any reason to have a default value inside an android xml layout?
Ex.) The TextView below has included a default value of
android:visibility="visible"
`<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="visible"/>`
Conjecture: Because this is a default value, it has no effect, and therefore is an unnecessary line of code in the XML file. Is that line of thinking correct?
2.) If there is no reason for default values to exist in Android xml files, is there a lint plugin available to point out default value code lines in android XML files?
It is my thought that a large number of code lines in XML files are default values, serving no purpose. What can we do to reduce these lines of code?
U can create a style with your default values and use it.
For example:
<style name="DefaultTextViewStyle">
<item name="android:visibility">visible</item>
</style>
to use this:
<TextView
style="#style/DefaultTextViewStyle" />
I had some hope that the Lint inspection Redundant default value attribute assignment for xml, run via Android Studio might have done what you're asking. You can run it as specified under the Manually Run Inspections part of the Android docs. i.e.Android Studio -> Analyze -> Run Inspection by name -> enter "Redundant default value attribute assignment", then select the scope for the Lint check.
Sadly though, it doesn't pick up the case you mention above. I'm just wondering if there's something I've missed, or if this isn't intended for Android xml in some way?
I cannot change the color of the bottom of the selected tab (TabItem) on the TabControl. I need to change from light blue to dark blue as at the top.
I'm using Firemonkey for Android and Delphi XE6.
Open Bitmap Style Designer (see a bin folder of IDE Rad Studio)
In Menu: File -> New -> Android Light Style for FireMonkey (or Dark style)
Expand node "Images". There are a several images for different screen scales.
Look at the style.png. Find a subimages for Tab. Tou can repaint this area. Don't forget, that you need to do it for all images (for all screen scales: style15, style20, style30)
Save style to file.
Styles:
use the following link to help you obtain original default style.From there you can use a copy of the style, and make changes as you wish via dropping a TStylebook and double clicking-> Loading your copied style -> after you make changes, then save and apply.. Then simply change the stylebook property for the form...
http://delphihaven.wordpress.com/2013/12/31/inspecting-platform-styles-redux/
May be theme generator will help you.
Change the style name [now example] and Set your theme, and Just download the zip, paste it in your resource folder, then change the theme in your manifest file, like
android:theme="#style/example"
I hope this will help you.
I have an android application built with Xamarin Studio. I added a file named colors.xml to the Resources/values folder. The contents were:
<resources>
<color name="ViewBackgroundColor">#ffffff</color>
</resources>
To that, I was following this approach to define and use it; however, I was trying to apply it to the view's root element (found that resource elsewhere on SO, don't have exact link). So I applied it to the view by adding android:background="#color/ViewBackgroundColor" attribute to the root element. However, this generates a build error that #color/ViewBackgroundColor isn't a value. is anybody else having this issue and is there a resolution?
To reference that color, you must use all lowercase letters.
So
android:background="#color/viewbackgroundcolor"
This is because the Xamarin tools lowercases all names to be compliant with the rules Android has for resource names.
Is also important to set "Build Action" (with mouse right button on file color.xml) as AndroidResource.
I know how to use custom font in an app, but what I want to do is adding custom fonts system-wide, just like adding a new font on Windows. If there's no official way, which module of android source code should I read? I have to change android source code and build it to support custom fonts.
Here are steps to add custom font into Android build-in system:
Copy custom font .ttf into frameworks/base/data/fonts
Modify frameworks/base/data/fonts/Android.mk
Add your custom font into list of 'font_src_files'
font_src_files :=
Roboto-Regular.ttf
....
AndroidClock_Solid.ttf
<custom_font>.ttf \
Modify frameworks/base/data/fonts/fonts.mk
Add your custom font into list of PRODUCT_PACKAGES
PRODUCT_PACKAGES :=
DroidSansFallback.ttf
...
AndroidClock_Solid.ttf
<custom_font>.ttf \
Rebuild
NOTE: Check if your custom font exists in out/target/product/generic/system/fonts or not. If yes, your custom font have already included in system. If no, recheck your modification.
I'm working on Android 5.1.1 Lollipop.
Nguyen's answer supported me, but only to some extent. So I have to edit fonts.xml and fallback_fonts.xml files (as commented by Chef Pharaoh).
First do the steps Nguyen explained.
Add following lines in fonts.xml towards the end of the <familyset> element.
<family>
<font weight="400" style="normal"><custom_font>.ttf</font>
</family>
Add following lines in fallback_fonts.xml towards the end of the <familyset> element.
<family>
<fileset>
<file><custom_font>.ttf</file>
</fileset>
</family>
(replace <custom_font> with your custom font name)
I found this link a bit helpful, and it is in Chinese.
FYR following note is about the file fallback_fonts.xml
NOTE: this file is the legacy format, for compatibility with apps. The new,
more flexible format is fonts.xml. Please keep the two in sync until the legacy
format can be fully removed.
Download Helvetica.ttf file ,copy this file into assets folder and use this code.
Typeface font = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
your_textview_id.setTypeface(font);