I have a resource file with the xml definition of a shape which I want to use as a common background for various views..
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#FF0000"/>
</shape>
How do I load this resource at runtime?
I tried to access R.drawable.resource_file_name but this is not recognized (the resource file name does not appear in R.java).
How do I load this resource?
Where did you put your xml file? I have code like this and it works just fine.
After checking that the file is in the drawable dir, try - if you're using eclipse - cleaning the project. I have to do this also from time to time. I don't know why but sometimes the code gets mangled up and views can't be found and runtime and such jokes. Cleaning the project in eclipse helped if something like that happened.
Try to rename the resource name (preferred to be PNG or JPEG) to be only charachters in lower case as Android is picky for naming of resources (special chars not allowed) in Drawable directory.
Related
My primary problem is I want to increase the size of the thumb of the Seekbar, so I found this post: Changing size of seekbar thumb
From there it said i can create thumb_size.xml and add the below code inside:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<size
android:height="40dp"
android:width="40dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:drawable="#drawable/scrubber_control_normal_holo"/>
However then the Android Studio give me error message saying that it cannot find the
scrubber_control_normal_holo
Then I find this post: Android: Where to find the RadioButton Drawable?
However in my SDK folder /platforms/android-17/data/res/drawable folder, I still cannot find this file, I can only find something like:
seek_thumb.xml
but when I try to use it, it still give me error.
At last I can find the resource images in the following folders (for different device size):
SDK folder\platforms\android-17\data\res\drawable-hdpi
SDK folder\platforms\android-17\data\res\drawable-mdpi
SDK folder\platforms\android-17\data\res\drawable-xhdpi
SDK folder\platforms\android-17\data\res\drawable-xxhdpi
SDK folder\platforms\android-17\data\res\drawable-xxxhdpi
I am trying to make a well designed app in Android-Studio, but I just can't get the buttons right. Where can I find good examples for button style?
1st you can get buttons from the default system resources and also download or create your own button img file of at least 30x80
I'm not sure if this would help but you can do it yourself:
In your "drawable folder" you can create a new "drawable resource file" and place this code there:
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="50dp" android:width="50dp"></size>
<solid android:color="#color/colorPrimaryDark"></solid>
<corners android:radius="23dp"></corners>
In the xml file where you'll want to use it, add this piece of code to your "button tag" :
android:background="#drawable/round_button"
I cannot figure out why my Android Studio is giving me this error when I am trying to make my FrameLayout have rounded edges. This is the exact error it gives me "Error:(162) No resource identifier found for attribute 'corners' in package 'android'", I have rebuilt my project to see if that was the problem but fortunately it wasn't, can someone explain. I am running version 0.8 Android Studio and my Lowest SDK Version is API 13.
The XML layout file that gives me the error
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:radius="10dip"/>
<stroke android:color="#000000" android:width="1dp"/>
</shape>
Update
I no longer get the 'corners' attribute error.
I recreated the file by deleting it and creating a new XML file, but when I try to apply this layout to my FrameLayout I get this error "Error:(126, 15) error: method setBackground in class View cannot be applied to given types;
required: Drawable
found: int
reason: actual argument int cannot be converted to Drawable by method invocation conversion"
that's not a layout; it's a shape drawable. ensure it is located in "res/drawable" not "res/layout".
Just a guess: maybe because you are specifying shape as rectangle there is a problem with corners?
Check this answer: How to make layout with rounded corners..?
I asked this question in chat rooms tagged with Android but it turns out they didn't encounter this before, and I cannot find specific question either on Google so I think this is a valid point to post this as a question here.
The problem is, if I refer to an XML file format as android:background="#drawable/bg", I got compilation error saying:
No resource found that matches the given name (at 'background' with value '#drawable/bg').
But if I put some file like for e.g ".png" or ".jpg", it compiles well. Sounds like it does not recognize the file as a valid drawable? Either if I put that file in other location, its just don't compile.
This is the bg.xml contains:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<stroke
android:width="1dp"
android:color="#fff" />
</shape>
What am I doing wrong?
Edit:
I've create the "res" and "drawable" folders by myself because it's not default in this kind of environment.
Set the Build Action property of item bg.xml to DrawableResource.
I am also facing this problem in VS 2013. Then i do this..
In the solution explorer click on drawable folder and check if your image exists here or not. If not then right click on drawable folder and click add-> existing item and then add your Image here.
In the activity_main.xml, there are two rows code android:background="#drawable/border_ui" and android:background="#drawable/my",
but the one use the border_ui.xml located the folder res\drawable, and the other use the file drawable.xml located the folder res\values.
What different? Thanks!
And more, an error will occur if I remove the file drawable.xml to the folder res\drawable and rename it as my.xml.
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/border_ui"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:background="#drawable/my"
/>
</RelativeLayout>
border_ui.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#ffffff" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
drawable.xml
<resources>
<drawable name="my">#f00</drawable>
</resources>
The difference is one, the border, is involving a shape which is a drawable entity. The other, the values resource, is referencing a color which is acceptable as a drawable as shown in a note here - like so =>
Note: A color resource can also be used as a drawable in XML. For
example, when creating a state list drawable, you can reference a
color resource for the android:drawable attribute
(android:drawable="#color/green").
I would imagine the background in question looks something like this. (#f00 represents a websafe color)
Edit:
Please see this article about color drawables.
What it says, and what the Android Drawable Resources shows, is that files used for specific purposes like any of the drawable types shown require certain tags to be the root. Just removing the <resource> tag as has been suggested is not going to work. If you're wanting to force the color drawable into another resource folder other than where it belongs (values), which I can't really see why, then you're going to have to hack it.
Final word:
You asked why they're in different places and why you can't just rename and move it into another folder of your choosing - both of which have been answered. I believe if you're expecting a solution on moving the file to the drawable folder then another question asking such would be in order. Please consider upvoting or accepting an informative answer.
You can change the name of the file, but don't move it to the drawable folder. Keep it where it is currently, and it should work fine. drawable.xml is not a keyword. But you can't have this file, which contains a list of resources in the drawable folder.
Since your drawable is a resource, it should be inside the values folder instead of the drawable folder.
it's from android documention about String but it's can explain about :
Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one element.
the syntax of drawable is without element but regular xml file
In fact, the drawable.xml file is a file containing resources, but not only drawables.
So, if you want to move it to the folder drawable and rename it my.xml, it is ok. You should just remove the <resources> & </resources> tags and it should work.