title: Getting current location in Android Studio app
body: <p>I am developing my first Android app which should get the <strong>latitude</strong> and <strong>longitude</strong> of an android device and send it via a web service to a template document.</p>
  
  <p>I followed the guide of getting the location from <a href="http://developer.android.com/training/location/retrieve-current.html" rel="nofollow">http://developer.android.com/training/location/retrieve-current.html</a>. </p>
  
  <p>This is the code from my .java class:</p>
  
  <pre><code>import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.location.Location;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.EditText;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.location.LocationServices;
  
  public class GetLocation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
      private GoogleApiClient mGoogleApiClient;
      EditText textLat;
      EditText textLong;
      EditText lat;
      EditText lon;
  
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.get_location);
  
          textLat = (EditText) findViewById(R.id.latitude);
          textLong = (EditText) findViewById(R.id.longitude);
  
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
      }
  
      private boolean isGPSEnabled() {
          LocationManager cm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          return cm.isProviderEnabled(LocationManager.GPS_PROVIDER);
      }
  
      @Override
      public void onConnected(Bundle bundle) {
          Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
          if (mLastLocation != null) {
              lat.setText(String.valueOf(mLastLocation.getLatitude()));
              lon.setText(String.valueOf(mLastLocation.getLongitude()));
          }
      }
  
      @Override
      public void onConnectionSuspended(int i) {
  
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
  
      }
  
      public void onButtonClick(View v){
          if(v.getId() == R.id.getGpsLocation){
              if(!isGPSEnabled()){
                  new AlertDialog.Builder(this)
                          .setMessage("Please activate your GPS Location!")
                          .setCancelable(false)
                          .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                  startActivity(i);
                              }
                          })
                          .setNegativeButton("Cancel", null)
                          .show();
              } else {
                  textLat.setText(String.valueOf(lat));
                  textLong.setText(String.valueOf(lon));
              }
  
          }
      }
  
  
  }
  </code></pre>
  
  <p>I don't get any errors but when I am tapping the button which should get the coordinates, <strong>I get 'null' text in both views</strong>.</p>
  
  <p>I also have included permissions for internet, access fine and coarse location.</p>
  
  <p>Thanks in advance!</p>