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.
Related
I am trying to achieve this layout. The upper part is a bit dark and it decreases as we move down making it complete transparent. I tried a couple of gradient variations but didn't get the desired results. Does anybody have idea of how to achieve this. Is it gradient or shadow ?
Create a gradient file and use this code you can increase or decrease transparency as you want
layout-->new file-->layout resource file--> give any name
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#B71A1A1A"
android:angle="270"
android:centerColor="#00FFFFFF"
android:endColor="#00FFFFFF"/>
</shape>
</item>
</selector>
I am using the class com.google.android.material.chip.ChipDrawable for defining input text field. I would like the default value to be shown in dark blue gradient color, However I didn't find a way to do that.
Usually gradient color is defined in drawable directory (with the high value and the lowest value the gradient).
in drawable directory:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#6586F0"
android:endColor="#D6D6D6"
android:angle="0" />
</shape>
and on the layout directory there should be something like:
android:background="#drawable/...."
Desired chip with gradient
After the call of EditText.setBackgroundColor(Color.RED) I see only a red rectangle with text inside it, but I want to change only the text background and to keep the standard bottom border which is grey when the field has no focus and blue if it has focus. Can I do it programmatically?
I want to change the text background to red if the input is invalid and to transparent if it is valid.
I think the best way is to work with drawable/shape.xml as background and upon logic code situation call EditText.setBackground(some_other_shape.xml). Here is an example for shape file xml demonstrating how to use:
Border color ("stroke"): In this example some custom color from colors.xml
Fill color ("solid"): In this example Android default transparent color
Even image icon inside
<!--Example for custom shape file xml design-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#mipmap/icon_image" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/orange_primary" />
</shape>
</item>
</layer-list>
So just prepare several shapes for each situation you need. Another approach is to work with layout params etc. but I think this one is faster and gives more control for custom design in easy to understand code.
I am adding a boundary for my layout but its not allowing me define the color other than white or black. Weird! This is a code snippet of boundary.xml in resource folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dip"
android:color="#android:color/white" />
</shape>
You can define your own colors in colors.xml, and then use them in your drawable as #color/mycolor
You can also use directly color codes for the android:color attribute, like #F00, #4F00 (contains alpha channel), #FF0000 or #44FF0000 (again, has alpha channel)
try hex value for color
android:color="#FF00FF00"
use your custom colors, first stored in colors.xml file in values and then get those values for usage..
or set like this
android:color="#FF0000"
I have the following drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2F2F2F"
android:endColor="#5A5A5A"
android:angle="90"
android:dither="true"
/>
</shape>
Is there anyway to start the startColor at 50% or the endColor at 50%?
Are there any links that show me all of the attributes I can apply to a gradient?
These pages show all the attributes you can apply to a gradient.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
To do something in the middle like you are asking, try experimenting with the android:centerColor attribute.
Are you talking about transparency? I'm fairly certain you can just use a 8-digit color code rather than a 6-digit, the first 2 of which constitute the alpha value. So #AARRGGBB, or something like #882F2F2F.