I'm trying to create a game. I need a kitten object with a image displayed. I need to use this object within multiple activities so I decided it's probably better using an outside class.
I used something like this. But the function findViewById(int)
is not aviable from outside an activity. How to do this?
public class Kitten {
Context parent;
View parentView;
ImageView imgBody, imgHead;
Kitten(Context context, View view) {
parent = context;
parentView = view;
//Create body & head
imgBody = new ImageView(parent);
imgBody.setImageResource(R.mipmap.img_kitty_body_white);
addImage(imgBody);
imgHead = new ImageView(parent);
imgHead.setImageResource(R.mipmap.img_kitty_head_white);
addImage(imgHead);
}
private void addImage(ImageView img) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.adoptView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.adoptView);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rl.addView(img, lp);
}
}
The main problem I have, is within the addImage
method...