0

I'm developing an android application for restaurant table booking. I need to draw dynamic squares using coordinates. I had used view class to draw rectangle on canvas but couldn't make the canvas rectangle clickable. can someone help me with this?

Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Subrat Pani
  • 851
  • 1
  • 7
  • 8
  • 1
    This question is not for solving a code problem. It would be convenient for you to look for a manual. – Peppo Jan 31 '17 at 10:41
  • `implement OnTouchListener` also in that class and check cordinate touch position. – D.J Jan 31 '17 at 10:43
  • Check this http://stackoverflow.com/questions/21515862/attaching-click-handler-to-canvas-drawrect-object otherwise describe more of your question, share your code to `setOnClickListener`. – Farmer Jan 31 '17 at 10:45

3 Answers3

0

You should not use canvas for this purpose, instead use inbuilt views of android and create it dynamically.

For example:

LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
while(condition)
{
    ImageView image = new ImageView(this);
    //set image properties
    layout.addView(image);
}
Pratik Popat
  • 2,891
  • 20
  • 31
  • can you please write a sample code for 2 rectangles using coordinates and simultaneously making them clickable too. – Subrat Pani Jan 31 '17 at 11:11
0

If the rectangle have a specific site on the app you can use this:

 Display display;
int width;
int width_perc;
int height;
int height_perc;

//INIT 
    display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    width = display.getWidth();
    width_perc = width/100;
    height = display.getHeight();
    height_perc = height/100;

relativeL.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int X = (int) event.getX();
            int Y = (int) event.getY();
            if((X < (width_perc*95))&&(X > (width_perc*60))){
                if((Y < (height_perc*30))&&(Y > (height_perc*5))){
                    accessCalendar();
                }
            }
            return false;
        }
    });
Nainou
  • 1
  • 1
0

I found the solution by drawing the bitmap not according to the screen size of the phone but rather drawing it to the size of the layout used,in my case linear layout

Subrat Pani
  • 851
  • 1
  • 7
  • 8