Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="show_my_location">Show My Location</string>
<string name="latitude">Latitude</string>
<string name="longitude">Longitude</string>
</resources>
Manifest Permissions
Open AndroidManifest.xml file in manifest folder and add permissions as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.demo.learnandroidwithrealapps">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonShowLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_my_location" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/latitude"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/longitude"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java file in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button buttonLocation;
private TextView textViewLatitude, textViewLongitude;
/* GPS Constant Permission */
private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11;
private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 12;
/* Position */
private static final long MINIMUM_TIME = 2 * 10000; // 10s
private static final float MINIMUM_DISTANCE = 1; // 50m
/* GPS */
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
buttonLocation = findViewById(R.id.buttonShowLocation);
buttonLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonLocation_onClick(view);
}
});
textViewLatitude = findViewById(R.id.textViewLatitude);
textViewLongitude = findViewById(R.id.textViewLongitude);
}
private void buttonLocation_onClick(View view) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME, MINIMUM_DISTANCE, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_ACCESS_COARSE_LOCATION);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_ACCESS_COARSE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "ACCESS_COARSE_LOCATION permission was granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "ACCESS_COARSE_LOCATION denied", Toast.LENGTH_LONG).show();
}
break;
case MY_PERMISSION_ACCESS_FINE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "ACCESS_FINE_LOCATION permission was granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "ACCESS_FINE_LOCATION denied", Toast.LENGTH_LONG).show();
}
break;
}
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
if (location != null) {
textViewLatitude.setText(String.valueOf(location.getLatitude()));
textViewLongitude.setText(String.valueOf(location.getLongitude()));
} else {
Toast.makeText(getApplicationContext(), "location is null", Toast.LENGTH_LONG).show();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this, "Provider enabled: " + s, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this, "Provider disabled: " + s, Toast.LENGTH_SHORT).show();
}
};
}
Structure of Project
Run App
Load Main Activity
Display My Location