Drawing on canvas overriding onDraw() method
Assuming using RelativeLayout in xml and with id "layout1"
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
Assuming using RelativeLayout in xml and with id "layout1"
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//setContentView(new MyView(this));
RelativeLayout layout = (RelativeLayout)(this.findViewById(R.id.layout1));
layout.addView(new MyView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_second, menu);
return true;
}
class MyView extends View{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
// if required to add this custom view using xml or the graphical layout in eclipse. have to add this constructor
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas){
System.out.println("draw canvas");
Paint paint = new Paint();
paint.setColor(0xFFFF0000);
Rect rect = new Rect();
rect.set(100, 100, 200, 150);
canvas.drawRect(rect, paint);
}
}
}