So I’m trying to make a canvas with a menu options to switch between drawing a line and a circle but I’m having trouble displaying the menu
public class MainActivity extends AppCompatActivity {
final static int LINE = 1, CIRCLE = 2;
static int curShape = LINE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyGraphicView(this));
}
public class MyGraphicView extends View{
int startX = -1, startY = -1, stopX = -1, stopY = -1;
public MyGraphicView(Context context)
{ super(context); }
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY(); break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
stopX = (int) event.getX();
stopY = (int) event.getY();
this.invalidate();
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
switch (curShape) {
case LINE: canvas.drawLine(startX, startY, stopX, stopY, paint);
break;
case CIRCLE: int radius = (int) (Math.sqrt(Math.pow(stopX-startX, 2))
+Math.sqrt(Math.pow(stopX-startX, 2)));
canvas.drawCircle(startX, startY, radius, paint); break;
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Drawing line");
menu.add(0, 2, 0, "Drawing circle");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case 1: curShape = LINE; return true;
case 2: curShape = CIRCLE; return true;
}
return super.onContextItemSelected(item);
}
}
}
So here is the code I’ve also tried putting the menu code in the main activity class but it doesn’t work what should I do. I’m also having the same problem with onOptionsItemSelected
2
Answers
As seen here, the method
onCreateOptionsMenu(...)
should be overriden either…in an
Activity
:or a
Fragment
:I’m taking the same code as you provided and updating in way that you can use the menuOptions in your customView class
Hope this will clear you the concept