I am trying to specify an menu icon for an Action Bar. I'm using Android Studio 1.2. My XML code follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/menu_connect"
android:icon="#drawable/ic_settings_bluetooth_white_24dp.png"
android:title="#string/menu_connect"
app:showAsAction="always"
/>
<item
android:id="#+id/menu_settings"
android:title="#string/menu_settings"
app:showAsAction="never"
/>
</menu>
The compiler complains about anything that I put after "android:icon=". Depending on what I've changed last, I get: "No resource found that matches the given name" or "String types not allowed" (among other errors).
I've tried suggestions from several posts. I cleared the Intellij cache and rebuilt everything and nothing changed. One post suggested that perhaps android:icon wasn't supported in earlier API versions so I set minSdkVersion to 23 (my current SDK version) in my module build script. I re-sync'd and again nothing changed.
I had some trouble figuring out how to set up the drawable directories for multiple resolution bitmaps. (Why doesn't Android Studio just create them when creating a new project?) I now have them (drawable-hdpi etc.) set up as siblings to the drawable directory. I think that's right. At least they appear to be correct in the Android directory view. Yeah, I'm a newbie.
Is there anything else that I can try?
Replace:
android:icon="#drawable/ic_settings_bluetooth_white_24dp.png"
with:
android:icon="#drawable/ic_settings_bluetooth_white_24dp"
You do not use file extensions when referring to resources from anywhere inside of Android.
Related
I have created a flutter application that works well on a physical phone but when I try it out on an emulator the application stops unfortunately. Error produced java.lang.RuntimeException: Unable to start activity ComponentInfo{big.xxxx.xxxxx/xxx.xxxxx.xxxxx.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/launch_background.xml from drawable resource ID #0x7f040019
I have checked and the launch_background.xml is present. Here is the content
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="#mipmap/launch_image" />
</item> -->
</layer-list>
Looks like you have deleted your launch_background.xml this is the first screen that is execute in your Android when you are using Flutter. Check in the path /android/app/src/main/drawable if you have the launch_background.xml.
If you don't you must added in your project.
After asking around I was able to solve the problem by removing the instances where I call the drawable file.
This was mainly in my styles.xml file and Manifest.xml file respectively.
<item name="android:windowBackground">#drawable/launch_background</item>
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="#drawable/launch_background" />
Had an opportunity of asking another person who had solved this problem differently and this is what he had to say.
The underlying cause
The underlying cause seems to be that the expects a bitmap but it didn't get one. Even though #mipmap/ic_launcher points to some PNGs (see e.g. res/mipmap-hdpi), on API >= 26, it would point to this XML which Android did not seem to like very much.
So in my case, referencing the #drawable (which is always a PNG == a nice bitmap) rather than the #mipmap (which may be an adaptive-icon == a weird bitmap? if a bitmap at all?) fixed it.
In other words: don't use an as the android:src of a .
If I'd to this today I would try if a vector drawable (just import an SVG in Android Studio to get the vector XML) works as well (as opposed to having to use a PNG).
Hope that helps (or at least explains this change of mine).
Do double check that you have the same issue - Resources$NotFoundException may be thrown for many different reasons, e.g. the ressource being in the drawable-v26 folder and thus ignored when running on e.g. API 24. Sometimes the gradle build log shows some warning saying that an XML failed to compile (but the build still succeeds and then things blow up at runtime).
Best search the internet for other causes and fixes for Resources$NotFoundException if it's not the same issue.
Just comment this out in android\app\src\main\AndroidManifest.xml
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background"/>
I am watching a udemy training series on Android. And for many of the videos, when he creates a new project there is a res/menu folder. In it, there are xml files like a menu_main.xml. However, when I launch a new project on Android Studio, there is no res/menu folder at all. Am I missing something? Why do I have to create it manually?
In the video, it has the following in menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never"/>
</menu>
Am I missing something?
The trainer in the video is using a different version of Android Studio than you are, or you did not set up your project in exactly the same manner (e.g., you chose a different activity template).
Why do I have to create it manually?
In the version of Android Studio that you are using, for the activity templates that you chose (e.g., "Blank Activity"), the templates do not employ an action bar, Toolbar, or anything else that involves a menu resource.
I am beginning android development and I am having problems making an action bar. Basically I am following some code form online and I am getting an error saying I do not have an icon:
error: Error: No resource found that matches the given name (at 'icon' with value '#drawable/ic_menu_search').
I did some research and thought my problem was I did not have all the extras downloaded from the android SDK. But after downloading all the extras it didnt work. I know I could just find the icon and download it, but I do not want to have to download all the icons individually, therefore I think I am missing something. My xml file I am getting the error with looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
this is under menu>main.xml
If you want to use the android icon, you should add the android prefix after #. Change
#drawable/ic_menu_search
in
#android:drawable/ic_menu_search
#drawable looks inside the res/drawable* folder of your app. If you want to access to the android resources. strings/drawable/id you have to put the android prefix after the #. with that prefix you will access to the android.R class not to your.R class
I'm attempting to style the ActionBar, following this blog post:
http://android-developers.blogspot.com/2011/04/customizing-action-bar.html
With this source code:
svn checkout http://styled-action-bar.googlecode.com/svn/trunk/ styled-action-bar-read-only
However, I'm getting issues in /res/values/styles.xml.
This:
<!-- style for the tabs -->
<style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBarView_TabView">
<item name="android:background">#drawable/actionbar_tab_bg</item>
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
</style>
Is erroring with:
Error retrieving parent for item: No resource found that matches the given name 'android:style/Widget.Holo.Light.ActionBarView_TabView'.
However, using the following answer, and digging through the source for Android, I can see that the theme does indeed exist:
https://stackoverflow.com/a/7149389/420001
The only thing that I changed in the project was moving it from android-11 to android-14, and then running an "Android" > "Fix Project Properties" in Eclipse. I do note that the repo linked to in that answer, that lists the style, is on branch master, so I can't see why the theme would be unfound.
Just for tests, I put that chunk of code in my working android-14 targeting app, minus the background drawable, and it throws the same error.
According to the links in https://stackoverflow.com/a/7837756/1003511 there is a Widget_Holo_Light_ActionBar_TabView but not a Widget_Holo_Light_ActionBarView_TabView. It's possible the resource has been renamed since that tutorial was written. It says the version without the extra "view" has been included since API level 13 - if the tutorial was originally written on 11 it's possible it was renamed in 13, and since you are running 14 you can only see the renamed version. I'd try removing the extraneous "view" and see if the code runs.
You may be missing an '#' character when specifying that you want to look in the android styles:
You can also double check to see if the parent style is listed in the API documentation here:
http://developer.android.com/reference/android/R.style.html
It looks like you may have the name slightly wrong.
Everything is working fine with my Android project with the standard layout, however whenever I add a new layout-xxx subdirectory, say layout-large, and copy an XML file to it to be my baseline for the new size in question, from that point forward I get the error listed above and the project won't compile.
Location TypeClass file collision: A resource exists with a different case: 'R$ID.class'
If I remove the xml file from the layout-xxx directory everything goes back to working fine. I do have the support-screen tags in the manifest.
I can't possibly need to ID all my views and such inside my layout with seperate names for seperate layout files do I? This would make coding so rediculously conditional that it would not be worth it.
What am I doing wrong? or missing?
I had the same issue today and it was because of my mistake.
I had my menu defined like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="#string/action_bar_share_with">
<menu >
<item android:id="#+Id/someid"
android:title="MyMenu"/>
</menu>
</item>
</menu>
Reason of error was Id with capitalized I which should be actually like id
Hope this helps someone