Skip to content

Month: April 2010

Life = Risk

A simple and beautiful video about the early failures, things and people that tries to put you down and we have to face before achieve what we really want.

“If you never failed, you never lived”

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 mHash;
		
      mHash = new HashMap();
      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.



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