How do I specify that the background "colour" of a Android layout view element should be a gradient (at a specific angle) ?
I wish to specify this in the XML, i.e. not at runtime. Preferably as a style I can apply to any layout I wish with the style property?
android:background="" is the attribute used to set background for any Layout file. You can directly specify the value as HEX color code as we do for CSS files in HTML. You can also add transparency to the color by adding 2 more hex numbers after the # (hash) symbol.
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
You can use something like this:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#A1A1A1" android:centerColor="#BDBDBD" android:endColor="#A4A4A4" android:angle="-90" /> </shape> to build a gradient (You choose the colors you like ). Place this in drawable and voila you got your own shape to use as background: android:background="@drawable/the_name_of_your_xml"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With