Android Studio can't resolve resource drawable - android-studio

I'm designing my layout, and I've exported many assets from Sketch. Everything's going fine, but on just one particular asset, I'm getting the following error:
The resource is there, included in the project, I can open it (a regular PNG) perfectly etc, and is no different from any other assets in any way.
When I click Details on the error above, I get:
java.lang.NumberFormatException: Color value '#drawable/comment_off' must start with #
at com.android.layoutlib.bridge.impl.ResourceHelper.getColor(ResourceHelper.java:79)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:270)
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:729)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.widget.ImageView.<init>(ImageView.java:136)
...
Why on Earth does Android Studio think that my drawable is a color, while it's a PNG image like all the other assets in my project?

You may want to check the spelling of your drawable folder to ensure it is correct, and that the image exists at the referenced assets folder.

Related

Error - Android failed linking resource on androidx CardView widget

All the XML files are ok, I have checked and they have no errors, this error started soon after I copied my card-view code to various xml files, the card-view code was once working perfectly in one file, before
I generated other layouts from it
On all these XML files the needed resources are grayed out, see below
Please check whether the cardview tag is closed properly
if yes, can you add full .xml file

Image Asset NOT generating ic_launcher_foreground.xml file

I have created all my adaptive icons in the Image Asset tool but its not creating the ic_launcher_foreground.xml file - which in turn is causing my build to fail because the foreground.xml file is being referenced in the ic_launcher.xml and ic_launcher_round.xml files.
I have read many threads on how to resolve this but none of them are working.
As well, both ic_launcher.xml and ic_launcher_round.xml files are specifically referencing these two files:
<background android:drawable="#drawable/ic_launcher_background" />
<foreground android:drawable="#mipmap/ic_launcher_foreground" />
The /drawable folder is being created with the ic_launcher_background.xml file, but the Image Asset tool is not creating a /mipmap folder - it looks to be creating all the other /mipmap-XYZdpi folders and icons.
I bring this up because alot of other threads show the ic_launcher.xml and ic_launcher_round.xml files reference both background.xml and foreground.xml files in the same /drawable folder, unlike mine above.
In Image Asset:
Foreground Tab:
Layer Name: ic_launcher_foreground
Asset Type: image
Path: /dev/myApp/resources/myCustomIcon.png
Background Tab:
Layer Name: ic_launcher_background
Asset Type: image
Path: res/drawable/ic_launcher_background.xml
The only time I can get the ic_launcher_foreground.xml file to appear in the "Output Files" screen is if on the foreground tab I select Asset Type: Clip Art
UPDATE:
I found a ic_launcher_foreground.xml file in a drawable (v24) folder deep in an Android Studio folder related to my project. I copied that file to my projects res/drawable folder - and now I get the Android half robot Icon for my app when it compiles to my test device.
Well, I figured this out - finally. Posting my answer for anyone else that has the same issues.
Using Image Asset tool - ic_launcher_foreground.xml and ic_launcher_background.xml will ONLY be generated if each type (foreground tab/background tab) Asset Type is defined as anything BUT an image type. PNG/JPG asset types do not generate these xml files.
Regardless of Asset Types, both ic_launcher.xml and ic_launcher_round.xml WILL be generated. Both of these xml files simply reference other files, if Asset type is color/art/text - the reference will be to other XML files that will either be in the #drawable/ folder or the #values/ folder.
<background android:drawable="#drawable/ic_launcher_background" /> OR
<background android:drawable="#values/ic_launcher_background" />
//#drawable - is the root of all your drawables folders
// - and references only XML, ie: ie_launcher_background.xml
If the Asset Type is an image (png/jpg) then the reference will be to the newly created png/jpg files that exist in any of the #mipmap folders.
<foreground android:drawable="#mipmap/ic_launcher_foreground" />
// #mipmap is simply root of all your mipmap folders
// references actual png files, ie: ic_launcher_foreground.png
And finally, if you are using Cordova, you need to modify your config.xml to reflect the icon FILES to use:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:icon="#mipmap/ic_launcher" android:roundIcon="#mipmap/ic_launcher_round" />
</edit-config>
// this telling the app to find those two XML files in the mipmap-anydpi-v26 folder
// and those files in turn tell the app to find all the png files in the other "mipmap" folders
// if it were this:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:icon="#drawable/ic_launcher" android:roundIcon="#drawable/ic_launcher_round" />
</edit-config>
// this telling the app to use the XML files sourced in the drawable folders
// if you used png as foreground and color as background
// ic_launcher.xml/ic_launcher_round.xml would both point to:
// #mipmap/ic_launcher_foreground (ie: png images)
// #drawable/ic_launcher_background (ie: xml files)
I hope this helps others as I feel all of the above is poorly explained in other documentation.
It is very simple now: start "Image Asset Studio" by Right clicking on the "res" or "mipmap" folder and select "New > Image Asset" from the pop-up menu. Image Asset Studio will now open. Now you can create an Adaptive Launcher Icon or Legacy Only Launcher Icon as per your requirements.
To update and existing icon just select your existing ic_launcher_foreground.xml file and ic_launcher_background.xml file and hit "Next" to create the new definitions, overwriting the old ones.
With Windows Explorer go to your project "res" folder where all "ic_launcher files" are stored and delete all "ic_launcher files" first
Now in Android Studio "click on mipmap folder"->"Generate image asset".
It solved the issue for me
Well, what I did... (It often happens that others' solutions who say they finally fixed it never work for me, so I thought I would contribute yet another solution that might only work for me :) )
First I generated the image assets file as-is. I right-clicked on the res folder, clicked "New", then "Image asset". I left the "source asset" path as-is (with the path to the launcher_foreground.xml file in place) and created the assets.
Then I went back and generated the image assets again, but this time with my own png image. Seems to have worked so far, at least until the next update makes this solution obsolete.
On the second page of the asset studio wizard, there is a dropdown called "Res Directory".
I had to select debug before clicking finish. I was able to build after that.
Open your Android Studio and go to your project then
go to android-> app -> scr -> main -> res -> mipmap-anydpi-v26 on
Right click -> New -> image Asset -> Configure image asset -> path ->
mipmap-hdpi -> ic_launcher_foreground.png
Following these steps will solve your problem definitely.

Exception when trying to use icons from Asset Catalog in iOS project with Xamarin TabbedPage

I create a Xamarin Forms project in VS 2017 and started by adding a TabbedPage with three tabs. I then added the icons to the Android (in Resources/drawable) and iOS (Asset Catalogs/Icons) projects.
When I start the project on Android it works fine, but on iOS I get an exception that the UIImage cannot be loaded because initWithCOntentsOfFile returned nil. Which is actually correct, because that's not the correct method to use to access Assets. But it's the TabbedPage calling the method, so I cannot do anything about it.
The TabbetPage looks pretty basic.
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:GreenThumb"
x:Class="GreenThumb.MainPage">
<NavigationPage Title="Today" Icon="current.png">
<x:Arguments>
<local:Current />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Overview" Icon="overview.png">
<x:Arguments>
<local:Overview />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Settings" Icon="settings.png">
<x:Arguments>
<local:Settings />
</x:Arguments>
</NavigationPage>
</TabbedPage>
And the Asset Catalog looks like this:
This problem seems to be pretty old, but I haven't found any solutions aside some very dirty hacks.
Is there a clean solution out there? Or do I just have to add all my images in the Resources folder instead of Asset Catalogs?
I struggled in the past on this issue. It seems that icons for tabbed pages are not working in iOS if placed in the .xcassets.
I moved those .png files inside the Resources folder and that made the magic.
I suppose it's a kind of bug on Xamarin.
The same error was occurring to me, and I was putting the images on the .xcassets on iOS.
When I placed the images on the Resources folder, they appeared normally on my TabbedPage.
In the official Xamarin documentation about images, they say to put the images in the Resources folder.
iOS - Place images in the Resources folder with Build Action:
BundleResource. Retina versions of the image should also be supplied -
two and three times the resolution with a #2x or #3x suffixes on the
filename before the file extension (eg. myimage#2x.png).
I place all my images in the .xcassets folder, they apparently work well, but it seems not to work on every case.

Trying to install custom font in xcode 4.2, iOS 5.0, but receiving error "Validation failed: [duplicate]

I want to use font "MgOpen Modata" in my iphone App. But I dont see it in the font list in Property inspector.
How do I include that font so that I can use it?
Add the font files to your resource files
Edit your Info.plist: Add a new entry with the key Fonts provided by application.
For each of your files, add the file name to this array
On the example below, I've added the font "DejaVu Sans Mono":
In your application you can the use [UIFont fontWithName:#"DejaVuSansMono-Bold" size:14.f].
Or if you want to use it in html/css, just use font-family:DejaVu Sans Mono;
Note: this is available in iOS 3.2 and later.
I found this walkthrough was the best
Although the above accepted answer does work I found that if you added text into an XIB file and then set the font to our new font then the text didn't update.
Making sure the new font is included in resources fixed this problem (taken directly from walkthrough):
"This should not be a problem but sometimes when you’re having trouble getting your font face to show up, this can be a source of headache so let’s double check now to rule it out as a potential pitfall.
Go to your project Build Phases pane by highlighting the XCode project file in your solution explorer and on the right hand side, select “Build Phases”. You’ll see that one of the sections you can expand is “Copy Bundle Resources”. Open that list and make sure that your fonts are included in that list."
The other stuff in the walkthrough is the same kind of stuff (add the file to your project and to the plist file too)
All credit to Code with Chris for this answer which saved me a lot of time and effort and I am merely copying a shot from his page to allow this answer to stand on its own feet instead of requiring you to click the link.
I think is better for a dev to have the plist structure as raw source code. I'm leaving it for copy/paste:
<key>UIAppFonts</key>
<array>
<string>Roboto-BoldItalic.ttf</string>
<string>Roboto-Medium.ttf</string>
<string>Roboto-MediumItalic.ttf</string>
<string>Roboto-Regular.ttf</string>
<string>Roboto-Bold.ttf</string>
</array>
Copy this snippet and modify the strings keys into the array to your desired fonts. Hint: To view the plist as raw source code please click secondary button over the file and select Open As -> Source code.
EDIT
// Logs fonts ordered by name
for (NSString* family in [[UIFont familyNames] sortedArrayUsingSelector:#selector(compare:)])
{
NSLog(#"%#", family);
for (NSString* name in [[UIFont fontNamesForFamilyName:family] sortedArrayUsingSelector:#selector(compare:)])
{
NSLog(#" %#", name);
}
}
After adding the fonts could be useful to add to (for example) the AppDelegate.m class to see what are the real fonts names (on the bundle). A problem I encountered various times that I was loading the fonts with a wrong name assuming that the font filename as the correct one. Sometimes it differs from the bundle real name.
In addition to this you can check the file full name using the file info window (cmd+i) on finder (macOS).
Example to clarify:
File Name = "DroidSansEthiopic-Regular"
Processed Name in bundle = "Droid Sans Ethiopic"
Don't forget to add the fonts to the targets compile bundle resources by going to 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources' and add all the fonts manually
Most of the time we forget to add font to the application Target. Make sure you did not forget this important step. Mind checking right panel File Inspector > Target Membership checkbox.
Also you can check wether font is added well to your application or not by running this code snippet in anywhere on your app.
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
EDIT
ALPHABETIC ORDER:
for (NSString* family in [[UIFont familyNames] sortedArrayUsingSelector:#selector(compare:)])
{
NSLog(#"%#", family);
for (NSString* name in [[UIFont fontNamesForFamilyName:family] sortedArrayUsingSelector:#selector(compare:)])
{
NSLog(#" %#", name);
}
}
This will populate a list of available fonts in your app, where your added font will also appear.
Its Easy and simple now- Tested in Xcode 9.2 and swift 4
Steps to add font to your Xcode
Select your UILabel, UITextField or whatever then under fonts section and follow
Step 1
Select settings menu from left corner of font selection screen. And choose font manager option.
Step 2
Click on the add button as marked below.
Step 3
Select the folder that contains your font. It will be loaded into XCode fonts list.
Steps to add fonts to your project
Don't forget to add description to your plist with the key Fonts provided by application. and put font files inside copy bundle resources under project target settings.
Yes! thats it.. njoy..
You can use [UIFont fontWithName:#"ArialMT" size:14]; method directly, if you want to change your font style. Also all the fonts may not be available in iphone library.Just refer this for available font styles.
If you have .otf format. sometime this format doesnt work. Convert .otf format to .ttf. There are bunch of font converter online.
Once you add the custom font to your .plist file under Fonts provided by application as explained above. Make sure in your Project Target under Build Phase -> copy Bundel Resourse that file is present. If not then add it.
Then just in your code add this [UIFont fontWithName:#"your custom font" size:your choice of size];
In Xamarin iOS
Step 1:
First of all you have to add your .ttf or .otf file into your Project.
like Roboto-BoldItalic.ttf .
Step 2 :
Right click on the Font and select property
Build Action --> Content
Copy to output directory --> Always Copy
Step 3:
go to info.plist
select property Fonts provided by application and add the font name.
Step 4:
set Font programatically below way
account.Font = UIFont.FromName("Roboto-BoldItalic", 18f);

Images do not show up when I run my app on iOS device

I have images titled like so in my app:
image~iPhone.png
image#2x~iPhone.png
In interface builder I am loading image.png into my UIImageView. I also programatically load some images into a different view using imageWithContentsOfFile. The images all load fine when I run in the simulator but I get no images when I run on the device. If I use the full name of the image in interface builder it works but I want iOS to distinguish between high res and lower res. I have tried a lot of different things but can't figure this out. I see this error in the debugger as well:
Could not load the "image.png" image referenced from a nib in the bundle with identifier "com.mycompany.myproject"
Xcode 4
Deployment Target 4.1
Base SDK 4.3
Thanks for any help.
Ok...so after much experimenting I got it working.
I had two images named:
image#2x~iPhone.png
image~iPhone.png
and I was trying to load them using IB or imageWithContentsOfFile using
image.png
This worked fine in the simulator but not on my device. I just got a blank white screen where the image should be.
I finally renamed the high resolution image to:
image~iPhone#2x.png
Moving the '#2x' modifier after the device modifier(~iPhone) when referencing my images allowed it to work the way I understood that it should from reading Apple's docs. I was under the impression that you didn't need to include the device modifier when referencing images but I had to.
To sum up, I am now using
- image~iPhone.png
to reference my images in IB and programatically for some images. I now get iOS recognizing that I am on a retina screen and loading the #2x images accordingly. So the #2x modifier had to go at the end and the ~iPhone modifier had to be included in the name of the '.png'.
That is what worked for me. Hope it helps someone else. Note that I am only building my app for iOS4.1 and above so there might be some issues with this if you are supporting previous version.
iOS does not automatically pick the right image for the device like that. You are going to have to write code to check which device it is, and set the image by full name.
e.g. if ([[UIScreen mainScreen] scale] == 2) // set hi res image
Or, you can just use the same image in both, and set the content mode to scale to fill. It will look the same.
EDIT: Try writing either ~iphone (lowercase), or just don't write ~iPhone at all on the file name. If your app is not universal, then writing the ~iphone suffix is completely pointless.
iOS file system is case sensitive and device modifiers should be lowercase, it should be
image~iphone.png
image#2x~iphone.png
The #2x comes before the device modifier.
See the resource programming guide

Resources