0

I wrote the following code and it works well. But I have other purpose. I want to click only on a view to doing the operations.First, Please see the following image:

enter image description here

MY CODE IS AS FOLLOWS:

public class MainActivity extends Activity {
    RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle bundle)
    {        super.onCreate(bundle);
    relativeLayout = new RelativeLayout(getApplicationContext());
    setContentView(relativeLayout);
        A a = new A(this);
        relativeLayout.addView(a);
        a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                B b = new B(getApplicationContext());
            relativeLayout.addView(b);
            }
        });
    }
}

class A extends View {
    Paint paint;
    A(Context context) {
     super(context);
    paint = new Paint();
    }
@Override

    protected void onDraw(Canvas canvas) {
    paint.setAntiAlias(true);
paint.setColor(Color.RED);
    canvas.drawRect(20,60,100,150,paint);
}
}

class B extends View {
    Paint paint;
    B(Context context){
        super(context);
        paint = new Paint();
    }
@Override
    protected void onDraw(Canvas canvas){

    paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
canvas.drawRect(100,150,200,250,paint);
}
}

when I run the above code I can see the green rectangle after press on the red rectangle. But the problem is that when I press another places on the screen I can do this operations also. I want that only I can see the green rectangle to press on the red rectangle and not in the another places on the screen to doing this operations.

and
  • 341
  • 1
  • 3
  • 11
  • Did you try checking what's the view that triggered the click? Inside your `clickListener` you do `if (view == a)` and only creates `B` inside it – Edson Menegatti Sep 02 '15 at 17:42
  • you can simply achieve this by making xml for the layout . add view tag and place the view . keep second layout visibility GONE and onclick of red view set visibility to VISIBLE – N J Sep 02 '15 at 17:43
  • 1
    This is just a hunch, but perhaps the canvas is much larger than the square you're drawing? Since the canvas owns the listener, you can press anywhere on the canvas to activate the listener. – nukeforum Sep 02 '15 at 17:52

3 Answers3

1

Use onTouch event

a.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getX(0)>=20 && event.getY(0)>=60 && event.getX(0)<=160 && event.getY(0)<=150) {
            B b = new B(getApplicationContext());
            relativeLayout.addView(b);
        }
        return true;
    }
});
Wilik
  • 7,630
  • 3
  • 29
  • 35
1

You are defining the red square's parameters but not the parameters of the canvas in which you are drawing. You are creating the view (A) without defining the width and height of it, so it is set to match_parent by default, which means it will take the whole size of your RelativeLayout (the whole screen). So, when you click "outside" the red square you are actually clicking the view (A).

Try to define an specific height and width for the view in which you are drawing, like this.

    A a = new A(this);
    a.setLayoutParams(new RelativeLayout.LayoutParams(300,300));

Remember that LayoutParams takes pixels as parameters, so you should really convert the dps to px as specified here

Also, setting some background colors to your views (relativeLayout, A) will help you visualize what you are doing.

Community
  • 1
  • 1
Tico R.
  • 11
  • 1
0

@ nukeforum, your guess helped me very much. I thank all of you. My problem was exactly from the canvas and its size. I added the following operation in my code and solved my problem.

 relativeLayout.addView(a,70,70);

For A class, I changed as follows:

canvas.drawRect(10,20,30,40,paint);
and
  • 341
  • 1
  • 3
  • 11