-2

How can i have bot the lat and log in one textview separated with a comma? I tried as below and i am getting the "do not concatenate text displayed with settext" warning!

 gpsTracker = new GpsTracker( MainActivity.this);
               if(gpsTracker.canGetLocation()){
                   double latitude = gpsTracker.getLatitude();
                   double longitude = gpsTracker.getLongitude();

                   textviewGPSLocation.setText(String.valueOf(latitude) + "/," + String.valueOf ( longitude ));
                 
shinsky Paul
  • 97
  • 10
  • usually the IDE will recommend an alternative to these warnings – a_local_nobody Aug 03 '20 at 10:10
  • Does this answer your question? [Do not concatenate text displayed with setText. Use resource string with placeholders](https://stackoverflow.com/questions/35113625/do-not-concatenate-text-displayed-with-settext-use-resource-string-with-placeho) – a_local_nobody Aug 03 '20 at 10:15
  • @a_local_nobody Not really but thanks for your contribution. – shinsky Paul Aug 03 '20 at 18:25

1 Answers1

3

You can format your string like this

double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
String text = String.format("%1$2f / %2$2f", latitude, longitude);
textviewGPSLocation.setText(text);
Bruno
  • 3,872
  • 4
  • 20
  • 37
  • Thanks @Bruno. This worked as follows String text = `String.format(latitude+" , " longitude); textviewGPSLocation.setText(text);` – shinsky Paul Aug 03 '20 at 18:22
  • 1
    If you're going to do this, you may as well just suppress the warning. This just confuses the linter enough to not realize you've effectively written the same code a different way. The idea is that some locales might express a latitude, longitude pair differently, and so you could use a string resource and translate it. But...honestly, I don't think this is the sort of case where there's any real likelihood of confusion, so either this or suppressing the warning are fine. It's effectively a false positive on the lint warning. – Ryan M Aug 04 '20 at 05:17