patrick1980 patrick1980 - 1 year ago 64
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) {


and at the very bottom android studio generated:


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) {


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:


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

What to do to make it work?

Answer Source

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) {
} else {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download