title: How to get current geolocation when clicking a button?
body: <p>I would like to be able to retreive the user's current geolocation (latitude, longitude) and show it into a TextView when he presses a button in Android. I've tried some different strategies (<a href="https://developer.android.com/guide/topics/location/strategies.html" rel="nofollow noreferrer">Android developer location strategies</a>) but it doesn't show any location.</p>
  
  <p>I believe it has something to do with the fact that the "onLocationChanged()" method is called when the user is moving but i couldn't find any documentation on that.</p>
  
  <p>Here's my code (minSdkVersion is 24, which is why i've set up <a href="https://developer.android.com/training/permissions/requesting.html" rel="nofollow noreferrer">the requesting permissions strategy</a>)</p>
  
  <hr>
  
  <h2>CreateCustomStepActivity</h2>
  
  <hr>
  
  <pre><code>public class CreateCustomStepActivity extends AppCompatActivity {
  private TextView showLocation;
  private Button scanGeolocationButton;
  private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1400;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_create_custom_step);
      showLocation = (TextView) findViewById(R.id.showLocationTextId);
      scanGeolocationButton = (Button) findViewById(R.id.scanPositionButtonId);
      scanGeolocationButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (ContextCompat.checkSelfPermission(CreateCustomStepActivity.this,
                      Manifest.permission.ACCESS_FINE_LOCATION)
                      != PackageManager.PERMISSION_GRANTED) {
                  ActivityCompat.requestPermissions(CreateCustomStepActivity.this,
                          new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                          PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
              }
          }
      });
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      switch (requestCode) {
          case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
              if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED)
                  getCurrentGeolocation();
          }
      }
  }
  
  private void getCurrentGeolocation() {
      // Acquire a reference to the system Location Manager
      LocationManager locationManager = (LocationManager) CreateCustomStepActivity.this.getSystemService(Context.LOCATION_SERVICE);
      LocationListener locationListener = new UserLocationListener();
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  }
  // Define a listener that responds to location updates
  private class UserLocationListener implements LocationListener {
  
      @Override
      public void onLocationChanged(Location loc) {
          String longitude = "Longitude: " + loc.getLongitude();
          String latitude = "Latitude: " + loc.getLatitude();
          String s = longitude + "\n" + latitude;
          showLocation.setText(s);
      }
  
      @Override
      public void onProviderDisabled(String provider) {}
  
      @Override
      public void onProviderEnabled(String provider) {}
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {}
  }}
  </code></pre>