views:

33

answers:

1

I would think this should be a relatively easy question to answer, but I'm brand new to android development and so not sure where else to turn.

I have a simple layout (relative currently) for my main Activity. I want to create an object (a card for a card game) that will have all of the necessary properties for that card (value, suit, etc). I have images for all of those cards in my drawable folder.

For the sake of testing, I created a card.xml file in my layout folder and "hardcoded" in the background for one card.

My question is, now that I have an xml file with the layout for my card, how should I plug this into an object that contains info about that card and then how do I pull that object onto my main scene? Should I create the card object as another activity and use setContentView() to reference the card.xml in the layout folder? As I said, I'm just starting android development and not entirely sure how Activities work. Assuming this is the correct method, how would I then pull this second activity onto my main scene?

TIA

A: 

Welcome to android programming!

What you should do is to create a custom component by creating a new class that extends a view (like View or your root element in card.xml) and set it to use ur xml file. You can find a guide here: http://developer.android.com/guide/topics/ui/custom-components.html

In this custom component you'll be able to save everything you need. To use your component, just use it in an xml file like you would do with a button or a textview, you can find out how to in the link i posted.

softarn
This looks promising. I read over the link, albeit briefly, and I've decided to try and extend ImageView as that seems the most logical (the card is essentially just an image). Eclipse is telling me that I need a constructor that takes one argument (Context). Sorry for my newbness, but what exactly does the context param do? And how can I yank in my card.xml file to layout the image within the class?
Kyle
You can use your xml file by inflating it in the onFinishInflate() method.((Activity)getContext()).getLayoutInflater().inflate(R.layout.card, this);Here is another short guide for this: http://maohao.wordpress.com/2009/08/27/building-mix-up-custom-component-android/Explaining what context is and why its there is to hard for me, try google it.
softarn
ok one other quick question. I created a class that extends ImageView called Card. When I try putting a reference to that new class in my main.xml file, it won't compile. If I change it to ImageView however, it compiles fine. What do I need to do in my Card class to expose it to the mail.xml (Main Activity)?
Kyle
Step five in the latter link I posted covers that part. You shouldn't have to do anything special in the card class.
softarn
ok I added the fully qualified name to my instance in main.xml and it now compiles, but as soon as the app loads, I get the force close screen. How can I debug that, or better yet, what else could be causing the problem?
Kyle
To me it sound like you have missed out on a lot of stuff in android programming, or maybe starting in the wrong end. I would recommend you buying a book that explains how android works from the basics, like Hello Android from pragprog. You will save yourself a lot of time not havin to ask questions. Use Log.d to send debug messages. Good luck!
softarn
nevermind I got it, I was missing the second constructor with the AttributeSet. Thanks for all your help!
Kyle