Skip to content

Instantly share code, notes, and snippets.

@kostovtd
kostovtd / AlertDialog.java
Last active December 7, 2016 21:03
A code snippet for overriding the behavior of AlertDialog buttons
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
/**
* Just a dummy container class in order to
* collect {@link com.google.android.gms.location.Geofence}
* and {@link com.google.android.gms.maps.model.LatLng} in one entity
*/
public class CompanyLocation {
private LatLng coordinates;
private Geofence geofence;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, MapsView {
private GoogleMap mMap;
private MapsPresenter presenter;
private Marker currentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
public interface MapsView {
void generateMap();
void updateLocationOnMap(Location location);
void showGeofences(List<CompanyLocation> companyLocationList);
}
public interface MapsPresenter {
void connectToLocationService();
void disconnectFromLocationService();
void fetchCompanyLocations();
void onMapReady();
}
public class MapsPresenterImpl implements MapsPresenter, LocationCallback, GeofenceCallback {
private static final String TAG = MapsPresenterImpl.class.getSimpleName();
private MapsView view;
private GoogleLocationApiManager googleLocationApiManager;
private GeofencingManager geofencingManager;
private List<CompanyLocation> companyLocationList = new ArrayList<>();
public MapsPresenterImpl(MapsView view, FragmentActivity fragmentActivity, Context context) {
if(view == null) throw new NullPointerException("view can not be NULL");
<android.support.v7.widget.Toolbar
android:id="@+id/your_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/your_icon"
public class GoogleLocationApiManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
ResultCallback<LocationSettingsResult>, LocationListener {
private static final String TAG = GoogleLocationApiManager.class.getSimpleName();
private static final int LOCATION_REQUEST_INTERVAL = 10000;
private static final int LOCATION_REQUEST_FASTEST_INTERVAL = 5000;
private static final int LOCATION_REQUEST_PRIORITY = LocationRequest.PRIORITY_HIGH_ACCURACY;
private GoogleApiClient mGoogleApiClient;
private LocationSettingsRequest.Builder mLocationSettingsRequestBuilder;
Display display = getWindowManager().getDefaultDisplay();
View view = findViewById(R.id.YOUR_VIEW_ID);
view.measure(display.getWidth(), display.getHeight());
view.getMeasuredWidth(); // view width
view.getMeasuredHeight(); //view height
public interface GeofenceCallback {
void onGeofenceResultAvailable();
}