Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, July 30, 2010

Android Title Marquee

I recently wanted the title of my activity to scroll like a marquee whenever it was too long to fit in its space (because it is fetched from the net and can be pretty much anything). I didn't want to use a custom multi-line title view because that would waste screen space and because I want to preserve the native look and feel. The solution I've found is rather hackish, but it works. Here goes the code, with explanatory comments and all:

// make the title scroll! // find the title TextView TextView title = (TextView) findViewById(android.R.id.title); // set the ellipsize mode to MARQUEE and make it scroll only once title.setEllipsize(TruncateAt.MARQUEE); title.setMarqueeRepeatLimit(1); // in order to start strolling, it has to be focusable and focused title.setFocusable(true); title.setFocusableInTouchMode(true); title.requestFocus();

Tuesday, June 1, 2010

Java "Hello, World!" 6x slower than Python

Yup. I know, micro-benchmarks, but I find this one quite interesting. Take two small programs, one Java, one Python:

package com.test; public class TestJava { public static void main(String[] args) throws Exception { System.out.println("Hello, World!"); } } print "Hello, World!"

Running these two through time clearly shows that Java is 6 (six) times slower:

[felix@the-machine bin]$ time java com.test.TestJava Hello, World! real 0m0.142s user 0m0.080s sys 0m0.017s [felix@the-machine bin]$ time java com.test.TestJava Hello, World! real 0m0.142s user 0m0.077s sys 0m0.020s [felix@the-machine bin]$ time java com.test.TestJava Hello, World! real 0m0.154s user 0m0.070s sys 0m0.023s [felix@the-machine python]$ time python test.py Hello, World! real 0m0.024s user 0m0.013s sys 0m0.007s [felix@the-machine python]$ time python test.py Hello, World! real 0m0.025s user 0m0.020s sys 0m0.003s [felix@the-machine python]$ time python test.py Hello, World! real 0m0.026s user 0m0.013s sys 0m0.007s

Not only is Java slower, it's also a lot more code. And I'm not only talking about lines of code here (which, still, are 6 times more; really it's 1 line of Python and 6 of Java -- if you remove the whitespace), but compiled code, too. Look:

[felix@the-machine bin]$ ls -lh com/test/TestJava.class -rw-r--r-- 1 felix felix 595 Jun 1 21:50 com/test/TestJava.class [felix@the-machine python]$ ls -lh test.pyc -rw-r--r-- 1 felix felix 117 Jun 1 21:59 test.pyc

The compiled Java code is 595 bytes, while the compiled Python code is 117 bytes. Five times bigger. Say it with me: Java == BLOAT !

Tuesday, September 22, 2009

Java: parse XML from the web smartly

Up until recently, I used to fetch the entire XML document before parsing it. I've found that that can be extremely unhealthy for your application, especially if you're developing on Android, where resources are scarce and the GC is very unforgiving. The optimal way to parse XML is to parse it while it's loading, so that resources are freed much more efficient. Here's a snippet of code that demonstrates how to do this: URL oUrl = new URL(sUrl); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); MyXMLHandler handler = new MyXMLHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(oUrl.openStream()));
Syntax Highlighting by Pygmentool
This way, XML is parsed as it comes in, and the application is much faster and smoother. Also, if you don't parse the whole XML this is an even greater improvement, because you can stop parsing when you have enough data (by throwing a SAXException) and it will even stop loading data, which results in faster load times and less bandwidth usage.

Sunday, September 20, 2009

Java Package Mayhem

Here's how a 350-some line Android Activity looks like, in terms of imports: import java.io.StringReader; import java.net.URLEncoder; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import com.tastekid.TasteKid.TasteKidUtils.Resource; import com.tastekid.TasteKid.TasteKidUtils.ResourceList; Thank God for Eclipse's Ctrl+Shift+O (yes, I know I bashed Eclipse before, but it seems to behave much more nicely under Ubuntu than under Windows -- it doesn't go above 300MB RAM usage).

Fetch HTTP content in Java

Since programming for Android, I hit my head against every possible snag in the Java programming language. For example, I have to fetch the content of a URL. In PHP, I'd simply do: $data = file_get_contents($url); But no, in Java, no such easiness for you! I had to write my own helper function: public static String getUrlContent(String sUrl) throws Exception { URL url = new URL(sUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); String content = "", line; while ((line = rd.readLine()) != null) { content += line + "\n"; } return content; } Seems very hackish, especially the line starting with BufferedReader. The whole function is actually composed of bits of code found around the web. Bah, why didn't Google choose Python as their default programming language for Android?

Thursday, August 6, 2009

Rant on Eclipse

If you at least once thought about programming in Java, you must have heard about Eclipse. Well, I'm playing around with the Android SDK, learning Java at the same time. While using Eclipse to code for Android is very easy and straightforward, Eclipse itself is a monstrous bloat of an application. I barely have one project and five files open and Eclipse is using up 400MB of memory. Don't believe me? Check for yourself: Quite annoying. And this is nothing, one time it happened that Eclipse was using 700MB with only a couple of files open. I'm not sure whether this is Eclipse's fault or Java's fault, but I think it's a bit of both. Eclipse's fault is that it's a huge application with tons of features you will never ever use. Java's fault is that it was never designed to run desktop applications (in my opinion). Because Java runs in a virtual machine, it can not give memory back to the host OS, so it just keeps using more and more, until it explodes, creating a black hole in your motherboard where the memory chips used to live. Bah, end of rant.