Category: Android Development

  • Android M runtime permissions utility class


    Hello world, I prepared a PermissionsUtil.java class and it’s on GitHub.

    Check it out: AndroidPermissionsUtil

    Why use it? It will help by letting you write less code as you add conditions to your Android app that supports Android 6.0 (Marshmallow). The utility class defines helper methods that were used in the following example.

    Sample usage of Android M PermissionUtils

    On the activity where you need to check for runtime permissions, add these:

    And add the callback function to the same activity:

    Feel free to use in on any project. And please let me know if you have suggestions on how to improve that Android M runtime permissions utility class.

  • Android app codes for Google Maps with clustering

    I prepared Android app codes with Google Maps and clustering feature. Feel free to use these as basis for your new projects.

    They are on GitHub:

    Android map clustering with default markers

    [1] Default Map Markers:
    catzie/SimpleAndroidMapClustering

     

     

    I simply took the necessary classes and res files from Android Maps Utility demo, and then placed them into bare Android Studio projects for simplicity. For me, it’s easier to start a new “maps with clustering” project on simple apps like these, rather than downloading the entire Android Maps Utility Demo, picking out the necessary files, and fixing errors before getting started each time. 😆

     

  • 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: Switch into another activity

    To switch into another activity, the method we use is startActivity().

    This is what starts another Android activity:

    Replace NameOfActivity with the name of any activity you’d like to switch into.

  • Android: Basic WebView Tutorial

    A WebView lets you pull a web page and display it in your Android app. It doesn’t take long to implement an Android WebView. Read on. I’ll show you how.

    AndroidManifest.xml

    First, open your manifest file to add this permission for INTERNET:

    The web page won’t be loaded into your app without this permission.

    This is how my manifest file looks like:

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\AndroidManifest.xml

    activity_main.xml

    Then we’re adding a WebView element to the XML file. My XML file is named activity_main.xml

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\res\layout\activity_main.xml

    MainActivity.java

    I have MainActivity.java here, which loads activity_main.xml and then loads a web page (google.com) into the WebView.

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\java\net\catzie\webview\MainActivity.java

    The following line of code attaches the WebView whose id is “webview” to the variable “webView”:

    And this allows JavaScript in the WebView:

    This one is what actually loads a given URl into the WebView:

  • 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”. 🙂