I am trying to create an ImageView in code by setting an image resource, and then add an ImageView as a child view in the main view. All the examples that I found used a layout for this. But inside the constructor of my view, I cannot figure out how to do this.
Here are the code snippets:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CanvasView(this));
}
}
View:
public class CanvasView extends SurfaceView implements SurfaceHolder.Callback {
public CanvasView(Context context) {
super(context);
SurfaceHolder sh = getHolder();
sh.addCallback(this);
ImageView iv = new ImageView(context);
iv.setImageResource(R.drawable.wand);
}
}
source
share