android

android tips and tutorials

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()) {
    listOfTeamNames.add(tc.getTeam().teamName);
}

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

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.

Source Code to The Busy Coder's Guide To Advanced Android Development alvin January 24, 2017 - 4:39pm

This link has the source code for the online book, The Busy Coder's Guide To Advanced Android Development.

Android SQLiteOpenHelper example source code

Here’s an Android SQLite class I use in an Android app I wrote in 2014-2015. I’m sharing it here so I can easily find an Android SQLiteOpenHelper example: