I am using MPAndroid Chart for my project.
I want to make the styling of LineChart as follows. Basically I want all 4 Quadrants and other styling like gradient colors etc.
First make it to fill color behind line by doing this:
dataset.setDrawFilled(true);
After that you have to give gradient for that you have make an xml file like that in drawables as gradient_chart_color:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#FFF300"
android:endColor="#F5F094" />
</shape>
After that you have to do this :
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
dataset.setFillDrawable(drawable);
Related
how to make a gradient color background like this in xml android?
Can we make like this background in android?
The image you provide is not a gradient it's normal image but to make gradient in android you can do this:
1- create new drawable resource file
2-change selector to shape
3- inside shape u can start creating your gradient like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="90"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
I want to create an ImageView like this one. I know it's a combination of a rectangle and a triangle shape,
but I really don't know how to implement it.
Is it possible in XML?. Does layer-list seem to help in this case?. I would like to have some example drawable xml codes
In XML you can create:
rectangle
oval
ring
line
gradient
and more
Image like this difficult or impossible create in XML, try use vector (.svg) files
Convert .PNG to .SVG
P.S. you can create image like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#EC6118"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="0.1dip"
android:topLeftRadius="7dip"
android:topRightRadius="0.1dp"/>
</shape>
</item>
</selector>
I was wondering, what would be the best way to make a gradient background for a LinearLayout in java ( not xml ) ?
Any ideas?
Thanks!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#006499"
android:endColor="#0093d7"
android:angle="90" />
</shape>
Set here startColor and endColor as your requirement and save this file in drawable folder
and in LinearLayout you can set this as setBackground="#drawable/your gradient filename"
Using java code you can do same thing using GradientDrawable
Besides xml you can also use GradientDrawable it has corresponding methods for all xml attributes
I have the following drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2F2F2F"
android:endColor="#5A5A5A"
android:angle="90"
android:dither="true"
/>
</shape>
Is there anyway to start the startColor at 50% or the endColor at 50%?
Are there any links that show me all of the attributes I can apply to a gradient?
These pages show all the attributes you can apply to a gradient.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
To do something in the middle like you are asking, try experimenting with the android:centerColor attribute.
Are you talking about transparency? I'm fairly certain you can just use a 8-digit color code rather than a 6-digit, the first 2 of which constitute the alpha value. So #AARRGGBB, or something like #882F2F2F.
I try to use the GradientDrawable to set a gradient to some backgrounds and buttons.
Sadly the documentation is not very detailed.
What are the main attributes to configure the gradient? I understand start and endcolor but some of the other attributes could need some explanation.
At the moment I used images as the background for the buttons but a drawable defined in XML would be much nicer.
I try to get a look like this (It is a very light gradient): alt text http://janusz.de/~janusz/RedButton.png
use this xml as background to the imageview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#7c0000" android:endColor="#A71C1C"/>
</shape>
thats it.
I will give the same answer as Praveen, but will try to explain the settings as well.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="#7c0000"
android:endColor="#A71C1C" />
</shape>
android:type
There are 3 types of gradients, the default and the one for this question is "linear". The other 2 are "radial" and "sweep".
android:angle
Counter-clockwise rotation of the gradient, where 0 is | start color --> end color | (horizontally).
android:startColor
Color the gradient starts from, start is defined by the rotation.
android:endColor
Color the gradient ends with, end is defined by the rotation.
android:centerColor
There can also be a color in between the start and end color, if desired.
Code
I originally found this question because I wanted to do it in code. Here is how to do it programmatically.
int startColor = 0xfff6ee19; // yellow
int endColor = 0xff115ede; // blue
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {startColor, endColor});
View myView = findViewById(R.id.my_view);
myView.setBackgroundDrawable(gradientDrawable);
The different orientations in the image at the top can be achieved by changing the Orientation in the constructor.
XML
As already answered, this is how you do it in xml.
my_gradient_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
You set it to the background of some view. For example:
<View
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#drawable/my_gradient_drawable"/>
See also
How to make gradient background in android
GradientDrawable documentation