title: How to call a Location method from a button click in Java Android?
body: <p>pretty new to java but after tutorials and such, I understand the basics. I'm trying to call a method I created to find the location of my device and then append the strings to a textview. How do I call this method from my button click? I tried just putting all the code inside my button so it would just run when clicked, but it doesn't like that. Does a button click have to contain a <code>View</code> parameter or because when I use <code>public onLocationChanged(Location location)</code> I get an error saying that it can't find the <code>public onLocationChanged(View)</code>. I tried doing <code>public onLocationChanged(View view, Location location)</code>but I get the same error in logcat. Thanks in advance for any help or feedback! </p>
  
  <p>Edit: I saw on the android dev site "Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter." when referring to the onClick parameter in the xml file. If I don't use this parameter and instead use the onClickListener method in the java file, can I use Location?</p>
  
  <hr>
  
  <p>XML FILE</p>
  
  <pre><code>&lt;Button 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/GPS"
      android:id="@+id/vid_message_button"
      android:onCLick="onLocationChanged" /&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          Criteria criteria = new Criteria();
          provider = locationManager.getBestProvider(criteria, false);
          Location location = locationManager.getLastKnownLocation(provider);
  
  }
  
  
  
  public void onLocationChanged(Location location) {
  
  
      if (view.getId() == R.id.send_GPS_button) {
  
      double lat = (double) (location.getLatitude());
      double lng = (double) (location.getLongitude());
      float accuracy = (float) (location.getAccuracy());
      double alt = (double) (location.getAltitude());
      double speed = (double) (location.getSpeed());
      double heading = (double) (location.getBearing());
  
      messageLogTextView.append("Latitude: " + String.valueOf(lat) + "\n" +
                      "Longitude: " + String.valueOf(longi) + "\n" +
                      "Accuracy: " + String.valueOf(accuracy) + "\n" +
                      "Altitude: " + String.valueOf(alt) + "\n" +
                      "Speed: " + String.valueOf(speed) + "\n" +
                      "Heading: " + String.valueOf(heading) + "\n");
  
      }    
  
  }
  
  public void onSendGPSButtonClick(View view) {
  
  }
  </code></pre>