0% found this document useful (0 votes)
28 views

User Location

The document contains code for an Android application that gets the device's location. The AndroidManifest.xml file declares permissions for location access. The activity_main.xml layout contains buttons and text views to display latitude and longitude. The MainActivity class uses the FusedLocationProviderClient to get the last known location on button click, then displays the latitude and longitude in the text views.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

User Location

The document contains code for an Android application that gets the device's location. The AndroidManifest.xml file declares permissions for location access. The activity_main.xml layout contains buttons and text views to display latitude and longitude. The MainActivity class uses the FusedLocationProviderClient to get the last known location on button click, then displays the latitude and longitude in the text views.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

AndroidManifest.

xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Q1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>

activity_main.xml
<?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"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:id="@+id/get_loc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"/>
<TextView
android:id ="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude is " />
<TextView
android:id ="@+id/longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude is " />

</LinearLayout>
MainActivity.java
package com.example.q1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

public class MainActivity extends AppCompatActivity {


FusedLocationProviderClient mFusedLocationClient;
TextView latitude, longitude;
Button get_loc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get_loc = findViewById(R.id.get_loc);
latitude = findViewById(R.id.latitude);
longitude = findViewById(R.id.longitude);
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
get_loc.setOnClickListener(new View.OnClickListener() {
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(new
OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if(location != null)
{
latitude.setText("Latitude is
"+location.getLatitude());
longitude.setText("Longitude is
"+location.getLongitude());
}
}
});

}
});

}
}

You might also like