Short source code examples

As a quick note, I just found this getUrlInputStream method in an old Scala project. It needs to be updated and cleaned-up, but if you are looking for some code to get you started with opening a URL as an InputStream in Scala, this may be helpful:

Java Jar file FAQ: Is there an easy way to delete a file from a Jar file?

Yes. Because a Jar file is just a Zip file, you can use the zip command to remove a file from a Jar file, like this:

zip -d MyApp.jar

Of course the file to delete can be any sort of file. I just went through this process where I had to delete a configuration file, and my command looked like this:

Android FAQ: How do I make my Android app display in landscape mode only?

To get an Android application to display in landscape mode only (to lock it in landscape mode), add this line to your Activity definition in the AndroidManifest.xml file:


For example, I’m writing an Android football game right now, and this is the definition for my main activity in the Android manifest file:

If you want to make sure that your users’ Android display stays on while your app is running -- such as if you are writing a game/gaming app -- use this call and flag in the onCreate method of your Android Activity:


Here’s what the Android docs say about the FLAG_KEEP_SCREEN_ON setting:

Without looking things up I don’t know when this changed, but ... to refer to a built-in Android color with the android:drawable XML tag, use code like this:

Android FAQ: What does the Android isScrollContainer XML setting do?

From the Android docs:

Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. If not set, the default value will be true if “scrollbars” has the vertical scrollbar set, else it will be false.

Android FAQ: Where should the ’assets’ directory be when using Android Studio?

Solution: If you want to include an assets folder in your project when using Android Studio, create the folder as src/main/assets, i.e., as an assets folder under src/main.

You can later open files in the assets folder using code like this:

InputStream is = getBaseContext().getAssets().open(relativeFilename);

where relativeFilename is the name of your file, like foo.jpg.

I just added an editable EditText widget to an Android application, and I needed to make the EditText a certain height, and also make the text in the EditText scroll, in case the user added some really long text. I used the following XML in my Android layout to make this happen:

Here’s some source code that shows how to add an Android OnClickListener to a CheckBox:

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:

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:


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