Quick snippets of Android-related code and bookmarks to increase productivity.
Map
There are two ways to implement Google Maps into an Android app: MapFragment
and MapView
Setup:
Good
Google Maps for Android, part 1 (Intro): How to implement Google maps from scratch, setup Google Developer Console to enabled the Maps SDK and get the API key- Add dependency:
implementation 'com.google.android.gms:play-services-maps:16.1.0'
- Add dependency:
Good
Google Maps for Android, part 2 (User Location): How to access user's location, including setting up permissions
More resources:
- GoogleMap API Reference: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap
How to show map marker at street address
import android.location.Geocoder
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
...
val markerName = "Example"
val streetAddress = "123 Fake St; Denver, CO 80246"
if (Geocoder.isPresent()) {
val geocoder = Geocoder(requireContext())
val addresses = geocoder.getFromLocationName(streetAddress, 1)
val address = if (addresses == null) null else addresses[0]
if (address != null) {
val location = LatLng(address.latitude, address.longitude)
map?.addMarker(MarkerOptions().position(location).title(markerName))
}
} else {
toast("Geocoder is not available on your device")
}
Navigation
View
Default sample page
Syntax Highlighting with Pygments
{% highlight language="java" %}
public abstract class OrchidGenerator extends Prioritized implements OptionsHolder {
protected final String key;
protected final OrchidContext context;
@Inject
public OrchidGenerator(OrchidContext context, String key, int priority) {
super(priority);
this.key = key;
this.context = context;
}
/**
* A callback to build the index of content this OrchidGenerator intends to create.
*
* @return a list of pages that will be built by this generator
*/
public abstract List<? extends OrchidPage> startIndexing();
/**
* A callback to begin generating content. The index is fully built and should not be changed at this time. The
* list of pages returned by `startIndexing` is passed back in as an argument to the method.
*
* @param pages the pages to render
*/
public abstract void startGeneration(Stream<? extends OrchidPage> pages);
}
{% endhighlight %}
1 public abstract class OrchidGenerator extends Prioritized implements OptionsHolder {
2
3 protected final String key;
4
5 protected final OrchidContext context;
6
7 @Inject
8 public OrchidGenerator(OrchidContext context, String key, int priority) {
9 super(priority);
10 this.key = key;
11 this.context = context;
12 }
13
14 /**
15 * A callback to build the index of content this OrchidGenerator intends to create.
16 *
17 * @return a list of pages that will be built by this generator
18 */
19 public abstract List<? extends OrchidPage> startIndexing();
20
21 /**
22 * A callback to begin generating content. The index is fully built and should not be changed at this time. The
23 * list of pages returned by `startIndexing` is passed back in as an argument to the method.
24 *
25 * @param pages the pages to render
26 */
27 public abstract void startGeneration(Stream<? extends OrchidPage> pages);
28 }
Embed Github Gist
{% gist user="cjbrooks12" id="83a11f066388c9fe905ee1bab47ecca8" %}