Current Location Latitude Longitude Android Studio Tutorial

Current location android is the today’s example.

It is necessary to find current location in android studio programmatically when you are developing location-based applications.

Globally, the location of any object is found in terms of the latitude and longitude.

For example, if you want to find restaurants near by you, you need your current gps location latitude and longitude.

With the help of latitude and longitude, you will be able to find location regardless of how far the object is from you.

In current GPS location android example tutorial, you will learn to get current latitude and longitude of Android device.

We will use Google’s FusedLocationAPI for getting current location with latitude and longitude.

Download Android App

Get the app version of DemoNuts from here : DemoNuts Official App.

Following is the output of current GPS location android example tutorial programmatically:


How to get maximum benefit from Current Location Android tutorial

To get most of this current GPS location android studio example, follow  these steps:
  • We strongly recommend you to download the source code and then run in your android studio.
  • If you have a working code already in your hand, then you will quickly grasp all the words in the tutorials.
  • Working code in the android studio will allow you to easily understand the project structure and where to put required images, files, resources and where to create new files.
  • When you are going through the whole example, implement it in your demo and then also check in working source code how it is implemented.
  • The tutorial is theory and source code is practical, and in the end, you need to have right practical so learn it from theory as well as practical.

Follow all the below steps carefully to get the current location.

Step 1.  Create a new project in Android Studio.

Make a fresh new project in Android Studio. Make sure you have select Empty activity as a Main Activity while creating a new project.

Step 2. Update build.gradle(Module:app) file

Add below source code in build.gradle(Module:app)

  • We need to use google’s play services to accomplish our task of getting current latitude and longitude.
  • Above line will import all the necessary classes from google api.

Step 3. Implement necessary interfaces

These interfaces are required.

  • All the above interfaces are required for using google’s fusedlocation API.
  • We also need to override necessary methods associated with these interfaces.

Step 4. Declare instances:

Declare Necessary statements as per below source code.

All these instances will help to use location services of the android architecture.

Step 5.  Put following in onCreate() method

Update your onCreate() method as per following:

Step 6.  Last but not least, put required permissions in AndroidManifest.xml

Below two permissions are must to get desired results.

If you are targeting marshmallow and after versions (targetSdkVersion >=23) then you have to check for run-time permissions.

You can learn about this topic here at our separate example.

Now full source code:




  • In the above layout file, I have used four TextViews. Two are static which will give you reference about which digit is latitude and which is other.
  • Other two textview represents the location factors and will be changed frequently.
  • After implementing interfaces, you need to override all of it’s required methods. Here is the full source code with those methods in class.

Descriptions of the various methods

In above class, there is a method

This method is used to check whether location service is on or off in your phone. If off, then it will pop up one dialog, which will take the user to settings to enable location service.


This method will prompt a dialog if the location settings of the device is off. Title of the dialog will tell user about this. From here, user can turn on the location or can simply ignore it.


In this method, android will finally use google’s FusedLocation API to fetch the current latitude and longitude of the android device.


Latitude and Longitude are very sensitive factors. It changes constantly every seconds to fetch the exact gps location of the device.

When these two factors have changed their values, this method onLocationChanged() will be called every second.

  • I have updated the textview values which represents the latitude and longitude in this method.
  • We will get the values of latitude and longitude in the long variable format because we must consider 7 to 8 decimal digits for the accuracy of the gps location.

If you have any query or questions then use comment section to ask them.

I will be happy to solve your questions as quickly as possible.

Kotlin Version

Java Version of this tutorial is present at : Android Current Location Kotlin

How to get exact Android example while searching in Google

When you search for any Android example in Google, always add “Android Studio programmatically” at the end of your query.

Why add studio?

If you search with the query “Facebook login in Android” then Google will also show you examples which are developed with Eclipse. Examples made with Eclipse are useless because it is officially deprecated.

Put studio in your query, and it becomes “Facebook login in Android Studio.

Why add programmatically

If your query is “Scan barcode in Android” then Google will show you some app URLs to play store which helps the user to scan the barcode.

Google doesn’t know that you are looking for source code to implement scan in Android.

Now if your query is “Scan barcode in Android Studio programmatically” then google will show you examples which are developed with android studio and will also remove app URLs of play store.

Thus adding Android Studio programmatically will save your lot of time in searching right tutorial.
That’s all for getting current GPS location android tutorial. Now if you run this demo, you will get output latitude and longitude as shown in the video.

Other charming tutorials you will definitely like

Cheers and happy coding!

Download Source Code For Current Location Android

[sociallocker] Download GPSDemocode [/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

92 Responses

  1. Android learner says:

    Latest version for google play service is “9.4.0”, can I use this version instead of “9.0.2”, which is mentioned on the above blog post?

  2. KRH says:

    First off, great tutorial!
    Secondly, my questions:
    I am new to android, but I am quite good in java. I work in android studio 1.5(do I need to update it?). I wonder if this code can be used as is for broadcasting the location to other devices with the same app, and then somehow plot the coordinates on google maps, such that a nearby person can be notified of the position of another person?

    Or do I have to do a lot of tweaks in this code?


    • Hardik Parsania says:

      You can implement this code on Android Studio 1.5 (No need to update). This code will give you latitude and longitude of your device, for broadcasting these latlong you can send them to server and from server all other devices can get them and then you can show it on google map. So this tutorial will give you only coordinates of your device, for your whole purpose, you have to do lot of tweaks as you guessed.
      Thanks 🙂

  3. Android learner says:

    Thank you very much for amazing tutorial, it’s nice and easy to understand.

  4. John Colley says:

    Beautiful code my friend, it helped me a lot.

  5. saiteja says:

    Hello Hardik Parsania,

    I’m writing a simple app that needs to set the current GPS coordinates from let’s say point A, then travel to another point B and have it detect the coordinates at point B. I am able to get point A coordinates no problem. I’m just not getting it to detect the coordinates at point B, it reads the same coordinates as the ones at point A. I’ve got the app installed on my phone, and I am driving a mile between points. My manifest file includes every possible permission for location.

    • Hardik Parsania says:

      It may happen that when you are at point A, app is giving you coordinates from wifi and at point no wifi is there. You can try by switching off your wifi and use only mobile data for testing.
      Also try by opening google map at both points and see if you are getting correct coordinates or not.

  6. saiteja says:

    Hello Hardik Parsania,

    I am writing a simple GPS location application in android studio. The current longitude latitude values are stored in SQL server database through web service.GPS Application is Refreshed every 5 secs and SQL server database is ping every 5 sec correctly. but SQL server is ping every 5 secs up to 40 mins only.After the values are not stored in database.Again App is opened and minimized the values are stored in SQL server database 40 mins or 1 net speed is also to solve this problem.My Agenda is daily once app is opened and minimized the values are stored up to next 24 hours in every 5 secs in SQL server Database

  7. Pulkit Shah says:

    I am trying to update GPS coordinates even when the screen is off? Will the above solution mentioned by you help me achieve so ?

    • Hardik Parsania says:

      If you want to achieve coordinates when screen is off, then you should fetch coordinates in to service class(Background service) instead of activity.

  8. Keeley Fenton says:

    Very informative articles and good description overall. Nice work !

  9. August says:

    Whats up are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and set up my own. Do you
    require any coding expertise to make your own blog? Any help would be greatly appreciated!

  10. Pokhraj Chaudhuri says:

    I ve used your code, but following line is returning null only.

    mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    • Hardik Parsania says:

      What is the brand name and model of your android device?

      • Pokhraj Chaudhuri says:

        Trying with Nexus 5X_API25 Emulator.

        Please find the build.gradle also for your reference.

        apply plugin: ‘’

        android {
        compileSdkVersion 25
        buildToolsVersion ‘25.0.0’

        defaultConfig {
        applicationId “com.exampledemo.parsaniahardik.gpsdemocode”
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName “1.0”
        buildTypes {
        release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘’

        dependencies {
        compile fileTree(dir: ‘libs’, include: [‘*.jar’])
        testCompile ‘junit:junit:4.12’
        compile ‘’
        compile ‘’
        compile ‘’
        compile ‘’

  11. Blake says:

    Hello. Good writing technique and well experience. Can you publish tutotials about SQlite for Android?

  12. Jin Ming says:

    Thanks for your great tutorial!!!
    it helped me a lot.

    I have one question.
    I am developing app now, I have to send location info to server in every 1 min on background.

    can you send me sample about this?

    • Hardik Parsania says:

      Thanks for your review. You can call web service every 1 min to send location info to server. You can use handler or thread for calling particular code snippet every 1 minute.

      You can use below code to send info every 1 second

      new Handler().postDelayed(new Runnable() {
      public void run() {
      }, 1000);

      Visit link: Send info every 1 second

      • Jin Ming says:

        Thank you very much.
        But I didn’t understand fully.
        Because I am beginner on app development.
        so can you post full code step by step, please?

  13. Bob Marsh says:

    Thank you very much! Nice and easy…Keep posting this kinds of tutorials, it really helps.

  14. Rachana says:

    When I tried the code I’m not able to get the latitude and longitude. It is just showing blank like this

  15. Andreas says:

    I was studying some off youhr articles on this site and I conceive this web site is very informative!
    Retain posting.

  16. manicure says:

    Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just
    posting videos to your weblog when you could be giving
    us something enlightening to read?

  17. priyanka says:

    ihave some trouble, when i run this project i can see my lat and long.
    and there is no error message.
    please someone help me

    • priyanka says:

      ihave some trouble, when i run this project i cant
      see my lat and long.
      and there is no error message.
      please someone help me

  18. Daniel says:

    Thank you very much!
    You helped me alot!
    I have one question, how do I get the current location just by clicked a button? I mean I don’t want to put the interval.
    So, which part do I need to change?

    Thank you for your attention!
    Appreciate it!

  19. Riho says:

    Hi. Code is working perfectly and send information to my server but if i copy that .java and .xml to the other project then its not starting. It showing just plain latitude and longitude labels but not values. Is there anything i can do to forse it?

  20. Priya says:

    Very nice blog! But I have a question.
    How to ask for runtime permission in this example?

  21. Kunal says:

    hey buddy..
    i tried your code but after successful installation of apk on my phone ..
    its showing a toast “Location not detected”
    please help
    also i turned my gps on.

  22. Kunal says:

    Hey buddy..
    your code was successfully installed but its showing only a toast that “location not detected”
    please help.
    i am using
    minSdkVersion 21
    targetSdkVersion 25
    also in Eveent log its showing :
    RuntimeException: incorrect header check

    • Hardik Parsania says:

      your target version is upper than marshmallow so have you check for runtime permissions?

      • Kunal says:

        i changed targetSdkVersion but its not showing any output.
        Nothing is happening.
        Please Help

        • Hardik Parsania says:

          Try by opening Google map first. If google map is showing your current location then your app should also show current location. I had this problem in lenovo A600 model.

  23. priya says:

    Use Shared Preferences to read and store values (1,2,3,4,5,6,7,8) 1+2 =3 every 5 sec.How to solve this example

    • Krishnan says:

      Try this..will help you

      private int counter = 0;
      private int total = 30; // the total number
      //when you want to start the counting start the thread bellow.
      new Thread(new Runnable() {

      public void run() {
      while (counter < total) {
      try {
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      } Runnable() {

      public void run() {
      t.setText("" + counter);





  24. Krishnan says:

    how can we get current location by signal provider(not using gps)

    • Krishnan says:

      Try this..will help you

      private int counter = 0;
      private int total = 30; // the total number
      //when you want to start the counting start the thread bellow.
      new Thread(new Runnable() {

      public void run() {
      while (counter < total) {
      try {
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      } Runnable() {

      public void run() {
      t.setText("" + counter);





  25. Bert says:

    How can i put in on gmaps sir ? please help me

  26. shubha says:

    The function onLocationChanged() is not getting called. I amrunning it on Honor6x. Plese help.
    this is gradle file-
    android {
    compileSdkVersion 26
    defaultConfig {
    applicationId “”
    minSdkVersion 18
    targetSdkVersion 26
    versionCode 1
    versionName “1.0”
    testInstrumentationRunner “”
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘’

    dependencies {
    implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
    implementation ‘’
    implementation ‘’
    testImplementation ‘junit:junit:4.12’
    androidTestImplementation ‘’
    androidTestImplementation ‘’
    compile ‘’

    • Hardik Parsania says:

      Download working source code of tutorial and check if it works or not.

      • Shubha says:

        Yeah it’s working on the source code. I actually have want to combine this code with another code. That time when I copy the above code into a new project the function is not getting called. Please help. Thanks in advance

        • Hardik Parsania says:

          Then it must be error when you combine both codes. Check closely everything again. If possible again do it from scratch.

  27. Indranil says:

    Thank you sir!
    Best tutorial!

  28. Mahnoor says:

    My app is crashing. An i am running it on my real device. and my android version is 2.3.3 Please help me to find location and send in a text.

  29. Sanjay says:

    Hello ,
    I check this document and implement my android project.
    but i have run my apk but latitude longitude is display has null. (0).
    and my application is crashes.

  30. Sumit Chauhan says:

    Can this app run without internet??I want to fetch coordinates using GPS only

  31. dalila yahyaoui says:

    full code please

  32. Nelchael says:

    Hai Sir,
    how to get the RAM, ROM, processor android information programmatically,

  33. Isabella says:

    thankyou for your great tutorial,
    i need some help, i have a task to get information internal memory and external memory (ROM, RAM, Processor, CPU) android programmatically. So, Please help me.


  34. Maria Jesus Abarca says:


  35. Bibin Johny says:

    Which is the best way to get the most accurate location programmatically in android? I have used FusedLocation, but it is not that much accurate. I am developing an app which requires exact location even when the user is inside the home. How can I do that?

    • Hardik Parsania says:

      Latest version and technique for location is FusedAPI only as per my knowledge. Location service has it’s limit also.
      But still do some R&D on this and if you find more accurate method than FUsedAPI, then please do share it with me. I will make separate example on that accurate method to help others in future.

  36. Atul Kansal says:

    Dear Sir
    This code is keep on running. The source code is keep on calling onLocationChanged() function again & again itself. I want to terminate the execution once the latitude and longitude are captured. Please help.

    • Hardik Parsania says:

      Increase the value of below lines

      private long UPDATE_INTERVAL = 2 * 1000; /* 10 secs */
      private long FASTEST_INTERVAL = 2000; /* 2 sec */

  37. Luisa Cecilia Patino Alvarado says:

    Hello Hardik Parsania! I am trying to download the source code but you page has reached a limit amount of likes.

    Could you please share it?

    • Hardik Parsania says:

      You can try again now. If it still do not work then remove your browser history and refresh again, it will surely work.

  38. Yumna Arif says:

    hello ! i’m working on an app but i have some troubles relating the location as app involved google map apis or other rest apis for the loaction etc
    can u help me out with that ?

  39. mukul says:

    Hello,I am trying to get the current location and used above code but somehow it doesn’t show me any data just blank text latitudes and longitudes.
    i am using api level 26 .can you help me as what could be possible issue .
    i have copied the above code exactly.

  40. Vishal says:

    Hello Hardik Sir,
    Sir I m facing problem in UPDATE_INTERVAL and FASTEST_INTERVAL. It shows red line error .Can you give me little bit explanation on it.
    protected void startLocationUpdates() {
    // Create the location request
    mLocationRequest = LocationRequest.create()

  41. Vishal says:

    “class file for not found”
    error occur sir

  42. Slobodan says:

    First: compile is deprecated long long time ago in gradle.
    As per Googles explicit advice: You should never just simply include entire package in dependancy like this “compile ‘′”. You should always specify the exact library that you need eg. “implementation ‘”

  43. mina says:

    thanks for your help , very professional

Leave a Reply

Your email address will not be published. Required fields are marked *