Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:21:52+00:00 2026-06-17T04:21:52+00:00

I’m writing an application where a user can fill out a form on an

  • 0

I’m writing an application where a user can fill out a form on an activity and sync it to a web service. Part of the application involves geolocation, so, lat and long value. I want my application to automatically the geolocation of a person’s smartphone (it does this successfully) and then automatically fill in a TextView on the activity’s form. I’ve tried some methods out but to no avail. Could anyone advise me on where I’m going wrong?

Here is the DataEntry class:-

package application.prototype.mfb;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import application.prototype.mfb.MyLocation.LocationResult;

/**
 * Records a new data form entry for a favourite building
 * @author DanielD
 *
 */
public class DataEntry extends Activity implements OnClickListener {

Button btnClear;  
Button btnSync;
EditText txtBuildingName;
EditText txtDescription;
TextView txtLongitude;
TextView txtLatitude;
Spinner project;

public static Location loc;
private static double longitude;
private static double latitude;
private static String name;
private static String description; 
private static String projectString;
private static String lngString = String.valueOf(longitude);
private static String latString = String.valueOf(latitude);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_entry);

    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            loc = location;
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            System.out.println("Latitude: " + latitude);
            System.out.println("Longitude: " + longitude);
        }
    };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(DataEntry.this, locationResult);

    txtBuildingName=(EditText)this.findViewById(R.id.nameContent);
    txtDescription=(EditText)this.findViewById(R.id.descriptionContent);

    project=(Spinner)findViewById(R.id.type_project);
    ArrayAdapter<CharSequence> adapterTwo = ArrayAdapter.createFromResource(this, R.array.type_project_array, android.R.layout.simple_spinner_item);
    project.setAdapter(adapterTwo);

    txtLatitude=(TextView)this.findViewById(R.id.TextViewLat);
    txtLongitude=(TextView)this.findViewById(R.id.TextViewLong);

    /*These checks will ensure that if a variable still has user data in it, it will refill the fields with that data preventing users entering
     * the same information multiple times
     */
    if(name != null){
        txtBuildingName.setText(name);
    }

    if(description != null){
        txtDescription.setText(description);
    }

    if(projectString != null){
        project.getItemAtPosition(project.getSelectedItemPosition()).toString();
    }

    if(latString != null){
        txtLatitude.setText(latString);
    }

    if(lngString != null){
        txtLongitude.setText(lngString);
    }

    btnClear=(Button)findViewById(R.id.btnClear);
    btnClear.setOnClickListener(this);

    btnSync=(Button)findViewById(R.id.btnSync);
    btnSync.setOnClickListener(new OnClickListener(){
        public void onClick(View v){                                        //NEW VERSION OF COMMIT DATA - WILL PRESERVE DATA OVER ACTIVITY
            if(txtBuildingName.getText().toString().length() > 0)
                name = txtBuildingName.getText().toString();
            if(txtDescription.getText().toString().length() > 0)
                description = txtDescription.getText().toString();
            if(project.getItemAtPosition(project.getSelectedItemPosition()).toString().length() > 0)
                projectString = project.getItemAtPosition(project.getSelectedItemPosition()).toString();
            Intent intent = new Intent(DataEntry.this, DataSummary.class);
            startActivity(intent);
        }
    });    
}

public static String getName(){
    return name;
}

public static String getDescription(){
    return description;
}

public static String getLongitude(){
    return lngString;
}

public static String getLatitude(){
    return latString;
}

/*public Location getLongitude(){
     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     longitude = location.getLongitude();
     return longitude;
 }

public Location getLatitude(){
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    latitude = location.getLatitude();
    latitudeString = String.valueOf(latitude);
    return latitudeString;

}*/

public static String getProject(){
    return projectString;
}

public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
}

public void onRestoredInstanceState(Bundle returnState)
{
    super.onRestoreInstanceState(returnState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_favourite_building, menu);
    return true;
}

public void onClick(View v){
    switch(v.getId())
    {
    case R.id.btnClear : clearFields();
    break;  
    }
}

/**
 * Switch statement governing the different options available on the Action Bar
 */
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.app_camera:
        Intent intentZero = new Intent(this, CameraDemo.class);
        intentZero.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentZero);
        return true;

    case R.id.app_dataentry:  
        Intent intent = new Intent(this, DataEntry.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    case R.id.app_syncorsave:
        Intent intentOne = new Intent(this, DataSummary.class);
        intentOne.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentOne);
        return true;

    case R.id.app_discard:  
        Intent intentTwo = new Intent(this, DeleteProjects.class);
        intentTwo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentTwo);
        return true;

    case R.id.app_homescreen:
        Intent intentThree = new Intent(this, ProjectGrid.class);
        intentThree.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentThree);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

 public void clearFields(){
        Toast.makeText(DataEntry.this, "All fields Reset", Toast.LENGTH_LONG).show();
        txtBuildingName.getEditableText().clear();
        txtDescription.getEditableText().clear();
        name = "";              //must also set the static variables to blank otherwise previous data still assigned in variable.
        description = "";
        projectString = ""; 
    }
}

And here is the MyLocation.java class:-

package application.prototype.mfb;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;

public boolean getLocation(Context context, LocationResult result)
{
    //I use LocationResult callback class to pass location value from MyLocation to user code.
    locationResult=result;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //exceptions will be thrown if provider is not permitted.
    try
    {
        gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    try
    {
        network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

    //don't start listeners if no provider is enabled
    if(!gps_enabled && !network_enabled)
        return false;

    if(gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    if(network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
    timer1=new Timer();
    timer1.schedule(new GetLastLocation(), 20000);
    return true;
}

LocationListener locationListenerGps = new LocationListener() 
{
    public void onLocationChanged(Location location) 
    {
        timer1.cancel();
        locationResult.gotLocation(location);



      //  lm.removeUpdates(this);
        //lm.removeUpdates(locationListenerNetwork);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener()
{
    public void onLocationChanged(Location location) 
    {
        timer1.cancel();
        locationResult.gotLocation(location);
       // lm.removeUpdates(this);
     //   lm.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class GetLastLocation extends TimerTask 
{
    @Override
    public void run() {
         //lm.removeUpdates(locationListenerGps);
         //lm.removeUpdates(locationListenerNetwork);

         Location net_loc=null, gps_loc=null;
         if(gps_enabled)
             gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if(network_enabled)
             net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

         //if there are both values use the latest one
         if(gps_loc!=null && net_loc!=null){
             if(gps_loc.getTime()>net_loc.getTime())
                 locationResult.gotLocation(gps_loc);
             else
                 locationResult.gotLocation(net_loc);
             return;
         }

         if(gps_loc!=null){
             locationResult.gotLocation(gps_loc);
             return;
         }
         if(net_loc!=null){
             locationResult.gotLocation(net_loc);
             return;
         }
         locationResult.gotLocation(null);
    }
}

public static abstract class LocationResult
{
    public abstract void gotLocation(Location location);
}
}

Now I’ve added the System.out.println methods in to identify in the logcat log whether the location values have been found and they have (see below). So how do I take those values and convert them into a suitable format to display in the TextView?

01-11 18:04:01.885: I/System.out(541): Latitude: -4.066498333333333

01-11 18:04:01.885: I/System.out(541): Longitude: 52.410999999999994

*taken as an exert from the resulting logcat file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:21:53+00:00Added an answer on June 17, 2026 at 4:21 am

    So how do I take those values and convert them into a suitable format to display in the TextView?

    Displaying them is easy, you’ve done 98% of the work, just add:

    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            loc = location;
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
    
            txtLatitude.setText(latitude + "");
            txtLongitude.setText(longitude + "");
    
            System.out.println("Latitude: " + latitude);
            System.out.println("Longitude: " + longitude);
        }
    };
    

    But “suitable format” is a little vague… What format do you want? You can use Location.convert() to use a more traditional hour:minute:second.fraction format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I'm interested in microtypography issues on the web. I want a tool to fix:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.