Tuesday, October 19, 2010

Check internet connection in Android

Sometimes, apps need internet access to get content from other remote resources. However, you also need to take care the case when there is no internet access. You can't assume users always has internet access. To check whether a mobile has internet or not, you can use the following code:

public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean connected = (conMgr.getActiveNetworkInfo() != null &&
conMgr.getActiveNetworkInfo().isAvailable() &&
conMgr.getActiveNetworkInfo().isConnected())
return connected;
}


Also, you need to add permission to the app so that it can access network status.



Note: In emulator, you can press F8 to toggle the internet access.

Remove extra space on the right in the WebView

WebView is a very important element in Android. In mobile, people like to scroll everything such as contact list, email, webpage. However, didn't you realize that the scrolling is only in the vertical direction? This makes the user interface more friendly.

In WebView, you can scroll a webpage horizontally and vertically, but it is better to fix the horizontal bar by adjust the size of the contents so that the contents don't expand the View. I did that but I found there is some extra spaces in the WebView to make view scrollable horizontally. First, I think it is related to CSS and margin, so I add

* {margin:0px; padding: 0px;}

But it doesn't work. Finally, I realize that is the problem of the vertical scroll that occupies some spaces. To solve this problem, you can set the scrollbar style to display the scrollbars inside the content area.
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

Sunday, October 3, 2010

[HOWTO] Remove .svn folder under svn repository

When you svn checkout a project via SVN, it will create .svn folder to keep track the changing in this working folder also. However, when you need to give out the source code, this .svn folder may not be good to be distributed to other also. Instead of remove the folder one by one. You can use find command

find PATH -type d -name '.svn' -exec rm -rf {} \;

Friday, October 1, 2010

[HOWTO] Connect Creative Zen with Ubuntu

I have a Creative Zen, and if you need to transfer music to the device, you need another software to help you. In the past when I was using Fedora, there is a software named Gnomad2 http://gnomad2.sourceforge.net/. After compilation, you can use it can transfer your favorite songs.

However, it seems that Ubuntu 10.04 has some problem about this software. I use apt-get to install it first. Then, plugged in the zen, unmounted and started gnomad2. Gnomad2 crashed immediately. I tried "gnomad2" in terminal and received the following output:

Queried Creative ZEN
Segmentation fault

Then, I googled this situation and some tell me I should compile from the source. But it doesn't work for me, here is the guide and you can try

1. Uninstall gnomad2 from your system
$ sudo apt-get remove gnomad2


2. Clean out any left over dependencies
$ sudo apt-get autoremove


3. Download the source files for gnomad2
$ apt-get source gnomad2

Note:
a. You may get some error about dependency, you need to use apt-get to install the dependency if needed.
In my case, it told me I do not have "dpkg-source" then I
$ sudo apt-get install dpkg-dev

b. "apt-get" not "sudo apt-get", you don't need root permissions here as we're not installing anything. Using the sudo command will cause permissions problems later when trying to delete the source files.

4. Configure
$ ./configure

Note:
a. For the first time, it may get following errors
No package 'glib-2.0' found
No package 'gthread-2.0' found
No package 'libnjb' found
No package 'gtk+-2.0' found
Then to solve this problem,
sudo apt-get install libglib2.0-dev

sudo apt-get install libgtk2.0-dev

sudo apt-get install libnjb-dev

b. For the second time,
checking for id3tag.h... no
configure: error: *** id3tag.h C header is needed (missing -dev package?) ***\n*** You might have erroneously installed id3lib instead of libid3tag ****\nThis distinction is very delicate, so PLEASE pay attention! ***
To solve this problem,
$sudo apt-get install libid3tag0-dev


c.For the third time
configure: error: Your intltool is too old. You need intltool 0.35.0 or later.

$ sudo apt-get install intltool


5. compile
$ make


6. install
$ sudo make install


7. Gnomad2 installed successfully! Delete the files and directs apt-get downloaded.

Unfortunately, when I launch Gnomad2, it said it cannot find the device. Finally, I realized that Ubuntu can support my Zen using RhythmBox by default and I can use it to transfer music. Awesome!