<?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=".activities.Location">
<TextView
android:layout_marginHorizontal="15dp"
android:layout_marginTop="200dp"
android:id="@+id/txtlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Current Location "
android:gravity="center"
android:textSize="20sp" />
<EditText
android:id="@+id/editTextLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="100dp"
android:hint="your Address is here..."
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck,TouchTargetSizeCheck">
</EditText>
<LinearLayout
android:id="@+id/layButtonH"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="0.15">
<Button
android:id="@+id/btnLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location">
</Button>
</LinearLayout>
</LinearLayout>
package com.daizzyinfo.food18.activities;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.daizzyinfo.food18.R;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class Location extends AppCompatActivity{
protected LocationManager locationMangaer ;
protected Boolean flag = false;
protected Button btnLocation ;
protected EditText editLocation;
protected static final String TAG = "Location";
TextView txtlocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
locationMangaer = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// new code from here......
btnLocation = (Button) findViewById(R.id.btnLocation);
txtlocation = findViewById(R.id.txtlocation);
editLocation = (EditText) findViewById(R.id.editTextLocation);
// Runtime permissions .
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(Location.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);
}
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getLocationCordinates(v);
}
});
}
public void getLocationCordinates(View view ) {
Geocoder geocoder = new Geocoder(this);
List<Address> addressList ;
try {
addressList = geocoder.getFromLocationName(editLocation.getText().toString(),1);
if(addressList!=null)
{
double lat = addressList.get(0).getLatitude();
double log = addressList.get(0).getLongitude();
txtlocation.setText("longitude " + String.valueOf(log) + "latitude " + String.valueOf(lat));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Comments
Post a Comment