I would like to use a gradient color instead of the traditional solid color for a Floating Action Button.
I already know how to modify the background color of the button, using ColorStateList. Is there a way to implement the following drawable as a background tint color?
overlay.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#c25f82"
android:endColor="#5a3e60"
android:angle="-270" />
</shape>
Yes, you can set a Gradient to a FAB, I've just found a heck.
1.) Create a gradient.xml in drawable folder and add the following code in it,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#03a9f4"
android:endColor="#e91e63" />
</shape>
2.) In your dimens.xml add this piece of line,
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
3.) In your Fab add android:src="#drawable/gradient"
No. As you already noticed, tint is always a solid color. You can set anything as a background, but I guess such approach would break the support FAB.
Related
I have an AppCompatSpinner with a dropdown menu custom background with rounded corners. When I tap an option in the AppCompatSpinner, it shows the ripple background however the ripple goes outside the round corners and forms a normal rectangle. How can i make it so that the ripple fit in the background of popup?
This is how it looks now, ripple effect goes out of rounded background:
Im using popupBackground property to set rounded corners background.
MyActivity:
<androidx.appcompat.widget.AppCompatSpinner
popupBackground="#drawable/background_white_corners"
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="50dp"
...
/>
background_white_corners.xml
<?xml version="1.0" encoding="utf-8">
<shape ... android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius ="30dp"/>
</shape>
My item spinner is just a TextView, without any layout.
<?xml version="1.0" encoding="utf-8">
<androidx.appcompat.widget.AppCompatCheckedTextView ...
android:id="#+id/textview_spinner_item"
/>
Try to use the setClipToOutline(true) on the AppCompatSpinner.
You could also do it in the XML with android:clipToOutline but it's available in API 31+ only
You should use ripple tag inside background drawable for ripple effect in this way:
<?xml version="1.0" encoding="utf-8">
<ripple ....>
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius ="30dp"/>
</shape>
</ripple>
I was wondering if there is any way to set a gradient background color to a Card from the Material Components. I was reading through the docs, but I couldn't find anything.
Create an xml below res/drawable and set the background of your cardview to this xml.
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/colorPurple"
android:endColor="#color/colorBlue"
android:angle="0"/>
</shape>
The standard Android Image Studio only allows me to select a single color as the background for my icon. Is there a method for selecting two colors and creating a linear gradient effect?
Create an color_gradient.xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#color/start_gradient" <!--Start color of the gradient-->
android:endColor="#color/end_gradient" <!--End color of the gradient-->
android:angle="45" /> <!--Change this to see the combination of gradient color-->
</shape>
</item>
</selector>
When using for your image:
android:backgroundTint="#drawable/color_gradient
EDIT:
In case of using Android Image Asset, you still can link the background color to this xml file, to create a gradient background
I am trying to implement a ripple effect on click of a relative layout which already has a background XML. But how can I do this? If I want to add the ripple effect to the relative layout then I must change the tag in the background XML to <ripple></ripple>, but when I do this I lose the corner radius and solid color that I had given for the relative layout.
Below is the code of the relative layout:
<RelativeLayout
android:layout_width="400dp"
android:layout_height="100dp"
android:background="#drawable/roundedcorners">
</RelativeLayout>
Below is the code in the background XML "roundedcorners"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners
android:radius="25dp"/>
</shape>
I looked out for posts for the same but did not find one, as the posts had just the information on how to achieve the ripple effect, but not on how to merge and use the ripple effect, scale, or shape tags together.
The solution for this lies in the special mask layer of the RippleDrawable. You specify the mask layer via the android:id value set to #android:id/mask.
For the example below, you can set the mask to the same size/shape as the view you’re masking, and then the ripple will only show for that area. Do something like this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape
android:shape="rectangle">
<solid android:color="#000000"/>
<corners
android:radius="25dp"/>
</shape>
</item>
<item android:drawable="#drawable/rounded_corners" />
</ripple>
now you can set it as your android:background
How do I change the background color of a custom shape like this:
<?xml version="1.0" encoding="UTF-8"?>
<inset android:insetLeft="1.0px" android:insetRight="1.0px" android:insetTop="0.0px" android:insetBottom="1.0px" xmlns:android="http://schemas.android.com/apk/res/android">
<selector>
<item>
<shape>
<stroke android:width="1.0px" android:startColor="#color/grey_rounded_container_slider_start" android:endColor="#color/grey_rounded_container_slider_end" />
<gradient android:startColor="#color/grey_rounded_container_slider_start" android:endColor="#color/grey_rounded_container_slider_end" android:angle="270.0" />
<corners android:radius="10.0dip" />
</shape>
</item>
</selector>
</inset>
programmatically? I would only need to change the gradient(background color). So I assume I need to give the gradient an id? And then how should I use view.findViewById(R.id.name) to change my gradient color? Thanks.
You can't change the color of drawable programmatically. Alternatively You can have more than one shape, And change the drawable file programmatically.
You can use following code snippet for change programmatically.
if(true)
yourview.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape1))
else
yourview.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2))
I hope this will help you.