IdleSun's Weblog

August 27, 2011

How to make a TextView scrollable with a Scrollbar

Filed under: Android,Java,UI,XML — idlesun @ 8:08 am
Tags: , , , ,

 

This post has been moved to my new blog.

 

July 29, 2011

Android Parcelable Example 3 – Subclass, Inheritance

Filed under: Android,Java — idlesun @ 7:04 am
Tags: , , , , ,

 

This post has been moved to my new blog.

 

July 15, 2011

Android Parcelable Example 2 – Sub-object and List

Filed under: Android,Java — idlesun @ 7:48 pm
Tags: , , , , ,

 

This post has been moved to my new blog.

 

July 1, 2011

Android Parcelable Example 1 – boolean and enum

Filed under: Android,Java,Uncategorized — idlesun @ 6:44 pm
Tags: , , ,

 

This post has been moved to my new blog.

 

April 8, 2011

How to make PreferenceActivity use non-default SharedPreferences

Filed under: Android,Java — idlesun @ 5:49 am
Tags: , ,

 

This post has been moved to my new blog.

 

December 12, 2010

Android Handler and Message Tutorial

Filed under: Android,Java — idlesun @ 1:44 am

 

This post has been moved to my new blog.

 

November 18, 2010

How to use an Java enum like an integer (for Android Message.what)

Filed under: Android,Java — idlesun @ 4:52 am

Java enum is powerful. However, it is tricky if you want to use it as integer just like in C/C++. Actually, it is not easy at all because Java enum elements are objects. So it is best to use Java enum as enum. But what about the case you need to pass it back an forth as an integer. One example is when you want to use enum for a ‘what’ of Android Message. Lets say you have defined the enum, WhatAbout, like this:

enum WhatAbout { ART, LIFE, MONEY, WORLD };

And you want to sendMessage to someHandler like this:

someHandler.sendMessage(updater.obtainMessage(WhatAbout.ART.ordinal(), 0, 0));

As you can see enum’s ordinal() method solves the issue. Now look at the receiver side. Here is the code for handleMessge() method of someHander

  public void handleMessage(Message msg) {
    if (msg.what == WhatAbout.ART.ordinal()) {
      // Do something about ART
    } else if (msg.what == WhatAbout.LIFE.ordinal()) {
      // Do something about LIFE
    } else ...
  }

It will work, but looks ugly. Using switch must be better. However, unfortunately, the ordinal() method cannot be a case expression because it is not a constant expression. So here is my trick to get around that restriction and use switch:

  static WhatAbout[] wa = WhatAbout.values();

  public void handleMessage(Message msg) {
    try {
      switch(wa[msg.what]) {
      case ART:
        // Do something about ART
        break;
      case LIFE:
        // Do something about LIFE
        break;
      ...
      }
    } catch (Exception x) {
      // Handle ArrayIndexOutOfBoundsException exception
    }
  }

The point is having an enum value array instantiated and use it in the switch expression.

November 11, 2010

Android SharedPreferences.edit().commit() lies! Not really.

Filed under: Android,Java — idlesun @ 5:37 am

 

This post has been moved to my new blog.

 

June 18, 2010

Getting Android source in Ubuntu 10.04

Filed under: Android,Ubuntu — idlesun @ 8:38 pm

While I was following the Android instruction I ran into a couple of difficulties. So here is the list of them with how I resolved.

Forcing WUBI to install 32bit version.

I used Ubuntu Windows Installer (WUBI). It really simplified Windows-Ubuntu dual-boot setup. However, on my desktop it automatically choose 64bit Ubuntu without asking me. According to Android instruction 32-bit Ubuntu is preferred. So, I had to use command line option to install 32-bit Ubuntu using WUBI. From Windows command prompt, run

> wubi.exe --32bit

For more on WUBI: Wubi Guide.

Couldn’t find package git-core

Right after Ubuntu installation, if you follow Android instruction to install required packages then this error may occur. Then run this:

> sudo apt-get update

Couldn’t find package sun-java5-jdk

This will occur because Ubutu 10 comes with jdk 6. I followed the instruction on http://zebardast.ir/en/installing-sun-jdk-5-on-ubuntu-9-10-and-10-04/.

Making sure PATH includes ~/bin

Once ~/bin is created, at home run:

> source .profile
> echo $PATH

Now, following Android instruction was good enough to finish the task.

June 5, 2010

How to hide Android status bar and title bar.

Filed under: Android — idlesun @ 12:26 am

I  looked for how-to. And found one that uses API: http://www.anddev.org/hide_statusbar-title_on_qvga_-_getting_screen_resolution-t2689.html. This solution is nice when app window style needs to be determined and set dynamically.

Later, I found an another way that uses style and much easier if app window style is fixed.  It is just adding the android:theme attribute to activity element in AdroidManifest.xml file:

<activity android:name=".HelloWorld" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

For more info, check the last part of http://developer.android.com/guide/topics/ui/themes.html and the Android Styles and Themes XML files.

Next Page »

Create a free website or blog at WordPress.com.