How to use gradient in following code

Go To StackoverFlow.com

0

I have defined XML for gradient.

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient 
    android:startColor="#9acd32"
    android:endColor="#ffffff"
    android:angle="180"
    />

i want to use gradient in following code.

XYRegionFormatter regionFormatter3 = new XYRegionFormatter(Color.BLUE);

instead of Color.BLUE

How to use it?

2012-04-04 05:32
by Ravi Ranjan
What is this class XYRegionFormatter - Herry 2012-04-04 05:36
I am ploting a graph in android. XYRegionFormatter is class defined in androidplot.xy.XYRegionFormatter

and this i am using in LineAndPointFormatter lpFormatter1;

lpFormatter1.addRegion(new RectRegion(0, 4, Double.NEGATIVEINFINITY, Double.POSITIVEINFINITY, "R3"), regionFormatter3) - Ravi Ranjan 2012-04-04 05:38

and where you have use Color.BLUE in that class - Herry 2012-04-04 05:38
I have used Color.BLUE in code i have mentioned - Ravi Ranjan 2012-04-04 05:41
You can Pass R.drawable.yourxml in res/drawable folder - Herry 2012-04-04 05:41
my xml file is already in res/drawable folder - Ravi Ranjan 2012-04-04 05:46
check my answer - Herry 2012-04-04 05:50


0

try this...

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:type="radial" android:gradientRadius="250"
        android:startColor="#E9E9E9" android:endColor="#D4D4D4" />
</shape>
2012-04-04 05:43
by Android
this error is coming.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.atos.range/com.atos.range.Range}: android.content.res.Resources$NotFoundException: File res/drawable/shape.xml from color state list resource ID #0x7f020007

But there is entry of shape.xml in R.jav - Ravi Ranjan 2012-04-04 06:04



0

Try this:

XYRegionFormatter regionFormatter3 = new XYRegionFormatter(getResources().getColor(R.color.<xml_file_name>));

Edit:
I realized it needs a Color attribute, while this line tries to cross reference it to a drawable, which is wrong. The gradient actually forms a drawable, while the XYRegionFormatter needs a Color attribute. Therefore, the gradient drawable cannot be used in this case. There are no other possibilities available in the code to use gradient as color attribute, afaik.
Also, since the XYRegionFormatter does not define any ways to use a Drawable(), or even Paint() for that matter, it presently does not seem possible to use a gradient for the scenario you are looking at.

2012-04-04 05:37
by SamSPICA
I have defined gradient in drawable resource. and i tried it. it's not working - Ravi Ranjan 2012-04-04 05:44
add a line android:shape="rectangle" to your shape attribute - SamSPICA 2012-04-04 05:45
this error is coming.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.atos.range/com.atos.range.Range}: android.content.res.Resources$NotFoundException: File res/drawable/shape.xml from color state list resource ID #0x7f020007

But there is entry of shape.xml in R.jav - Ravi Ranjan 2012-04-04 06:04



0

You should do some thing like this .

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient 
android:startColor="#9acd32"
android:endColor="#ffffff"
android:angle="180"
/>
</shape>

this xml will be in your res/drawable with name like shape.xml

Now in you code you can pass this like below code.

  XYRegionFormatter regionFormatter3 = new XYRegionFormatter(getResources().getDrawable(R.drawable.shape));
2012-04-04 05:49
by Herry
this error is coming.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.atos.range/com.atos.range.Range}: android.content.res.Resources$NotFoundException: File res/drawable/shape.xml from color state list resource ID #0x7f020007

But there is entry of shape.xml in R.jav - Ravi Ranjan 2012-04-04 06:04

can you just write code where passed Resource is used in XYRegionFormatter this class - Herry 2012-04-04 06:06
because your should be use this code for set Resource drawable for View.setBackgroundResource();Herry 2012-04-04 06:09
Clear Project and make sure your xml file have no issue .and also check in debug that getResources().getDrawable(R.drawable.shape) this line return some value not null - Herry 2012-04-04 06:11
Ads