Tag: Android app development

  • Android Maps: Get current location coordinates with Google API Client Builder

    To get your current location via Google Map in an Android app, this was how we do it:

    Stop. Don’t use that! The method GoogleMap.getMyLocation() is deprecated. Instead, we need to use FusedLocationApi and the callback onLocationChanged().

    Here’s an example code that works for me ( consider copying the contents of onResume() into onStart() ):

    Code source: How to get current location of device on google maps api v2, getMyLocation() method is deprecated

  • Android Studio: add Google Map from empty activity (dev note)

    //Update: I prepared a simple Android app that features a Google Map with markers that form clusters together when zoomed out. Check it out: Simple Android Map Clustering on GitHub.

    This “dev note” of mine will give you a rough idea on how to add a Google Map to your Android up from an empty activity, not from a pre-made Google Maps activity.

    I won’t be able to write everything in detail here because I’m in a hurry, and so I’m calling this article a “dev note” instead of tutorial. 🙂 This dev note can be considered a “guide” too.

    This dev note was based on Android Studio v.2.1 running on MAC OS X

    From your Android Studio’s SDK Manager, install Google Play Services.

    Add this as child of <application> tag in AndroidManifest.xml:

    Later we’ll replace YOUR_API_KEY with a key from Google API Console.

    Also add these to your AndroidManifest.xml as direct child of manifest tag:

    We need to get an API KEY for the value attribute of this meta data. Let’s get one from the Google API Console — create a new project there and then search for Google Maps Android API. Enable it.

    After enabling it, you’ll find a way to get an API key that we need from Google API Console. Go to Credentials -> Create Credentials -> API Key -> Android Key. Before we are given the API key, we need to provide Google API Console a SHA1 certificate fingerprint from our computer and our app’s package name.

    To generate SHA1 certificate fingerprint on a Mac, type this on the terminal:

    On Windows, try this command:

    When prompted for a keystore password, enter the word debug as your password.

    Copy the SHA1 certificate fingerprint and then paste it into Google API Console. Enter your package name as well (e.g. com.yourdomain.projname). You’ll be given an API Key in return.

    Note: My Android Studio says ‘In the Google Developer Console (https://console.developers.google.com), ensure that the “Google Maps Android API v2” is enabled.’ but I can’t find the option for V2.

    Remember the meta-data tag we added in the AndroidManifest.xml? Replace the value with the API key that you just copied from Google API Console.

    Resource: https://developers.google.com/maps/documentation/android-api/signup

    Open app/build.gradle file and add the following as dependency:

    Sync your project with Gradle files.

    This was one of the tutorials I followed: GOOGLE MAPS ANDROID API V2 TUTORIAL USING ANDROID STUDIO and I based my XML plus Java codes on the author’s.

    My files in the end…

    Here were the codes of my Android app when I finally made the Google Map show up in the activity:

    app/build.gradle

    build.gradle

    app/src/main/res/layout/activity_main.xml

    app/src/main/java/net/catzie/maptest/MainActivity.java

    app/src/main/AndroidManifest.xml

    …even though the Google Map now shows up on my Android activity, I get these errors that I’m not able to fix yet:

  • Android Development: Permission denied when running ./gradlew in Mac

    I’m using a Mac at the moment to work on my Android app projects.

    For some reason I need to run ./gradlew in the terminal, but I was getting this error:

    -bash: ./gradlew: Permission denied

    To fix this, what I did was run the following commands:

    After those commands, my Mac downloaded numerous files and it took an hour to finish because of our poor Internet connection in the workplace. 🙁

    After an hour and a half, I was able to finally use the command ./gradlew on the Mac Mini. Phew!

  • Android Studio and the 64K methods limit (DEX error)

    Hi! I’ll share a few tips with you on how to find out the number of methods that your Android app has. I’m gonna show you a few tools such as APK file methods counter, and an Android plugin that will show you the number of methods each imported class or dependency has. These will help you figure out if you’re nearing the 64K methods limit, and determine which classes/dependencies must be causing it.

    First off, try this online drag-and-drop tool that will show you the total method count as well as breakdown based on the APK file you’ll use: APK Method Count

    Now, let’s install the plugin. After installation, you should be able to see method counts on your app/build.gradle file.

    Install plugin to check for unused dependencies: Jonnyzzz Dependencies. I used version 1.0.10. To do this, in [Windows] go to File -> Plugins, or in [Mac] go to Android Studio -> Preferences. Press “Browse Repositories” then find and install Jonnyzzz Dependencies. Restart Android Studio.

    To use, from the menu, select Tools -> Unused Module Dependencies -> Analyse All Modules

    You’ll be prompted for an option to remove all unused dependencies. I could not do that because I had a team mate who might use some of those in the list, so I manually deleted libraries that I’m sure we no longer need.

    Sync Gradle, Clean, or even Rebuild… and run your app again.

    I must admit that this DEX file error is a nightmare to me. xD I used to only code my Android app in a Windows 7 computer, which was very slow. Then some office friends decided to lend me a higher-specs Mac Mini that nobody’s using. It’s helping a lot, but the app I’m working on in Windows is getting 64K methods limit in Mac. I had the exact same copy of my Android Studio project in both Windows and Mac computers, but only the one in Mac was getting 64K methods limit error. 😆

    Now I’m thinking of the small differences in my Windows’ and Mac’s config for Android Studio…

    Could it be because ‘com.google.gms:google-services:2.0.0-alpha9’ and Android Studio v.2.1 in Mac, while my Windows 7 had ‘com.google.gms:google-services:2.0.0-alpha6’ and Android Studio v.1.4?

    From what I understand in the Methods Count pages for Google Services 2.0.0 Alpha 6 and Alpha 9, Alpha 6 has more methods than Alpha 9.

    My Android Studio v.1.4 in Windows using Alpha 6 (more methods) but the app runs without problem, while Android Studio 2.1 in Mac using Alpha 9 (less methods) triggers the 64K methods limit. 😆

    I don’t want to keep thinking about this right now because I have pending tasks to do. 😆 But it’s nice to know roughly what we can do next time I get this dreaded 64K methods limit error.

  • Android NetworkOnMainThreadException at StrictMode fix (with deprecated methods used)

    A JSON Parser developed by some members of the Android dev team. But a few weeks after completion of the parser, several methods became deprecated.

    And when we use that parser, we get errors. Part of the stack trace is as follows:

    Not sure if the NetworkOnMainThreadException is being thrown because of the deprecated methods but the following code is what prevents our app from getting the error.

  • Android/Java: get variable data type

    For some reason you might need to get the data type of your Java variable. Here’s an example of what you can try:

    Then do whatever you like with data_type, whose value should be “String”. 🙂

  • Get device’s IMEI in Android app development

    Here’s the content of my MainActivity class where I log the IMEI of my test device.

    You also need to add this permission to your AndroidManifest.xml file:

    Place it inside the <manifest></manifest> tag, but same level as <application></application>.

    Source: How to get the device’s IMEI/ESN programmatically in android?

  • Android: open web page in a browser

    To open a web page in an Android app, we need to create a new Intent and start a new Activity with it.

    Here’s a Java method that we can re-use:

    Just pass the URL of the web page you’d like to open to this method.

    Source: How to open a website when a Button is clicked in Android application?

  • Android / Java: Get the name of method being executed

    If you want to trace the name of the method that’s being executed, the Java code snippet in this page might help you.

    This is especially handy when you are debugging by tracking every method that is executed. You can enclose these lines of codes in a function that you call in every method — that’s easier than typing the name for each method you’re tracking, right? 🙂

    The String variable methodName will contain the name of the method where you place the following lines of code:

  • Android: underline text in your app

    When I need to underline text in an app that I’m developing, I like doing it programmatically. The code is in the Java file, not in the XML.

    Maybe we should make a function for this where we pass the ID of the TextView whose content we’d like to underline.

    Disadvantages in this method:

    • It’s done in the Java file, not in UI files such as XML. It makes more sense to add codes for styling in XML files to me.
    • This underlines all text in the TextView. What if we need to underline only one text in a paragraph?

    If you have any suggestion, feel free to comment. 🙂

    // EDIT:

    Here’s a method I prepared to make underlining TextView values easier:

    public void setTextViewUnderline(TextView myTextView){
    String htmlString = “” + myTextView.getText() + ““;
    myTextView.setText(Html.fromHtml(htmlString));
    }