Context ctx = getContext(); Display display = ((WindowManager)ctx.getSystemService(ctx.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Yes, there are easier ways to retrieve the screen width on Android but there are cases that this long code is the only solution. You may already have the Context. WindowManager or the Display and so it would be [...]
Essa quinta-feira, dia 6 de Maio de 2010, conversaremos sobre Android na palestra “Android: Visão Geral” a partir das 18:30 na Faculdade Evolução (R. Pedro I, 1276, no Centro). A ideia é falar por cima sobre o mercado mobile, o desenvolvimento Android, as possibilidades e os conceitos principais sobre suas aplicações. Maiores detalhes sobre inscrição [...]
29 April, 2010 - 10:29 am
Tags: Class, Java, method, programming, Reflection
Posted in english | 2 comments
import java.lang.reflect.*; public class Foo { public void bar(int param){ System.out.println(param); } public static void main(String args[]){ Object f = new Foo(); try { Method m = f.getClass().getMethod("bar", int.class); m.invoke(f, 42); } catch (Exception e){ System.err.println(e); } } } $ java Foo 42
8 April, 2010 - 1:09 pm
Tags: getruntime, Java, programming, runtime
Posted in english | No comments
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 [...]
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>(); [...]
This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users. Download: WhoAmI.tar.bz2 – Eclipse project. It’s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions. WhoAmI.apk – [...]
27 February, 2010 - 9:39 pm
Tags: caps lock, Java, programming, socket, upcase
Posted in english | 3 comments
Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior. Features: A client should enter a string and the server would answer the same string, with each symbol in up case, when possible. Default port at 8080. One [...]
22 February, 2010 - 5:16 pm
Tags: development, i18n, Java, JUnit, locale, programming, String, teste unitário, toString, unit testing
Posted in english | No comments
Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C’s like printf formatter. @Override public String toString() { return String.format("VO[a: %.1f, b: %.1f, c: %.1f]", a, b, a+b); } %.1f means a float with one digit precision after the dot separator. [...]
Essa quarta-feira acontece o primeiro CEJUG Café com Tapioca de 2010. O foco serão duas palestras ministradas pelo José Donizetti da Caelum, sendo a primeira sobre VRaptor 3 e a segunda sobre Test Driven Development. O evento será realizado no auditório do Atlântico. Maiores informações no site do evento.
5 January, 2010 - 6:10 pm
Tags: AWT, fonts, Java, JVM, Larabie, Larabie Fonts, programming
Posted in english | Comments Off
Here’s a program that lists fonts available in your JVM. You can also set the environment variable JAVA_FONTS to specify the font directory. import java.awt.GraphicsEnvironment; public class ListFonts { public static void main(String args[]){ GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); for(String font:e.getAvailableFontFamilyNames()){ System.out.println(font); } } } By using pipes you can count how many fonts you [...]