I have something to draw at runtime. I did drawing in onDraw in MyView class.
Because, I already used setContentView(R.layout.main) in onCreate, I cannot use it again.
How to call onDraw after setContentView(R.layout.main)?
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // I have something to draw in XML also.
MyView myView = new MyView(this);
// setContentView(myView); I cannot use setContentView two times.
}
protected class MyView extends View {
public MyView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
// there are some drawing codes and these cannot be done in XML.
}
}
I see two ways
1:
You could add your myView (instance) to an ViewGroup defined in R.layout.main
2:
You could directly add use MyView in your XML R.layout.main.
Instead of “LinearLayout” and so on, you the full qualified class name
see http://developer.android.com/guide/topics/ui/custom-components.html (at bottom)