I am using fusedlocationproviderclient for finding the current location but i want progressbar should display till location found, but i dont understand how to use progress bar in oncomplete() method Here is the code of my fusedlocation function.
progressBar = findViewById(R.id.homrprogressbar);
progressBar.setIndeterminate(true);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
progressBar.setVisibility(View.VISIBLE);
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
countryname = findcountry(location.getLatitude(), location.getLongitude());
if(countryname.equals("India")) {
TypedArray allcountry = getResources().obtainTypedArray(R.array.indianews);
String[] allcountryname = getResources().getStringArray(R.array.indianewsnames);
String[] allcountryurls = getResources().getStringArray(R.array.indiahref);
newsModels = new ArrayList<>();
for (int i = 0; i < allcountry.length(); i++) {
NewsModel newsModel = new NewsModel(allcountry.getResourceId(i, 0), allcountryname[i], allcountryurls[i]);
newsModels.add(newsModel);
}
Singleton.getConstant().addNewsModel(newsModels);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, homeFragment);
fragmentTransaction.commit();
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
progressBar.setVisibility(View.GONE);
}
}
}
}
);
} else {
Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
} else {
requestPermissions();
}
Put progressBar.setVisibility(View.VISIBLE);
under if (isLocationEnabled())