Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

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);

Wednesday, September 29, 2010

JS1k

I just know that there is a competition about javascript called JS1k. As the name self-explained, this is about to write a javascript program with 1k only. Unfortunately, when I know this event, the deadline passed. The rules are
  1. Create a fancy pancy Javascript demo
  2. Submissions may be up to 1k. (And not crash)
  3. Externals are strictly forbidden, unlike "some" contests. (Good luck minifying jQuery though!)
  4. Must work on current fx, sa, ch and o. (Let's level the playing field)
  5. Minification and hacks allowed. (Go nuts)
  6. Bonus points if your submission fits in one tweet ;)
  7. Last day to submit is 10 September 2010
If you are interested, you can visit JS1K home page. Here is the top one and the author shows his source code with comment in his website, http://marijn.haverbeke.nl/js1k.html

Friday, September 24, 2010

Do Programming Quickly??

I think I am still young in programming area. I learnt Pascal in High School and learn C++ and Java in University. Most of the time, implementing a program takes lots of time especially designing Graphic User Interface (GUI).

Ubuntu now provides a solution "Quickly" that helps people to write program QUICKLY.
https://wiki.ubuntu.com/Quickly

Quickly uses Python as programming language, GTK as the GUI. It provides lots of handy commands help you to program. Basically, what you need first is to install Quickly, but it only has Ubuntu version only. I hope it can support more distribution later.

1. To install
$ sudo apt-get install quickly


2. Build a project
$ quickly create ubuntu-application myapps
$ cd !$

(refer to my previous post to know what !$ means if you don't know)

3. Edit the project
$ quickly edit


4. Design the GUI
$ quickly design


5. Run the program
$ quickly run


Which Quickly, it is really easy to build your own application with small amount of time.