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..?
Related
I have a drawable in this XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="#color/transparent"
android:gradientRadius="200dp"
android:startColor="?attr/primaryDarkTransparent"
android:type="radial" />
</shape>
The XML causes a crash when startColor uses ?attr/primaryDarkTransparent saying:
Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type
Caused by: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type
The dramatic story is that it works very well when I use attr in solid and stroke but I don't know what the hell is going on in gradient.
Any advices will be appreciated.
There are two problems in your shape.
Below Android L (API 21) you can't use attributes in custom drawables, so you should replace ?attr/primaryDarkTransparent with a color reference.
The gradientRadius should be a float. E.g. 200
Workaround for a gradient from some color (could be solid or with some transparency) to the same color with any percentage of transparency.
Note: This is not a complete/general solution to the problem as the result will only have one color and it is applied to an ImageView (although it can be used in other types of Views).
Tested: From API 31 down to 24.
Steps:
Create a shape with a gradient from some color with any percentage of transparency (black, for example, it doesn't matter which, it will be overridden later) into the same color with another percentage of transparency.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:type="radial"
android:gradientRadius="115%"
android:centerX="1"
android:centerY="1"
android:startColor="#fe000000"
android:centerColor="#f0000000"
android:endColor="#50000000" />
<corners
android:topLeftRadius="10dp"/>
</shape>
Then, in your ImageView add the following if you need it in the foreground:
This is the place where we use the attr from our theme to override the color we used in the gradient.
android:foreground="#drawable/rounded_rectangle_with_translucent_gradient"
android:foregroundTint="?attr/colorSurface"
Or these for the background:
android:background="#drawable/rounded_rectangle_with_translucent_gradient"
app:backgroundTint="?attr/colorSurface"
Final result:
Our ImageView over another ImageView and a TextView.
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 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.
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.
Creating a Drawable that is completely empty seems like a common need, as a place holder, initial state, etc., but there doesn't seem to be a good way to do this... at least in XML. Several places refer to the system resource #android:drawable/empty but as far as I can tell (i.e., it's not in the reference docs, and aapt chokes saying that it can't find the resource) this doesn't exist.
Is there a general way of referencing an empty Drawable, or do you end up creating a fake empty PNG for each project?
For me it didn't work when I tried to use #android:id/empty when making an empty drawable. For me, #android:color/transparent was the one.
I use an empty ShapeDrawable, i.e. create my own drawable/empty.xml containing just <shape/>
<shape />
creates an empty shape and does the trick for me. Or create a file empty_drawable.xml containing:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" />
so you can use it elsewhere.
You can also make a shape disappear when the view it is used in is not enabled by creating my_disablable_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="oval">
<size android:height="10dp" android:width="10dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
<item android:state_enabled="false">
<shape />
</item>
</selector>
Use #android:color/transparent and don't forgot add android:constantSize="true" on <selector>
For me, using #android:drawable/empty would show an error and prevent compiling. Using #android:id/empty fixed the error and let me compile, but showed "NullPointerException: null" in the Eclipse Layout editor. I changed it to #android:color/transparent and now everything is fine.
I can't vote up yet or I would have up'ed Emil's answer.
#null in XML has the same effect as using a null as a Drawable in Java.
Kai, I don't know why this is the case, but I've gotten the same issue. I think it might be related to the version of Android you're compiling for? In any case, I found that simply using #android:id/empty where you would use #android:drawable/empty does the trick.
I had the same problem. My App crashed with an empty vector image. I solved it by adding a transparent/invisible path element. I tested it with API 22 and 30.
ic_empty.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:strokeAlpha="0" android:pathData="M0,12h24"/>
</vector>
To create an empty Drawable image, you may use ShapeDrawable with transparent color:
val shapeDrawable = ShapeDrawable(OvalShape())
shapeDrawable.paint.color = context.getColor(android.R.color.transparent)
If the image size is important, use:
shapeDrawable.intrinsicWidth = 100
shapeDrawable.intrinsicHeight = 100