Get the center coordinates of phone screen

13,133

Solution 1

You can try with DisplayMetrics to achieve your solution

int mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;

Solution 2

Here is how I resolved the issue. As a background, I have a button that once the user moves the map to the desired location (cross hairs showing the centre of map) they can tap the button and a marker appears in the centre of the map.

findViewById(R.id.addMarker).setOnClickListener(
            new View.OnClickListener(){
                @Override
                public  void onClick(View view) {
                    if(myMap != null){
                        double myLat = myMap.getCameraPosition().target.latitude;
                        double myLng = myMap.getCameraPosition().target.longitude;
                        LatLng markerPoint = new LatLng(myLat, myLng);
                        myMap.addMarker(new MarkerOptions().position(markerPoint)).setTitle("new marker");
                    }
                }
            }
    );

Solution 3

int cx =screenHeight = displaymetrics.heightPixels; int cy = screenWidth = displaymetrics.widthPixels; float finalRadius = (float) Math.sqrt(cx * cx + cy * cy);

float is the center if the object

Share:
13,133

Related videos on Youtube

1QuickQuestion
Author by

1QuickQuestion

Updated on October 30, 2022

Comments

  • 1QuickQuestion
    1QuickQuestion over 1 year

    Can someone give me an example of how to get the center coordinates of the current displayed screen? I would like my objects to always be relative to the center coordinates of the active view.

  • PK Gupta
    PK Gupta almost 5 years
    Probably this doesn't give you full height if keyboard is open.
  • Nasib
    Nasib over 4 years
    @Gupta , one simple solution for that would setting the activity to never resize with keyboard (in the manifest)