Skip to content

Month: April 2010

calling commands in Java

I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands.

import java.io.*;
import java.util.*;
public class Exec {
   public static void main(String args[]) throws IOException {
      Process proc = Runtime.getRuntime().e xec(args);
      BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
      String line;
      while ((line = br.readLine()) != null) {
         System.out.println(line);
      }
   }
}

Usage:

java Exec VALID_COMMAND

Example:

$ java Exec echo hello
hello

ps: I had to write “e xec” instead of exec because it was triggering some very strange security protection in the blog engine here. If you need to compile this code change that. =P Also there’s no error handling, you should pass a valid command when executing this code.

Iterating over a HashMap

Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent.

import java.util.HashMap;
import java.util.Map;
 
class Foo {}
 
public class Main {
 
   public static void main(String args[]){
      Map<Byte, Foo> mHash;
 
      mHash = new HashMap<Byte, Foo>();
      mHash.put((byte)1, new Foo());
      mHash.put((byte)2, new Foo());
      mHash.put((byte)3, new Foo());
 
      for(Foo f: mHash.values()){
         System.out.println(f.toString());
      }
   }
}

Android TextView Shadow

How to add a shadow in a text view? How to improve the text readability on widgets?

There’s four properties on TextView related to shadows.

First a a normal TextView XML declaration.

<TextView
android:text="I thought what I'd do was, I'd pretend I was one of those deaf-mutes."
android:gravity="center_horizontal"
android:textColor="#000000"
android:layout_width="200px"
android:layout_height="fill_parent">
</TextView>

Now the same TextView with the four shadow properties, the color, the x,y offset and the blur radius.

<TextView
android:text="I thought what I'd do was, I'd pretend I was one of those deaf-mutes."
android:gravity="center_horizontal"
android:textColor="#000000"
android:layout_width="200px"
android:layout_height="fill_parent"
 
android:shadowColor="#555555"
android:shadowDx="5.0"
android:shadowDy="5.0"
android:shadowRadius="3.0">
</TextView>