0% found this document useful (0 votes)
229 views2 pages

CurrentLocationDemo PrNo31

The document contains code for an Android application that displays the user's current location. The activity_main.xml layout file contains two text views, one displaying static text and the other to display the location. The MainActivity class uses the LocationManager to request location updates and displays the latitude and longitude in the second text view when the location changes. The manifest file requests coarse and fine location permissions.

Uploaded by

Tejas Shinde 45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views2 pages

CurrentLocationDemo PrNo31

The document contains code for an Android application that displays the user's current location. The activity_main.xml layout file contains two text views, one displaying static text and the other to display the location. The MainActivity class uses the LocationManager to request location updates and displays the latitude and longitude in the second text view when the location changes. The manifest file requests coarse and fine location permissions.

Uploaded by

Tejas Shinde 45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Code for activity_main.

xml file

<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/teal_200">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" This Program shows your current location"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="10dp"
android:layout_marginTop="100dp"
android:textColor="@color/purple_700"/>

</LinearLayout>

// Code for MainActivity.java file

public class MainActivity extends AppCompatActivity implements LocationListener {


protected LocationManager locationManager;
protected Context context;
TextView txtLat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
}
@Override
public void onLocationChanged(@NonNull Location location) {
txtLat= findViewById(R.id.textview1);
txtLat.setText("Latitude" + location.getLatitude() +", Longitude:" +
location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(this,"This Your cururent
Location",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(@NonNull String provider) {
Toast.makeText(this," Location Enable",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(@NonNull String provider) {
Toast.makeText(this,"Location disable",Toast.LENGTH_LONG).show();
}
}

// Code for manifest.xml file

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


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

You might also like