Applies to:

  • Android Studio (Beta) 0.8.6
  • Google Android Maps API

I am writing an app for a tablet running Android and wanted to include a Google Map. The following exercise focuses solely on creating an app which opens Google Maps at your current location.

I couldn't find any instructions using the tools I had (Android Studio) which is why I've written this article.

A reference to what size an image background should be set to for various screen sizes.

A background image I was using was unedited at 1600x2133 (364kb) and was not displaying on smaller devices. Configuring drawables for each layout seemed to be the only solution.

res/drawable-ldpi      240x320
res/drawable-mdpi      320x480
res/drawable-hdpi      480x800
res/drawable-xhdpi     640x960
res/drawable-xxhdpi    960x1440
res/drawable-xxxhdpi   // for launcher only 640dpi
res/drawable-tvdpi     1.33*mdpi // not needed as mdpi or hdpi is used

Applies to:

  • Android Studio (Beta) 0.8.9
  • Android Google Maps v2

A sequel to my article "Basic Android App using Google Maps and Current Location", this article suggests how to add Map as a sub activity. Note that this article is based on a new blank project which does not include code from the previous example but you can still build this over the previous example (as I did).

The previous article meant that the Google map was your main activity and any other activity would be a sub-activity; returning to the map activity would restart the app as a menu item, it would need to launch a new intent... I wanted to change my previous example so that the homepage of the app (the starting page) will simply display a button to the map (in case of offline mode or incompatibility with the device).

In this example, I'm going to add the map as a fragment. This is the most rudimentary example (display map, move camera and display marker) and can be run from any other activity:

A quick article on how to display a OK/Cancel popup which I use as a message to the user for debug purposes.

It's similar to javascript's message box or confirm box, only Android let's you redesign the dialog. For my purposes I have used an XML as the layout.

This displays a standard Ok/cancel message popup which you attach to some event

import android.content.DialogInterface;

new AlertDialog.Builder(this)
        .setTitle("Some Title")
        .setMessage("some message")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                        // Some stuff to do when ok got clicked
        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                        // Some stuff to do when cancel got clicked

Additional: Pre-populating default value and returning input
Lots of confusing answers out there on the web but here's an all-in-one where I
  • Open an AlertDialog designed with a custom XML layout
  • Set the hint text of a specific user input field (EditText)
  • Store the input in a TextView (invisible or visible) held in the calling XML
/* 201411041253 */
private String hangar_aircraft_name;

public void editHangarAircraftName(View view) {

        // get current value for hint
        TextView currentName = (TextView) findViewById(;
        hangar_aircraft_name = currentName.getText().toString();

        // get the AlertDialog XML layout file
        final View v = getLayoutInflater().inflate(R.layout.my_custom_dialog, null);

        // set the hint of the EditText in the AlertDialog
        EditText inputText = (EditText) v.findViewById(;

        // Start building the AlertDialog
        AlertDialog.Builder b = new AlertDialog.Builder(this);

        // set the view of the AlertDialog along with changes (eg. changed "hint")

        // set OK button action
        b.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                        // Get the f !!!
                        Dialog f = (Dialog) dialog;

                        // this is the EditText that has the value I want
                        EditText inputTemp = (EditText) f.findViewById(;  

                        // this is the TextView to store the value in (not the AlertDialog but main xml)
                        TextView newName = (TextView) findViewById(;  

                        // setting header text to returned input

        // set Cancel button action
        b.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

        // create and show the alertDialog
The key mistake I was doing was trying to recalculate which view the main activity item was in.