Short source code examples

Here’s a source code snippet that shows how to create an Android AlertDialog. If I remember right, I got the initial code from Stack Overflow, and then adapted it for my need, which was to confirm that the user wanted to delete an image from an image gallery:

While writing some Java code today, I needed a method to get the extension from a filename. This code solved the problem:

public static String getFilenameExtension(String filename) {
    String extension = "NoExtension";
    int i = filename.lastIndexOf('.');
    if (i > 0) {
        extension = filename.substring(i + 1);
    }
    return extension;
}

Given an input filename like foo.jpg, the getFilenameExtension method returns the “jpg” part as a String.

I’ve seen a few approach to handling a Swift “Value Changed” event, but when you’re only interested in whole number (50, 51, etc.), this approach seems the simplest:

As a quick note to self, I used this Apache httpd.conf configuration in MAMP on my MacBook Pro when developing my “Focus” web application in 2014:

These are some notes on what happens when I use a PreferenceFragment with the Google Navigation Drawer code.

These are the Fragment lifecycle methods that are run when I go to my PreferenceFragment the first time:

I’ve been working on an Android app that uses a navigation drawer, and uses fragments for each item in the drawer that you tap on. One of the items in the nav drawer is a “Preferences” item, so when I tap on that item, I run the following code from my nav drawer code:

Here’s a short example of how to use an Intent to launch an Android Activity, while also adding some data (an “extra”) to the activity-launching process:

Use this code to get the Android screen orientation:

// orientation (either ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT)
int orientation = getResources().getConfiguration().orientation;

As shown in the comment, the resulting orientation will either be ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT. See the Android docs for more information.

 

As a quick note, I was just in a situation where I wanted to get my ActionBar’s Back/Up button to work just like the Android back button. To get it to work like that, I used this Java code in my Fragment class:

This Java code shows how to implement a couple of things in an onCreateView method inside a Fragment class:

As two quick Android “drawable” notes, if you want to convert a drawable resource into a Drawable reference, you can use code like this:

Drawable myImage = getResources().getDrawable(R.drawable.myImage);

Second, if you want to display a drawable image resource on an ImageView, you can use code like this:

imageView.setImageResource(R.drawable.myImage);

(I share little code snippets out here like this because I can never remember how to do some of these things.)

If you’re working with an Android application, this source code seems to work to load an image from a file:

Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);

The Bitmap and BitmapFactory classes are located in the android.graphics package:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

Assuming that your pathToPicture is correct, you can then add this bitmap image to an ImageView like this:

When you want to provide Up/Back/Home navigation in an Android sub-activity, this Java code doesn’t seem to be needed:

getActionBar().setDisplayHomeAsUpEnabled(true);

The “Add Up Action” docs on this Android.com page state that is needed, but as of some (unknown) Android version it is not needed. All you need to do is specify the “Up/Parent” activity in your AndroidManifest.xml file, like this:

As a quick Android “centering” tip, I found that by adding this snippet to the ImageButton below:

android:layout_gravity="center"

I was able to get the ImageButton to center itself in the Toolbar. Here’s the layout code for the Toolbar and the ImageButton:

As another quick Android example, this Java source code shows how to add a “click listener” (on click/tap event) to an Android Button:

To set an image on an Android ImageButton, you basically just need to put your image in the “res/drawable” directories, and then add this tag to your ImageButton XML definition:

android:src="@drawable/my_image"

That assumes the image file is named my_image.png.

Here’s a full ImageButton example to demonstrate this:

To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:

String prefsKeyTimeBetweenNotifications = getString(R.string.prefsKeyTimeBetweenNotifications);

In this example I’ve defined prefsKeyTimeBetweenNotifications as a string resource key in a res/values/strings.xml file, like this:

Android SQLite FAQ: I’m trying to catch an exception when my SQLiteOpenHelper/SQLiteDatabase insert method fails (typically because of a unique/duplicate constraint); why can’t I catch the exception?

I’m working on a very small Android “notifications” app where I a) display a notification using a background service, b) the user taps the notification, which c) takes them to a view that shows the full text of the notification. To get this to work, I need to send the full text along with the notification. However, this didn’t work easily. The text that was shown by my full view would be updated once, then never again.

After a lot of googling and trial and error, I finally got this approach in my sendNotification method working:

Not much to see here, just a couple of Android permissions that an Android app may need to access the network/internet:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

As the names imply, the first permission requests access to the internet, and the second permission is needed to check the internet status/state. These permissions need to be put in an Android app’s AndroidManifest.xml file.