Android background gradient radial - android

I want that my app have a background gradient radial, and i Think I did it, but in runtime my app show just one color.
Here my drawable background
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="radial"
android:gradientRadius="360"
android:startColor="#color/color_start"
android:endColor="#color/color_end" />
And I call it in my activity XML in this way:
android:background="#drawable/background_app"
PD:Sorry about my bad English

android:gradientRadius="250" will be ignored. You should point to a dimen resource with a px or dp value, like: android:gradientRadius="#dimen/gradient_radius"

Related

Is it ok to use png as a button background?

Is there is any drawback of using png as a background for a Button or EditText. Or should I write a code for XML for background of Button or EditText. I want to know which is the better way.
You can use color,drawable file or image as background
Color
android:background="#2F6699"
Drawable
android:background="#drawable/edittext_background"
Image
android:background="#drawable/ic_launcher"
Drawable file
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="2dp"
android:color="#2F6699"/>
<corners android:radius="3dp" />
<gradient android:startColor="#C8C8C8"
android:endColor="#FFFFFF"
android:type="linear"
android:angle="270"/>
</shape>
I think you can try 9 patch image for button and edittext background.
9 Patch:
9-Patch images are stretchable, repeatable images reduced to their
smallest size; users draw a right and bottom, solid black 1 pixel
border to tell the system how to place the content within the image.
For more information visit this post:
Utility of android nine patch
just put png image into drawable, and than set it in xml via android:background="#drawable/image.png"
Use it in button properties
android:background="#drawable/imageName"

How to reuse drawable

I have a number of background drawables that the only different is the color of the border or the color of the background.
Is there a way to define a general drawable and then add (or change) the attribute I need.
For example here is a rectangular drawable
XML file
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="#dimen/radius_small"/>
<solid android:color="#color/simple_black" />
<stroke android:width="2dip" android:color="#color/simple_white" />
<padding
android:left="#dimen/two_dp"
android:right="#dimen/two_dp"/>
</shape>
and I would like to change the solid color or the stroke color, and not to create a separate drawable for each one.
This assumes that you want to do it programatically. If all you need is to change the background and/or stroke color, here is how you can do it:
Obtain a reference to the shape
GradientDrawable drawable = (GradientDrawable)context.getResources().getDrawable(R.drawable.shape_id)
To change color
drawable.setColor(Color.RED)
To change stroke color and width (notice that you can only change both of them together, so I recommend keeping a variable with the stroke's width in px)
drawable.setStroke(Util.dpToPx(context, 2), Color.YELLOW)

Android enlarge transparent background of icon drawable

I have many different dynamic icons in my Android app and I would like to put a rectangular frame around all of them. I tried to use LayerDrawable but I think it scales the smaller drawable to the size of the larger one so in the end the icons overlap with the frame instead of within it. (the icon drawables are 64x64 while the frame drawable is 96x96). Is there an way to enlarge the transparent background of the icon drawables to the same size as the frame drawable without scaling the actual icon?
Any help would be appreciated!
You should be able to use a LayerDrawable for this; just tested it myself and it seems to work just fine. Define the frame first, then add another item with the specified insets.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000"/>
</shape>
</item>
<item android:left="20dp" android:top="20dp" android:bottom="20dp" android:right="20dp">
<bitmap android:src="#drawable/ic_launcher"/>
</item>
</layer-list>

Dynamic radial circle in the middle of the screen

Why is it so difficult to make this little thing work? : /
I want to create something like android's lock screen.
I have an XML:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="#dimen/dp_5"
android:color="#color/black" />
<gradient
android:type="radial"
android:startColor="#color/green"
android:endColor="#color/red"
android:gradientRadius="#dimen/dp_50" />
</shape>
This circle is a background of a TextView, so the following happens:
1. wrap_content creates a little circle in the center of the screen
2. fill_parent creates an elipse taking the whole screen, instead of a circle
In addition I would like to change the startColor and endColor programmatically - Didn't find any documentations\threads which solved this SIMPLE issue.

How to control the gradient on a shape?

I have a screen with a label on top. This label is done with a TextView.
As a background I'd like to have a vertical gradient, starting with color1, changing to color2 and back to color1.
At the moment I have:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1px" android:color="#000000" />
<gradient
android:startColor="#FFFFFFFF"
android:centerColor="#FF8800"
android:endColor="#FFFFFFFF"
android:type="linear"
android:angle="270"
/>
<corners android:radius="10dp"/>
</shape>
My problem is that the centerColor line is too thin. I want it to ocupy all the letters space.
I can't find any way to make the gradient to be faster.
I've already tried to use a layout-list but with no success.
Any idea?
This is a much more different track to follow but have you considered working with 9-patch image files? This will allow you to stretch the parts of the gradient that you wish to and make the centre line as big as needed by segmenting up the background properly.
Here are some great tutorials I have used to learn about them:
draw 9 patch tutorial
And from Google's own Android mouth:
Android 9 patch
All Drawable resources for Android

Categories

Resources