When trying to add a custom image to a ImageButton, I get the following error:
E/AndroidRuntime(3304): Caused by: java.io.FileNotFoundException: res/drawable/btn_default.xml
This Works:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_plus" />
This does not:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/elec" />
On the designer I can see the image fine on the button, but as soon as I try to run it, I get the above error. The elec.png file is in drawable-hdpi.
Burned 3 hours trying to find the problem. Any help really appreciated!
You should also put you image in the drawable folder ( + correctly sized version in drawable-ldpi, drawable-mdpi, drawable-xhdpi to follow Android guidelines )
In the end, it turned out that my emulator was somehow corrupted. The code works perfectly on a real device and on a newly generated emulator.
Related
Today I received a complaint for an app that I made. Basically, the problem is that I have an ImageView which loads an image from the resources directory, but sometimes, the image is not loaded and a white space is displayed instead. The only complaint comes from Sony Xperia Z3 with the Android version 5.1.1. The drawable is added only for xhdpi, but I know that if needed Android scales it to the corresponding resolution. Do you have any ideas why this could take place since I wasn't able to reproduce the bug? Also, here's my ImageView code but I'm sure that there is no problem.
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="#dimen/padding_medium"
android:background="#drawable/green_logo"
android:scaleType="fitCenter"
android:contentDescription="#null" />
Why do you use background for ImageView? Use android:src="#drawable/image".
Creating vector images in Photoshop (for transparent backgrounds). When I add them as the image source for my ImageButtons, and run the app, the image displays with a white background.
Can't find any useful info on why the images are not coming out transparent through the app.
Are there some additional steps/coding required to make vector images work properly? I can't see why.
Example code, as requested:
<ImageButton
android:id="#+id/btnGameUp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/arrowright"
android:adjustViewBounds="false"
android:clickable="true"
android:cropToPadding="false"
android:padding="0dp"
android:scaleType="fitXY"
android:layout_marginLeft="270dp"
android:layout_marginTop="260dp" />
No one came to help us, but we managed to find the solution on our own after hours of crying and holding each other for comfort.
In the layout xml file, you need to add the following line to each vector object:
android:background="#android:color/transparent"
That's it! Tested and working perfectly. Hope this helps someone.
My linearLayout backgroud is losing is patch 9 effet after the imageView is load from a dynamic Url
the only way to fix the problem is to use holder.imgView.setScaleType(ImageView.ScaleType.FIT_XY);
but i don't want to stretch the image at the same time
<LinearLayout
android:id="#+id/llPatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/testt1"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageRecette"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#drawable/tartare" />
</LinearLayout>
9-patch images are compiled with the project (from your res folder).
You can see how a compiled 9-patch looks like if you add one to a project, build it (the project) and look in bin/res/[where your image is at]/[image name].9.png
So you can't load a 9-patch image (with the lines on the sides) from a url.
You can however upload a compiled 9-patch to your server, download it and use it as specified here:
https://stackoverflow.com/a/10639923/876603
My application crashes when i put background line like in this code. When i delete this line my application works fine and also when i use another photo it also works. What's the problem?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/table2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
The background for the main screen works fine also. But in the second page, it is problamatic.
If you are using only drawable folder to store all images.then change storage folder for high resolution images in drawable-xhdpi folder or higher.
Hope this helps.
Thanks to you all. I changed the dimensions from 2500x1900 to 1280x920, now it works.
Make sure about the image path if it is in drawable folder..put it inside Mipmap folder in your project directory.
I think you should check the size of your image anything above 300kb may tend to constantly crash your app. You can always use image compression Softwares to reduce the size of your image.
I am trying to partially change the images for Android's checkboxes. Following the tutorial here, I have done the following experiment:
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:button="#android:drawable/btn_check_on"
android:background="#android:drawable/btn_check_label_background"/>
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:button="#drawable/cb_on"
android:background="#drawable/cb_background"/>
For the third checkbox, I have copied the images btn_check_on.png and btn_check_label_background.9.png from Android's SDK to the project's res/drawable and rename them to cb_on.png and cb_background.png. While I expected the three checkboxes to have identical appearances, surprisingly, the third checkbox is larger than the first two. Can anybody explain why? How can I fix this problem?
Try renaming your image cb_background.png in cb_background.9.png
.9 means that the image is a nine-patch image which is done to rescale itself automatically.
I think that why the third image is larger.
For more information about nine-patch: here
My guess would be that you only copied one resolution of the drawables. You need to copy all of them (mdpi, hdpi,ldpi) to the appropriate res folder (drawable-mdpi, drawable-hdpi, drawable-ldpi)
#Jokahero is right about the 9-patch, and #jkhouw1 is right about the resolutions. For checkboxes, you will also need to know about StateListDrawable.