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.
Android LinearLayout Gradient Background
Ok I have managed to solve this using a selector. See code below:
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/main_header_selector">
</LinearLayout>
main_header_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
Hopefully this helps someone who has the same problem.
Set the background colour of a Layout view to a gradient in Android?
create gradient.xml
in /res/drawable
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:endColor="#00000000"
android:angle="45"/>
</shape>
and in your main.xml
layout file in /res/layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient"
>
</LinearLayout>
you can specify the angle by replacing the android:angle
value and start/end colour by replacing android:startColor
and android:endColor
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
.
How to set a gradient background in a Material Button from Material Components?
From the documentation for MaterialButton
:
Do not use the
android:background
attribute. MaterialButton manages its own background drawable, and setting a new background meansMaterialButton
can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed,MaterialButton
cannot guarantee well-defined behavior.
The only supported way to modify the background of the button is to set a color value to the app:backgroundTint
attribute. Unfortunately, gradients are not supported here either; you can only use a simple color value or a ColorStateList
.
So, there is no supported way to use a gradient background with MaterialButton.
How to make a transparent white gradient background
Try below code may help you to solve your problem
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#ffffff"
android:centerColor="#90ffffff"
android:startColor="#65ffffff"
android:angle="-90"/>
</shape>
Button background gradient is not showing
I got the solution. By adding this code in the button xml file, it worked just fine.
app:backgroundTint="@null"
Related Topics
Any Way to Run Shell Commands on Android Programmatically
How to Make an Imageview in Circular Shape
Adt 22.2 New App Wizard: Unsupported Template Dependency: Upgrade Your Android Eclipse Plugin
How to Write Vertically in a Textview in Android
How to Get City Name from Latitude and Longitude Coordinates in Google Maps
How to Download the Android Sdk Without Downloading Android Studio
Linearlayout Not Expanding Inside a Scrollview
How to Get the List of Mounted External Storage of Android Device
Android Studio Isn't Opening on Windows
How to Rename the Android Package Name
Android Usb Host and Hidden Devices
How to Call a Local Web Service from an Android Mobile Application
Draw Overlay in Android (System Wide)
Display Back Button on Action Bar