noob question(s) about Android app development

Yeah, I know… Google suggests taking questions about Android app development to Stack Overflow, but time and time again I’ve gotten better results here on the Dope.

I’m wanting to programatically draw a graph, and I’m having a bit of trouble even getting as far as the X axis. More specifically, I’m having trouble passing a color to the line I’m drawing.

For drawing lines, I’ve created a class called DrawnLine. Here’s the constructor:



	public DrawnLine(Context context, int offsetX, int offsetY, int width, int height, int color) {
		super(context);
		
		mDrawable=new ShapeDrawable(new RectShape());
		mDrawable.getPaint().setColor(color);
		mDrawable.setBounds(offsetX, offsetY, width, height);
	}


In addition to the context argument, it takes five integer arguments: 2 for position, 2 for size, and 1 for color. Creating an instance of DrawnLine gets me expected results if I pass a hard-coded color, like this:



        DrawnLine mLine=new DrawnLine(this, 0, 0, 5, 500, 0xffCC0000);


But if I refer to a colors.xml, like this



        DrawnLine mLine=new DrawnLine(this, 0, 0, 5, 500, R.color.axes);


I get nothing. It seems I need to do another step to make #ffCC0000 something that can be passed to DrawnLine (e.g. 0xffCC0000), but what is it?

Also puzzling: the second argument, setting the x offset of the line to any number greater than 5 causes the line to not be visible. Seems this doesn’t do what I think it does (although the y offset behaves exactly as I’d expect).

I’m new to both android development and Java, so I’m sure what I’m overlooking is really basic…

ETA: When I try tracing the value of R.color.axes as a decimal integer (at least, I think that’s what I’m getting), the xml version gives me 2130968576; hard coded I get -3407872. Yes, a negative number.

Pretty sure you’re on the right track here.

I haven’t used DrawnLine specifically, but in general, the resource handle (R.color.axes) is just an index to the item, not the item itself. You can see the generated code in R.java, under the gen folder. I’d bet if you search for R.color.axes, you’ll find it evaluates 0x7F04000 - in other words, the hex value of 2130968576.

To actually use the resource, you need to expand it into the item you want. Try changing your DrawnLine constructor to:


DrawnLine mLine=new DrawnLine(this, 0, 0, 5, 500, getResources().getColor(R.color.axes));


Hope this helps - I’ve been bitten by the resource thing more than once, so I’m guessing that’s it, but I’m far too lazy to write code to test it out to see if I’m right. :smiley:

R.color.axes, although an int, is actually just a resource Id, while Paint.setColor expects an RGB value (coincidentally also an int). You’ll have to query the resource manager to convert the resource ID into the RGB value it reflects. (This is by design, so you can retheme your app by changing the resource without having to track down hard-coded color values in code.)

Therefore, if you change this line:


mDrawable.getPaint().setColor(color);

to


mDrawable.getPaint().setColor(context.getResources().getColor(color));

it will work as you expect. However, this will break the other case, where you’re passing in an actual RGB value like 0xffCC0000, since that’s not a resource Id. The best way would be to leave the DrawnLine implementation as is and do the conversion from the calling side, like this:



int rgbColor = getResources().getColor(R.color.axes); 
DrawnLine mLine=new DrawnLine(this, 0, 0, 5, 500, rgbColor);

(Code shown is untested, just off the top of my head.)

If it were me, I’d prefer the latter way, since your DrawnLine constructor’s use of “color” as a param suggests that it should take an RGB value. If you use the former approach, you might want to change that param name to “colorResourceId” so there’s no ambiguity.

BTW, the latter example assumes you’re calling it from an Activity, so “getResources()” is called on “this”. If not, you’ll need a context to call getResources() on (as in the first example).

ETA: Damn, I need to learn to type faster. Athena nailed it.

Thank you both, Athena and sco3tt. It was indeed the case that I was passing the identifier and not the thing itself.

So, onward to my next :smack: moment.