java - OnClickListener will not detect click on rectangle? -


i have 2 rectangles each rectangle taking half of screen , trying detect clicks on rectangles.

what tried:

i tried set onclicklistener on both rectangles when click either rectangle click not detected. sure wasn't detecting click logged inside listeners , confirmed suspicions; listeners weren't being entered , therefore no click being detected.

here declare listeners in activity:

toprectangle = new rectangle(this, 0, 0, mscrwidth, mscrheight/2, 0xc757ff57);     bottomrectangle = new rectangle(this,0, mscrheight /2, mscrwidth, mscrheight, 0xdeff5845 );      mainview.addview(mline);     mline.invalidate();      mainview.addview(toprectangle);     toprectangle.invalidate();      mainview.addview(bottomrectangle);     bottomrectangle.invalidate();      view.onclicklistener toplistener = new view.onclicklistener() {         @override         public void onclick(view v) {             if (toprectangle.getcolor() == 0xc757ff57 /*green*/) {                  if (!isgamestarted) {                     isgamestarted = true;                 }                  toprectangle.setcolor(0xdeff5845);                  if (movingup) {                     movingup = false;                 } else {                     movingup = true;                 }             }             else if (toprectangle.getcolor() == 0xdeff5845 /*red*/) {                 /*player dies*/             }         }     };      view.onclicklistener bottomlistener = new view.onclicklistener() {         @override         public void onclick(view v) {             if (bottomrectangle.getcolor() == 0xc757ff57 /*green*/) {                 bottomrectangle.setcolor(0xdeff5845);                  if (movingup) {                     movingup = false;                 } else {                     movingup = true;                 }             }             else if (bottomrectangle.getcolor() == 0xdeff5845 /*red*/) {                 /*player dies*/             }         }     };      toprectangle.setonclicklistener(toplistener);      bottomrectangle.setonclicklistener(bottomlistener);      toprectangle.setclickable(true);     bottomrectangle.setclickable(true); 

here class have rectangles:

public class rectangle extends view {  public float left; public float top; public float right; public float bottom; public int color; private final paint mpaint = new paint(paint.anti_alias_flag);  public static int[] colorarray = {0xff949cff, 0xffeef16b, 0xffffad85, 0xffafff78, 0xffe3abff,         0xff7dffe0, 0xfffebc71, 0xffa877fb, 0xff62ff8b, 0xfff99aa1, 0xffa9ff53,         0xffd02a21, 0xff1d1ad0, 0xffced07e, 0xff60b4ff, 0xffffa1e0};   /*faded blue, yellow, salmon, green, pink-red, light blue, light orange, purple, teal, pink, light-green, red, blue, sand, lighter blue, pink*/ public static random randomgenerator = new random();   public int getcolor() {     return color; }  public void setcolor(int color) {     mpaint.setcolor(color);     this.color = color; }   public int getthebottom() {     return (int) bottom; }   public int gettheleft() {     return (int) left; }   public int getthetop() {     return (int) top; }   public int gettheright() {     return (int) right; }  //construct new rectangle object public rectangle(context context, float left, float top, float right, float bottom, int color) {     super(context);     //color hex [transparncy][red][green][blue]     mpaint.setcolor(color);  //not transparent. color white     this.left = left;     this.top = top;     this.right = right;     this.bottom = bottom; }  //construct new rectangle object public rectangle(context context, float left, float top, float right, float bottom) {     super(context);     this.left = left;     this.top = top;     this.right = right;     this.bottom = bottom; }  //qcalled invalidate() @override protected void ondraw(canvas canvas) {     super.ondraw(canvas);     canvas.drawrect(left, top, right, bottom, mpaint); }  public void setx(float left, float right) {     this.left = left;     this.right = right; }  public void sety(float top, float bottom) {     this.top = top;     this.bottom = bottom; }  public int getrectwidth() {     return (int) (right - left); }  public int getrectheight() {     return (int) (bottom - top); }  public int getcenterx() {     return (int) (right + left) / 2; }  public int getcentery() {     return (int) (top + bottom) / 2; }  } 

what have referenced:

onclicklistener won't work

android: onclicklistener not work programmed

question:

why no click being detected on rectangle views?

you have told view when user clicks on looks view not clickable. try this:

toprectangle = new rectangle(this, 0, 0, mscrwidth, mscrheight/2, 0xc757ff57); toprectangle.setclickable(true);  bottomrectangle = new rectangle(this,0, mscrheight /2, mscrwidth, mscrheight, 0xdeff5845 ); bottomrectangle.setclickable(true); 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -