Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, the problem is during the creation of <code>BallView</code> and <code>SmalBall</code> as <code>View</code> objects. I think I can fully explain this well enough, so bear with me:</p> <p>A good place to start is with a layered cake. Think about standing in the kitchen and making a cake like this: adding one layer at a time. First the chocolate layer, and then the vanilla layer and then another chocolate layer. Sound good? Alright.</p> <p>So, there are a lot of circles being drawn on a <code>Canvas</code> and seperately, <code>Ball</code> objects are added as <code>View</code>s. Note a Canvas simply represents a rectangle the user can interact with, and is associated with a <code>View</code>-- the <code>FrameLayout</code> in your code. For all intents and purposes, the <code>Canvas</code> is completely filling the <code>FrameLayout</code>. In your app, the <code>Canvas</code> is the bottom layer of the cake-- filling both the width and height of the <code>ContentView</code> (or pan). </p> <p>Every call to <code>m.addView(View v)</code> is essentially placing a new <code>View v</code> on top of however many <code>View</code> objects are already contained in <code>m</code>. In terms of cake, every call to <code>addView</code> is adding another layer to our layer cake.</p> <p>In your project, you want to add a layer to the cake but not have it fill up the entire pan. You want to put a couple strawberries on top, you don't want a full layer of strawberry.</p> <p>So, by default, your <code>BallView</code> objects are taking up the entire screen. Each on is piling on top of the next, filling up the entire canvas. When you click, it deletes the last one-- because the last one is on top, and is taking up the width and height of the entire canvas-- and is actually the <em>only</em> ball that the user can possibly click on.</p> <p>So you have to tell your <code>BallView</code> <code>View</code> objects to be the same size as the circle you are drawing on the <code>Canvas</code> object.</p> <p>So, there are a couple issues here:</p> <p>You can only do absolute positioning with a <code>RelativeLayout</code>. If you want the <code>BallView</code> the user can click on to be on top of the circle being drawn on the canvas, you need a way to tell the <code>BallView</code> where it is located, and change that position dynamically. Currently you are using a <code>FrameLayout</code>, which is necessary for the <code>Canvas</code>-- so in your XML, add a full screen <code>&lt;RelativeLayout&gt;</code>. And then, in your code, add the <code>BallView</code> objects to this <code>RelativeLayout</code> instead. </p> <p>and then, FINALLY:</p> <p>Go here:</p> <p><a href="https://stackoverflow.com/questions/3294590/set-the-absolute-position-of-a-view-in-android">Set the absolute position of a view</a></p> <p>Good luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload