skip to Main Content

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


  1. As seen here, the method onCreateOptionsMenu(...) should be overriden either…

    in an Activity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    

    or a Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.main_fragment, container, false);
        setHasOptionsMenu(true);
        return root;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_menu, menu);
    }
    
    Login or Signup to reply.
  2. I’m taking the same code as you provided and updating in way that you can use the menuOptions in your customView class

     public class MainActivity extends AppCompatActivity {
        final static int LINE = 1, CIRCLE = 2;
        static int curShape = LINE;
        MyGraphicView myGraphicView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            myGraphicView=new MyGraphicView(this)
            setContentView(myGraphicView);
        }
        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)
            {
    
                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 null;
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            return myGraphicView.onCreateOptionsMenu(menu);  // must be the Obj of MyGraphicView
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            Boolean result= myGraphicView.onOptionsItemSelected(item); // must be the Obj of MyGraphicView
            if (result!=null) return result else
            return super.onContextItemSelected(item);
        }
    
    }
    

    Hope this will clear you the concept

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search