Category: Java

Android: Simple Example of Page Transition Using ViewPager

17 May, 2015 (18:54) | Java, Snippets, Programming, Code, Android

The ViewPager class built in to the Android SDK is a simple method of showing an animated transition between two views. For example, turning a page, or a calendar scrolling through months. This functionality can be added with only a few lines of code. Below I present an example containing pretty much […]

A Quick Snippet for Drawing Images in Java

2 October, 2014 (02:22) | Java, Snippets, Programming, Code, Android

Java does not have the best built-in tools in the world for drawing images, but it’s still useful for many purposes. Below is just a snippet on getting started drawing in Java. For more information on what’s available see the Java Graphics class reference.

BufferedImage i = new BufferedImage(500, 500,BufferedImage.TYPE_INT_RGB);
Graphics g=i.createGraphics();
g.drawLine(0, 0, 500, 500);
g.drawString(”This […]

Android: Accessing other views from inside a custom view using findViewById().

1 October, 2014 (11:15) | Java, Snippets, Programming, Android

If you attempt to call findViewById() on the ID of a view that is not from the Activity that holds the view, findViewById() returns null. I’ve seen a lot of solutions posted for attempting to access a View from outside the activity that created it. Most of them involve inflating the original view […]

Why you should return “this” from your setter methods.

26 September, 2014 (21:35) | .Net, Java, Snippets, Programming, C#, Code

Fortunately most API designers know to use this technique, but it’s still pretty common to find some that do not. It’s generally good practice, that when writing a setter method for a class, that you return the object’s “this” rather than “void”. The reason is that it allows a user of your class […]

Resurecting a broken Android phone as a development sandbox for app development.

24 August, 2014 (22:17) | Java, Programming, Operating Systems, Android

I had the unfortunate incident of cracking the screen on my fairly new Galaxy S5. Since most of the phone worked, I wanted to get all of my old data off, on to my new phone, and finally re-purpose the old phone as a development sandbox which I can dedicate to Android app development. […]

Beware of System.nanoTime() in Java

25 April, 2008 (16:45) | Java, Programming

The documentation for System.nanoTime() (and the function’s name itself) leads developers to believe it’s a much more accurate timer than anything else Java provides. Depending on which set of documentation you read, it claims to use “the most precise available system timer”. While I can’t speak for any architecture other than x86, I […]

The difference between Java’s “final” and C#’s “const”

15 February, 2008 (20:01) | .Net, Java, C#, Computer Science

Final in Java is used to declare a class that can’t be subclassed, a method that can’t be overridden, or …
A final variable means the value won’t change, but the value can still be determined at run time:

final int i;
if ( j > 0 ){
i =1;
} else {
i = […]

Passing immutable types by reference in Java and C#

12 October, 2007 (18:26) | Java, C#, Computer Science

To many, it should be obvious what the following code prints:

public static void main(String[] args){
int x=0;

protected static void SomeMethod(int x){

The code prints 0, because “int” is a native type and is passed by value. […]

MD5 in a few lines of Java

12 March, 2007 (10:58) | Cryptography, Java, Security, Snippets

1 import*;
2 import java.math.*;
4 public class MD5 {
5 public static void main(String args[]) throws Exception{
6 String s="This is a test";