patrick1980 patrick1980 - 1 month ago 10
Android Question

Change the visibility of a button when location==null

In method onCreate i have button:

connection.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

{
GoogleApiClient.connect();
}


and at the very bottom android studio generated:

@Override

public void onConnected(Bundle bundle)
{

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;

}

Location location = LocationServices.FusedLocationApi.getLastLocation(GoogleApiClient);

if (location == null)
{

LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient, LocationRequest, (LocationListener) this);

}


And now i describe my problem:
Trying to hide my connection button when location == null, and 'else' unhide.

i found on stack this method:

button.setEnabled(false/true);


but i don't now where i should place this method

What to do to make it work?

Answer

Hiding is done using View.setVisibility(int) - options are View.GONE (completely hides the View, other Views fill its space); View.INVISIBLE (hides the View but it still occupies the layout space); View.VISIBLE (shows the View, again occupying the space).

So as people commented before:

if (location == null) {
    myButton.setVisibility(View.GONE);
} else {
    myButton.setVisibility(View.VISIBLE);
}