How to set background image for button in Android? - android

I Imported one image inside the drawable-mdpi, then implemented the image from button, but an error occurs no resource found here. How do I fix this issue?
I tried this:
main.xml
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable-mdpi/button_focused_orange"/>

All drawables are compiled under a single resource name, i.e. drawable. Android automatically chooses from which folder to take the drawable depending on the screen size, and hence you do not need to specifically point it out. Also, hard coding Android to use resources from a particular folder kind of defeats the purpose of having multiple folders for Android to choose from. To solve this issue, simply change:
android:background="#drawable-mdbi/button_focused_orange"/>
To
android:background="#drawable/button_focused_orange"/>

Should be #drawable/button_focused_orange
Not #drawable-mdpi/button_focused_orange

Try to clean and rebuild your project.
If you are usuing Eclipse you can do this by clicking project -> clean and then project -> Build project

You don't have to mention -mdpi to add background images, simply use drawable only. Here is you revised code. Try this.
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_focused_orange"/>

Related

Android studio can not detect the source file in res/drawable

This is my code. I don't know how to post this image.
I tried to add source file for my ImageView. But it doesn't work. I tried to copy the path folder and it is still not work.
Solution - first problem
The resource name must start with a letter (not number).
Solution - second problem
Use just:
#drawable/039_cloud
instead
#drawable/039_cloud/039_cloud
so:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/039_cloud" />
Android Studio
Android Studio is grouping same images, but for different DPIs.
When you choose Android view, you will have:
But when you will choose Project you will see "real" project structure:
Copy the image resource and paste it directly inside the drawable folder. Rename the image such that no number is included (e.g. image_thirty_nine.png). Then try this
<ImageView
android:id="#+id/image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_thirty_nine" />
If you are till getting the same error after trying the above step, then try adding the image resource programmably
//ensure you assignn an id to your image
ImageView imageView = findViewById(R.id.image_id);
imageView.setImageResource(R.drawable.image_thirty_nine);
then run your code to see if it works

How to fix android error 'Color value must start with #'?

I was trying to use an icon from this page in my layout as follows
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backspace_black_24dp"
/>
but it could not be rendered because of an error
Color value '#drawable/ic_backspace_black_24dp' must start with #
I found two related issues in SO here and here which did not help resolve the issue.
There is no dash ('-') in the filename, the file-format is a 'png' and there is no res/color directory in my project.
All my colors defined in colors.xml start with a #.
So how to fix this error?
Simply rename your file name by removing the numbers and only keep lower case letters or underscores.
Problem solved: I forgot to remember that the android sources on the internet are really outdated, and almost none of the documentation works as-is!
The folder res/drawable as suggested in the android developer guide is deprecated! The folder is now named mipmap! So the simple solution is to have the following layout to fix the problem:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_backspace_black_24dp"
/>
Never trust the android developer documentation ... !
Try removing the '_" underscores and make the name shorter. Not sure why but that worked for me.
Check the file drawable/ic_backspace_black_24dp.
If you downloaded the SVG version you will able to open as a text file. Check if the android:fillColor has the #.
Anyway if you are using Android Studio those icons can be downloaded very easily using Vector Asset Studio:
Right click in drawable
new
vector assets
choose the icon you want
You have more info of this feature here.

R doesn't generate button

I've just finished to do the layout I want, and even although I saved everything, when I try to find by id the button, the button I'm looking for is missing.. Both the button's I have.. I've checked in the R class, under the ID, and it seems it doesn't sees' the buttons. Doesn't generate them? I dunno.. Any way, it happens every time I use relative layout!! Is there any connection? It works with TableLayout and with linear layout.. Only with relative layout it make's problems.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background" >
<TextView
android:id="#+id/corpTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/corpTag"
android:textColor="#color/White" />
<Button
android:id="#+id/addBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="208dp"
android:text="#string/addYourBusiness" />
<Button
android:id="#+id/searchBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignBottom="#+id/addBusinessButton"
android:layout_alignLeft="#+id/addBusinessButton"
android:layout_marginBottom="36dp"
android:text="#string/searchBusiness" />
I found this happening to me with a broken layout. No need to be worry. I am trying my best to giving you the solution
Solution
Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
If you somehow hit something and created import android.R in your activity, remove it.
Run Project -> Clean. This will delete and regenerate R and BuildConfig.
Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
Wait a few seconds for the errors to disappear.
This problem isn't related to relative layout, but is actually set on the background because Android drwable is case-sensitive and now allows you any capital letters for image or layout. Rename your image name or drwable name to "background" instead of Background."
Here is the problem with set background name:
android:background="#drawable/Background"
It might be the Android Style ButtonBar. It isn't available for every API level. So if you have a low minSDK, it's not going to build and Eclipse/IntelliJ should give you an error message in its error pane.
The rule given by Google, is that you should always define your own resources. If you want to use a resource that is in the Android name space, that's fine, but you should copy it first and package it with your apk, because that's the only way you can be assured that it will always be bundled with your application.
And yes, I realize many people and many tutorials on the web don't do it that way, but that's because most people are lazy.

Android FileNotFound Exception on custom.xml in the drawable-hdpi folder

I keep getting a FileNotFound Exception, and quite frankly, it makes no sense. Here are my files:
custom.xml
<item android:drawable="#drawable/easymode"></item>
main.xml
<Button android:id="#+id/guess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_marginBottom="20dp"
android:onClick="myClickHandler"
android:background="#drawable/custom"></Button>
I've tried moving the background tag above the others to see if that was causing it to fail, it didn't work. I've moved the custom.xml file into a drawable folder, and into the ldpi and mdpi folders but it keeps giving me the same error. Why can't it find my custom.xml when it's quite clearly there? R.java also lists it, so I know it's generated the link to it successfully... Help!
Have you tried building the R file again after saving? It can be a little confusing when working with the Android SDK in Eclipse (i'm guessing you are using Eclipse). After adding your new layout xml, are the ids for buttons and strings correctly parsed and identified in your code?
I would do a save all and then compile the project and then run it and see if this sorts your problem out.
The order of the tags makes no difference here as the layout xmls are compiled into a compact resource.
If it still cannot find the xml I suggest deleting the xml, restart Eclipse and create it again under layout, add your code above to the xml, save, compile and run and see if that sorts it out.

Android XML Viewer failing

I've been having this problem for a while:
Here is my header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/logo_text"/>
</LinearLayout>
And this what the Eclipse/Android XML viewer is giving me:
Unable to resolve drawable "C:\Users\Me\MyApp\res\drawable-hdpi\logo_text.png" in attribute "src"
This happens in other projects as well for seemingly random images. Project->Clean doesn't help either. I know this has to be an issue with my machine or eclipse installation because it doesn't happen on my work machine, only my home laptop.
There are no problems when the app is installed, but it would be nice to be able to see layout changes without having to install the app over and over again.
EDIT
I also get these messages anytime I use a custom View in a layout file:
error!
UnsupportedClassVersionError: Bad version number in .class file
Unable to resolve drawable "C:\Users\Me\MyApp\res\drawable-hdpi\logo_text.png" in attribute "src"
Exception details are logged in Window > Show View > Error Log
I sometimes have weird issues like this when importing new images into my project. Do you have any other res-image directories with different versions of this file? You may try removing the png and re-adding to the project.
The error message explicitly mentions the -hdpi version of the drawable directory. Have you got that image in all of the different versions of the drawable directory? Perhaps it is in res/drawable-ldpi or res/drawable-mdpi but not res/drawable-hdpi?
If you don't already have one, you could create a plain res/drawable folder and add the image there, then one would expect the system to fall back to that whenever it didn't find a definition-specific version.
Actually try to put an id tag on ImageView : android:id=:"#+id/imageView".Tell me if it is working or not.

Categories

Resources