Can GradientColor be used to define a gradient for a fill or stroke entirely in XML?
I've finally made it work. Gradient color feature is not supported yet in Android Studio (current ver is 2.2) so it doesn't help you with autocomplete but marks gradient tag as error instead. Nevertheless, the feature does actually work, I've tested it successfully on Nexus 5X / API 24. Of course, you have to use an API 24+ device because otherwise this feature is not supported by OS.
First, you need to add color resource file like this:
<?xml version="1.0" encoding="utf-8"?>
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
android:startColor="#FFFFFF"
android:centerColor="#0000FF"
android:endColor="#00FFFF"
android:angle="145"
android:startX="30"
android:endX="70"
android:startY="30"
android:endY="70"
android:type="linear"/>
Please pay attention to start/end parameters as I found they are essential for vector gradients.
Place this file into res/color folder under some name. I've named it gradient.xml so the full path is res/color/gradient.xml. After that you can refer to this resource in color attributes, including vector path colors:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="120dp"
android:height="120dp"
android:viewportWidth="120.0"
android:viewportHeight="120.0">
<path
android:name="play_triangle"
android:pathData="M 30 30 L 30 90 L 80 60 z"
android:strokeWidth="10"
android:strokeColor="@color/gradient"/>
</vector>
Notice the reference to gradient color resource in strokeColor. Hope this helps!
How to make gradient background in android
You can create this 'half-gradient' look by using an xml Layer-List to combine the top and bottom 'bands' into one file. Each band is an xml shape.
See this previous answer on SO for a detailed tutorial: Multi-gradient shapes.
Color of Primary Dark to be Gradient
This will break starting from Android N (API 25) with the following error:
android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID...
It's pretty much the same error as this SO issue, and as pointed out by an answer, the error/crash is intentional.
I guess the reason is because the Android gods are now being strict with not letting you use something other than color for a @color resource. So, this is definitely not the "right way" to do it.
A workaround would be to use a custom toolbar where you can use the drawable gradient for the background.
I did try this other solution on SO, which claims to work for API 24+, but alas it breaks in API 25 & 26. It does seem like something Android should simply work with but just doesn't..
Set gradient on stroke android
You should do something like this. Use layer-list
with 2 shapes. First one is for gradient stroke and second one is for solid.
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval" >
<gradient
android:angle="360"
android:startColor="#543456"
android:endColor="#ff00b5"
android:type="linear" />
<size android:width="24dp"
android:height="24dp"/>
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="oval" >
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
This code looks like this
CardView: How do I add a gradient background while maintaining the radius
If I may ask, are you per-chance testing/running on a pre-lollipop Android device? Your code seems to work as you desire (curved corners showing with the gradient) except on Android 4.
To achieve the desired result on pre-lollipop devices, you can add <corners android:radius="4dp" />
to your @drawable/btn_gradient
file, (you would have to set the corner radius to match the CardView
's cardCornerRadius
.
How to set a gradient background to a Material Button?
To use a custom drawable background with the MaterialButton
you can use the android:background
attribute:
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/bkg_button_gradient"
... />
NOTE: It requires at least the version 1.2.0-alpha06
Currently it is very important to add app:backgroundTint="@null"
to avoid that the custom background doesn't get tinted by default with the backgroundTint
color.
With lower versions of the Material Components Library you have to use an AppCompatButton
.
Related Topics
How Can Retrofit 2.0 Parse Nested JSON Object
Recyclerview Items with Big Empty Space After 23.2.0
How to Get the Battery Level After Connect to the Ble Device
How to Get the Current Dns Servers for Android
How to Implement the Deprecated Methods of Notification
Does Retrofit Make Network Calls on Main Thread
Uploading Image from Android to Gcs
Taking a "Screenshot" of a Specific Layout in Android
Send Request Over Wifi (Without Connection) Even If Mobile Data Is on (With Connection) on Android M
No Notification Sound When Sending Notification from Firebase in Android
"Unknown Bits Set in Runtime_Flags: 0X8000" Warning in Logcat on Android Q Emulator
Debugging Android Ndk Native Apps
Delete File from Internal Storage
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Switching Between Fragment View