Tag : java

If you want to sort any ArrayList in java, you need to make sure, your objects in the arrylist are comparable. This is done by using implements Comparable and implementing the abstract method compareTo (T cmp). For example if you want to sort some highscores you can do it like this. public class Highscore implements ..

Read more

In Java there is a easy was to capture screenshots. The following captureScreenshot method will capture a screenshot of the display of your screen and save it to the disk. public void captureScreenshot(String filename) throws Exception { java.awt.Dimension size = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); java.awt.Rectangle rectangle = new java.awt.Rectangle(size); java.awt.Robot robot = new java.awt.Robot(); java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle); ..

Read more

There is a simple solution, to get the name of the current method in Java. It uses the stack trace to get the name of the mehod. It works with different virtual machines like oracle, dalvik (Android), Open JDK, etc. String name = Thread.currentThread().getStackTrace()[1].getMethodName(); But don’t use this in time-critical code, because working with the ..

Read more

This is a simple Android background Service Template. You can use it to run updates in the background without blocking the UI. To start and stop the service use the following lines in your activity: // starting the service Intent intent = new Intent(this, UpdateService .class); startService(intent); // stopping the service Intent intent = new ..

Read more

If you play around with height and width for a android device you often need to convert between real pixel (px) and device indipenden pixel (dp, dip). The following methods are a easy solution to convert between px to dp private float px2Dp(float px, Context ctx) { return px / ctx.getResources().getDisplayMetrics().density; } private float dp2Px(float ..

Read more

Short answer Yes Long answer Shared Preference are saved in the file system of your device, and the Shared Preferences Mode defines whether other applications have the right to read or write the Shared Preferences or not. Android SharedPreferences with mode “MODE_PRIVATE”, “MODE_WORLD_READABLE” and “MODE_WORLD_WRITABLE” are possible in android. MODE_PRIVATE This is the default mode, ..

Read more

If you open a URLConnection with the Hypertext Transfer Protocol, the Object of URLConnection that is returned from openConnection is a HttpURLConnection. This class defines a method, which is able to access the status code of the http response. To get the status of your http reesponse, it is necessary to cast the URLConnection Object ..

Read more

With the getAll function of sharedPreferences it is possible to save the shared preferences to a file. The following code example is probably the easyest way to do it. With the saveSharedPreferences method you can save all shared preferences a file to the root of your sdcard. Make sure your App has the following permission. ..

Read more

If you have to create an object with default constructor, you can invoke the newInstance() method on a Class. This simple class creates the instance of a named class using the empty default constructor by calling the newInstance method: public class ReflectionTester throws InstantiationException, IllegalAccessException, ClassNotFoundException { public static Object createObject(String className) { return Class.forName(className).newInstance(); ..

Read more