For few last days I'm trying to set background image in my android application. First I added my image to res/drawable-hdpi and set it as a background in .xml code of my activity by writing
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bground"
tools:context="patrycja.MainActivity" >
It didn't work so I tried also to put this image into another drawable folder in res - without success. Then I even changed the extension of my file from .jpg to .png - nothing.
So I got into res/values/styles.xml and added this:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:background">#drawable/bground</item>
</style>
Everytime my application compiles but when I try to start it I can't even go to my first activity because it stops. LogCat shows me RuntimeException, InvocationTargetException and OutOfMemoryError. Problem must be here because my app works good with setting colour as a background.
Has anybody got an idea how to fix it?
Try this:
Use a .JPEG background.
Keep it in res/drawable-nodpi directory.
I think this will work as I have faced a similar crash earlier.
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've got a problem with my flutter app when I want to resume my app on android. So I tested the example flutter app that gets generated, but the problem also exists there. I modified only the launch_background.xml file to change the background color to black and put the launcher icon to the splash screen.
The problem is that when I resume the app after I paused it, the splash screen is visible for a short period of time. The gif below shows the problem.
Does anyone know a solution for this or do I have to deal with that?
Thanks in advance
--EDIT--
Changes to the styles.xml in Android
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="#android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">#drawable/launch_background</item>
</style>
</resources>
Try using flutter_native_splash. I use it in my all projects and it works flawlessly. Their doc says :
Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
Make sure to run the package with below line after you make all the changes :
flutter pub pub run flutter_native_splash:create
All I want is that an imagebutton which changes to another image when user press the button on. Following is my directory list. As you see I don't have drawable-hdpi, drawable-mdpi ..and others. And also I can't see mipmap-hdpi, mipmap-mdpi ..and others. My problem is I can't add selector xml for my imagebutton.
directory list
Following image is my content_main file which known as activity_main.
content_main.
My program shows the image of button but when I add android:background="#drawable/fbpressed" code piece in content_main, the program fails. it says unfortunately Login2 has stopped!
I tried lots of combination for my selector file which is fbpress.xml. like I tried android:icon="#mipmap/facebook_pressed" but it fails again and again. any idea?
Please change you selector:
<selector>
<item android:drawable="#mipmap/facebook_press" android:state_press="true">
<item android:drawable= "#mipmap/fracebook">
</selector>
Please click the Android in you screenshot , and change it to Project.It can be more clear.
Yes guys I've solved the problem. I did not know that i should change my values/styles.xml file if i add an resource file into my button. (which is fbpress.xml)
Also I changed my folder representation from Android style to Project style so that i can see my mipmap-hdpi, mdpi etc.
change from android to project
I add those lines into my styles.xml. fbpress is my resource file which is selector file.
<style name="fbpressed" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/fbpressed</item>
</style>
Following is my part of content_main.xml. I added android:background="#drawable:fbpressed" and style="#style/fbpressed" lines into this file.
imageButton
And finally my button works =) Thanks for all your helps
I have a strange problem with android resources. I have app created on Android 2.2 (android:minSdkVersion="8"). The application works fine.
When I installed app on for example Android 4.0, the application works fine, however at first run some resources are changed.
For example, I have drawable color named primary_color which refers to #FFFFFF html color. After installation, the system renders black color instead of white. In some cases #android:color/white renders black color.
Force closing app and restart app solving this problem.
Please help me to solve this issue.
This is caused by a bug in Android versions 4.0 to 4.1.3, I have been testing a similar problem with drawables turning to black only on devices with these versions, it is not present in devices with android version > 4.2.
I'm sure you noticed in your own research but it does not seem to be documented properly and there is no official solution but I can provide a few workarounds:
1) Turn off hardware acceleration, I used a different solution for my problem but this has been known to resolve some issues with images in the above mentioned Android versions. If you want hardware acceleration on for your application you can disable it for the problem activity by adding this to the activity declaration in your manifest.
<activity
android:name="activityName"
android:hardwareAccelerated="false"/>
2) For a solution specific to your example try setting the color to #android:color/transparent, this resolved my issue where I needed a background drawable to remain white.
3) Refer here: http://code.google.com/p/android/issues/detail?id=34619 to see a similar, documented bug and some of those solutions may work for your issue as well.
HTH
I came across the exact same problem!
I had this on colors.xml
<resources>
<color name="white">#ffffffff</color>
</resources>
And on styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#color/white</item>
</style>
The problem was that when using fragment(which contains a ListView) inside an activity the ListView shows white color as background(which is what I need) at first run but at second run the ListView background was black. The problem only existed on Android 4.0(I did not test on 4.1) but worked fine on Android 4.3, 4.4, 5 etc.
Turns out only white color had the problem, when colors other than white was used there was no black background issue!
So as I wanted a white background and I did not want to have overdraw of having multiple background so setting white as the windowBackground on styles.xml was important but at the same time it causes black background issues!
So as solution, I created another color which is not completely white but its white
<resources>
<color name="fakewhite">#fffefefe</color>
</resources>
And on styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#color/fakewhite</item>
</style>
Hence, in short color #ffffffff gets converted to #00000000 for Views like ListView and I also notice this behaviour on ScrollView.
This may not be a perfect solution but it worked for me!
whenever I try to apply a VERY simple style in eclipse I get errors:
the style is as below (filename styles.xml in res/layout)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="commonStyle">
<item name="android:background">#EEEEEE</item>
</style>
</resources>
Regardless of whether I try to apply this to a LinearLayout (using style="#style/commonStyle") or to an activity in my manifest file (using android:theme="#style/commonStyle") I ALWAYS get the error "No resource found that matches the given name...".
If I try to apply an Android theme to an activity (say Theme.Black) it works just fine.
If I remove any usage of my style, my R.java file is generated as normal with the following contents:
public static final int styles=0x7f030002; (in the generated layout class)
I have NO idea what is going on. It seems that NOONE is having the same troubles as me (Ive spent about 4 hours searching using google, and came up with not even a remotely close answer).
Ive tried restarting eclipse, clean building etc. etc. and nothing works what so ever...
So what am I missing?
Try moving your styles.xml file to res/values.
http://developer.android.com/guide/topics/resources/style-resource.html
Styles need to be stored in the res/values folder :)
http://developer.android.com/guide/topics/ui/themes.html (See Defining Styles)
The name of the XML file is arbitrary,
but it must use the .xml extension and
be saved in the res/values/ folder.
I haven't used styles in my code yet, however generally you need to use
android:
Does it work when you use this?:
<style android:name="commonStyle">
Please verify the you call the style in the correct manner :
for example this might compile but will not work :
style="mainButtonText"
this is the correct way to call style:
style="#style/mainButtonText"