Thursday, 29 January 2009

startActivity

So now you want to move onto to another page of you application. You've created another Activity class and filled it with loads of lovely goodness. What you need to do is create a Intent. Intents seem to be used to get things done (nice naming). So the code you need to call to get there is this.
Intent i = new Intent(this, NextActivity.class);
startActivity(i);

You create your Intent object and pass in the current context (this) and the Activity class that you're wanting to move to. All good?

Not quite, if you run the code like this you'll get an error? Why? You haven't added the new Activity class to the manifest? Huh? The manifest needs to be told about all Activities before it'll let you view them.

This seems to be the opposite of the resources situation where things are picked up automatically. I'm sure there's a good reason but still... open up the AnroidManifest.xml and if you're using Eclipse (like me) there's nice easy interface for adding new Activities (under the Application tab).

LayoutParams

Right now we've got an image on the screen (or some other kind of View) and you want to move it around. There's two things here, firstly the method you use for doing this and secondly the context you're in. This is the code you need:
LayoutParams params = new LayoutParams(100,100,100,200);
image.setLayoutParams(params);

The parameters you pass into that object are width,height,x,y so this is quite straight forward right? What you need to remember though is that there are different versions of LayoutParams depending on the type of ViewGroup you're using.

Above I was using a AbsoluteLayout so that LayoutParams object is imported from within this class.
import android.widget.AbsoluteLayout.LayoutParams;

If you import (and therefore pass) the wrong kind of LayoutParams object to a View that's within a different kind of ViewGroup this would cause problems (your app crashing). Eclipse isn't clever enough to sort this out for you so watch out! ;-)

Wednesday, 28 January 2009

getResources getDrawable

OK here's one which seems fairly big. Most of the time you refer to your resources (images, xml layouts etc) using this handy class called R. This is great in Eclipse (another thing I'm new to) because this R class is created automatically from files you drop in folders so Eclipse's auto-complete function can be used to pick up the resources (via this class) without you needing to look in your folder and check filenames etc. Coming from text-editor style web development this seems very nice.

However when I actually want to pick up one of those images and use it in an Activity (e.g. in an ImageView) the R class doesn't get me there because all it returns is the ID of the resource. So what I have to use is another method.
getResources().getDrawable(R.drawable.image);

I know I'm going to be coming back to this one time and again.

findViewById

This is the first thing you need to know. Chances are you have got something in your layout XML that you want to change in some way. First you need to get hold of it!

The function findViewById is your friend here.

Just remember to give the thing an id in your XML file. This seems to be the way to do that: android:id="@+id/fish". The important bit, as far as I can work out right now is the + sign in there. It seems to mean "if you don't know about this already that's cool, just create it!" which doesn't make a huge amount of sense to me yet - but it works.

Preface

OK just to start off I'm a complete novice here. Both to android and to java. Not to programming, I've done some php & flash as3 work, it looks like the flash stuff might be most relevant so far but I'm aware of the vast pool of java knowledge that I've never even dipped my toe into. So a lot of this is likely to be very slow and basic but it might help me a bit with learning.

See you at the beach.