Java Question Finding the Views position on screen

Jim Brown

Active Member
Licensed User
Longtime User
How can I get the Left/Top/Right/Bottom positions of a view as set by the user?

For example, taking this basic snippet from Erels GameView source code:
B4X:
public class DummyWrapper extends ViewWrapper<DummyViewWrapper.MyPanel> {

.....<<< SNIP >>>.........

public static class MyPanel extends View {
  public List sprites = new List();
  public MyPanel(Context context) {
  super(context);
  sprites.Initialize();
}


Now, in the B4a code the user adds the View as such
B4X:
gv.Initialize("gv")
Activity.AddView(gv, 20%x, 20%y, 70%x, 70%y)

I need to find out the position of the View from within the libray but I don't know how?

Any pointers please?
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi Erel,
Thankyou. With that hint I finally found the answer
B4X:
=DummyWrapper.this.getLeft();
 

Jim Brown

Active Member
Licensed User
Longtime User
Weirdly, this seems to have stopped working now. I altered some code but I can't seem to use the above without the B4a saying Null reference.

Essentially, I want to load 2 variables (VirtualWidth and VirtualHeight) with the game views width and height.
The user can later alter this variables which will then scale the game view.

The main struggle is, at what point in the Java code has the display been created and .getWidth() / .getHeight() can be used?


B4X:
public class GameViewWrapper extends ViewWrapper<GameViewWrapper.MyPanel> {
    @Override
    @Hide
    public void innerInitialize(final BA ba, final String eventName, boolean keepOldObject) {
        if (!keepOldObject)    setObject(new MyPanel(ba.context));
        super.innerInitialize(ba, eventName, true);
        // The _Touch() sub
        if (ba.subExists(eventName + "_touch")) {
            getObject().setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                 ba.raiseEventFromUI(getObject(), eventName + "_touch",
                 event.getAction(), event.getX(), event.getY());
                 return true;
                }
            });
        }
    }
  
    /**
    * Sets or gets the Virtual Width.
     */
    public static int VirtualWidth;   **SEE CODE BELOW**
    /**
    * Sets or gets the Virtual Height.
     */
    public static int VirtualHeight;

  .... SNIP ....

I tried this but it gives an error: "Cannot use this in a static context"
B4X:
public static int VirtualWidth=GameViewWrapper.this.getWidth();

If I make the code like so:
B4X:
public int VirtualWidth=GameViewWrapper.this.getWidth();

Then, its broken further down the code in this section, with an error:
"Cannot make a static reference to a non-static field"

B4X:
@Hide
    public static class MyPanel extends View {
        public List Sprites = new List();
        // The panel context
        public MyPanel(Context context) {
            super(context);
            Sprites.Initialize();
        }
        @Override
        protected void onDraw(Canvas c) {
            Iterator<Object> it = Sprites.getObject().iterator();
            Sprite s;
            c.scale((float)MyPanel.this.getWidth()/(float)VirtualWidth, (float)MyPanel.this.getHeight()/(float)VirtualHeight);
            while (it.hasNext()) {
 

Jim Brown

Active Member
Licensed User
Longtime User
Well spotted Erel. I am happy to say that my library (loosely based on the GameView code) is now fully functional.
 
Top