Android/Kotlin: A FragmentStatePagerAdapter (in TabLayout) example alvin December 1, 2018 - 2:48pm

As a note to self, here’s an example of implementing a FragmentStatePagerAdapter Android class in Kotlin:

Code snippets for rapid prototyping of Android Studio apps alvin August 28, 2017 - 7:22pm

This is a list of Android code examples I’m starting to allow me to create rapid prototypes of Android applications using Android Studio. This is a very early list, I hope to be adding many more Android code snippets over time.

How to show an Android Snackbar message alvin August 6, 2017 - 11:29am

To show an Android Snackbar message from an Activity or Fragment, use Java code like this:

Snackbar.make(view, "going to: " + url, Snackbar.LENGTH_LONG).show();

One key is to remember to call the show() method after make(). I have a tendency to forget to call show() and then wonder why my Snackbar message isn’t showing up. So maybe a better way to show that code is like this:

When is the Android Fragment onCreateOptionsMenu method called? alvin August 3, 2017 - 12:36pm

Android FAQ: When is the Android Fragment onCreateOptionsMenu method called?

I was just working through a problem with an Android Menu and MenuItem, and added some debug code to the methods in my Android Fragment, and found that the onCreateOptionsMenu method is called after onStart. I didn’t put Log/debug code in every activity lifecycle method, but for the ones I did add logging code to, the specific order of the fragment method calls looked like this:

Source code for an Android AsyncTask (REST client) example alvin July 20, 2017 - 12:41pm

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

An Android ActionBar + ListFragment example alvin February 23, 2017 - 10:45am

The source code below corresponds to an article I wrote titled, Android ActionBar example: How to create an options menu item.

Two ways to hide an Android ActionBar on an Activity alvin January 27, 2017 - 1:22pm

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:

How to set Android Spinner (drop down) array data from Java code alvin January 23, 2017 - 9:36pm

As a quick note, this source code shows how to set the data (array) on an Android Spinner (also known as a dropdown list, or drop down list) from Java code:

An Android cheat sheet (my notes, main concepts) alvin January 14, 2016 - 2:33pm

This page is a little unusual for me; it’s basically a terse summary of what I know about Android. I created it because I tend to (a) work with Android for a few weeks or months, and then (b) get away from it for several months, so this page helps me reload everything into my brain.

A Java method to log Android memory use alvin April 15, 2015 - 10:51am

As a quick note today, here’s a little Java method that I use to log Android memory use (RAM use) from an Activity or Fragment:

private void logMemoryInfo(Context context, String TAG) {
   ActivityManager activityManager = (ActivityManager) context.getSystemService(getActivity().ACTIVITY_SERVICE);
   int memoryClass = activityManager.getMemoryClass();
   ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

   Log.i(TAG, "\n------------ RAM -------------");
   Log.i(TAG, "mem class: " + memoryClass);
   Log.i(TAG, "mem avail: " + memoryInfo.availMem);
   Log.i(TAG, "low mem:   " + memoryInfo.lowMemory);
   Log.i(TAG, "threshold: " + memoryInfo.threshold);

   long mb = 1024*1024;
   Runtime runtime = Runtime.getRuntime();
   Log.i(TAG, "Used Memory:  " + (runtime.totalMemory() - runtime.freeMemory()) / mb);
   Log.i(TAG, "Free Memory:  " + runtime.freeMemory()  / mb);
   Log.i(TAG, "Total Memory: " + runtime.totalMemory() / mb);
   Log.i(TAG, "Max Memory:   " + runtime.maxMemory()   / mb);