Question: Hello I made a simple gps code using android studio api 2 7 and I want you to tweak it so that when you click

Hello I made a simple gps code using android studio api 27 and I want you to tweak it so that when you click "view saved locations", you can make a section in the app where you can ask the user to type the name of the location, and rate it out of 5 stars. Please dont add new classes its just a tiny little addition. Here is my code. I want the code to remain functional and simple.
MainActivity.java:
package com.example.simplegpsapp;
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.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TextView locationTextView;
private LocationManager locationManager;
private final ArrayList locationList = new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationTextView = findViewById(R.id.locationTextView);
Button recordLocationButton = findViewById(R.id.recordLocationButton);
Button viewLocationsButton = findViewById(R.id.viewLocationsButton);
ListView locationListView = findViewById(R.id.locationListView);
// Initialize Location Manager
locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
// Set up ListView adapter
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, locationList);
locationListView.setAdapter(adapter);
// Record location button functionality
recordLocationButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
getLocation();
}
});
// View saved locations button functionality
viewLocationsButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Showing saved locations", Toast.LENGTH_SHORT).show();
}
});
}
private void getLocation(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
return;
}
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener(){
@Override
public void onLocationChanged(@NonNull Location location){
String locationText = "Lat: "+ location.getLatitude()+", Lon: "+ location.getLongitude();
locationTextView.setText(locationText);
locationList.add(locationText);
Toast.makeText(MainActivity.this, "Location Recorded", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){}
@Override
public void onProviderEnabled(@NonNull String provider){}
@Override
public void onProviderDisabled(@NonNull String provider){}
}, null);
}
}
please make activity_main.xml simple and colorful also for android manifest you need 3 permissions, 1. access fine location 2. coarse location. 3. write external storage. So basically use the code I gave you at the top and build upon it. And please dont add new classes.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!