android tips and tutorials

How to print an array in Android Log output (Logcat)

If you need to dump the contents of an array to the Android Log (Logcat) output, I can confirm that this approach works, at least with simple arrays of integers and strings that know how to print themselves:

Log.i("MyAndroidClass", Arrays.toString(arr));

If you’re trying to print more complicated arrays of custom objects you’ll probably need to implement good toString methods on those objects, and then this technique should work.

A note about functional programming in Android

I spent some time last week working on an Android application, and with my newfound knowledge of functional programming (FP), I was trying to apply FP principles to my Android Activities and Fragments.

Android isn’t really meant for FP, but one thing I found that I could do is to move a lot of my business logic out of the Activities and Fragments and into separate classes, where I could often implement methods as static functions. The advantage of this is that it forces you to consciously pass variables in and out of those static functions, rather than mutating them as instance variables (think “global” variables) in your Activities and Fragments (which is a common way to handle them).

I don’t have a specific example I can share today, but when I can I’ll update this post to show specifically what I mean. In the meantime, if you try to move some of your logic out of your Activities and Fragments, I think you’ll see what I mean.

Next version of my Android football game

This image shows a very early (ugly) prototype of the next version of my Android football game. The orange boxes on the bottom-left let you choose the formation, then the “run,” “pass,” and “other” icons stand for tabs that let you choose different plays. I may put the formations inside a tab as well ... that would be more consistent, and it will be something I’ll need to do as the game grows.

A major new feature in the game is that you can can create your own custom teams, with each player on the team having a collection of ratings. For instance, a running back will have rating categories of a) running, b) blocking, and c) pass-catching abilities. Therefore, the running plays will let you choose to run left or right, and passing plays will let you choose the intended receiver.

I’ll write more as time goes on, but this ugly little prototype is one of the first steps in the redesign/upgrade.

Two ways to hide an Android ActionBar on an Activity

If you want to hide the Android ActionBar on an Activity, it looks like there are at least two approaches.

First, add the android:theme="@android:style/Theme.NoTitleBar" entry to the activity’s definition in AndroidManifest.xml:

    android:theme="@android:style/Theme.NoTitleBar" >

A second approach is to add this code in the Activity (or Fragment) onCreate method:

MyShake: A free app that has the ability to recognize earthquake shaking

Per their website: “MyShake is a free app for Android phones that has the ability to recognize earthquake shaking using the sensors in every smartphone. The app runs ‘silently’ in the background on your phone using very little power – just like the step-tracking fitness apps. When the shaking fits the vibrational profile of an earthquake, the app sends the anonymous information to our central system that confirms the location and magnitude of the quake.”

“Our goal is to build a worldwide seismic network and use the data to reduce the effects of earthquakes on us as individuals, and our society as a whole. MyShake also provides users with information about recent earthquakes around the world and significant global historical earthquakes.”

An Android ListView with the Back/Up/Home button enabled

Without much discussion, here’s an Android ListView/ListFragment with its Back/Up/Home button enabled:

An Android ListFragment/ListView with Back/Up button enabled

(That button used to be a Home button, but now it’s used for the Back/Up action.)

And here’s the source code for that ListView/ListFragment:

A simple way to populate an Android ListView with a SQLite database Cursor

Here’s an example of how to populate an Android ListView, where I get the data for the ListView from a database Cursor:

DatabaseHelper.TeamsCursor tc = DatabaseManager.get(getActivity()).getAllTeams();
ArrayList<String> listOfTeamNames = new ArrayList<>();
for(tc.moveToFirst(); !tc.isAfterLast(); tc.moveToNext()) {

// list the team names with an adapter that talks to our listview
TeamNamesAdapter adapter = new TeamNamesAdapter(listOfTeamNames);

There are more formal ways to create an adapter class to work with a Cursor, but for my needs I just needed to get a list of names from a SQLite database table and show them in a simple ListView — part of a ListFragment — and this was the simplest code to write.

FWIW, the example also shows one way to iterate over the elements in a Cursor using a Java for loop.