501 02 CH 5
501 02 CH 5
The code sets up an Android app that uses GPS to get the user's current location
and display it in a TextView. It requests the necessary permissions dynamically and
handles location updates via the LocationManager. The app ensures that location
permissions are properly checked and requested from the user before accessing
location services.
Variable Declarations
displayed.
onCreate Method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tvGpsLocation = findViewById(R.id.textView)
button.setOnClickListener {
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION),
locationPermissionCode)
} else {
if (requestCode == locationPermissionCode) {
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
5f, this)
else
MainActivity.java
package com.example.gmapcurrentlock
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
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">
<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GmapCurrentLocK"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="@string/hello_world"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/get_gps_location"
android:textColor="@android:color/holo_red_dark"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/getLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerInParent="true"
android:layout_marginTop="40dp"
android:text="@string/get_location" />
</RelativeLayout>
OUTPUT:
Prof. Bhumika Patel Page 8
An android application has been developed Capturing image using device camera. The
opening of the Camera from inside our app is achieved with the help of the
ACTION_IMAGE_CAPTURE Intent of MediaStore class.
This image shows the Image clicked by the camera and set in Imageview. When the app is
opened, it displays the “Camera” Button to open the camera. When pressed,
ACTION_IMAGE_CAPTURE Intent gets started by the MediaStore class. When the image is
captured, it is displayed in the image view.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the
code for the activity_main.xml file. Comments are added inside the code to understand the
code in more detail.
∙ A Button to open the Camera
∙ An ImageView to display the captured image
Also, Assign the ID to each component along with other attributes as shown in the image
and the code below.
Also, Assign the ID to each component along with other attributes as shown in the image
and the code below.
Syntax:
android:id="@+id/id_name"
Add the listener to the Camera button. This will be used to open the camera when the user
clicks on the button.This is done as follows:
camera_open_id.setOnClickListener {
// Your code here
}
Now create the ACTION_IMAGE_CAPTURE Intent provided by MediaStore. This Intent will
help to open the camera for capturing the image. Start the intent with the requested pic_id.
This is done as follows:
Now use the onActivityResult() method to get the result, here is the captured image.
This is done as follows:
Then set the image received as a result of Camera intent in the ImageView for display.
val photo: Bitmap = data.extras["data"] as Bitmap
clickedimageid.setImageBitmap(photo)
Example:
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
cameraOpenId = findViewById(R.id.camera_button)
clickImageId = findViewById(R.id.click_image)
if (requestCode == pic_id) {
val photo = data!!.extras!!["data"] as Bitmap?
clickImageId.setImageBitmap(photo)
}
}
companion object {
private const val pic_id = 123
}
}
OUTPUT:
Source:
⮚ https://techpassmaster.com/get-current-location-in-android-studio-using-kotlin/ ⮚
https://www.geeksforgeeks.org/how-to-open-camera-through-intent-and-display-captured-image
in-android/
⮚ https://www.geeksforgeeks.org/how-to-get-current-location-in-android/
⮚ https://www.geeksforgeeks.org/using-fused-location-api-to-fetch-current-location-in
android/?ref=asr1