Archive for July, 2011

Load WebView with ProgressDialog

July 31, 2011 10 comments

It’s very useful to determine the status while loading a web page. Here a sample to show ProgressDialog to track when WebVIew done with loading URL.

public class MainActivity extends Activity {

	WebView mWeb;
	ProgressDialog mProgress;

    public void onCreate(Bundle savedInstanceState) {
        // no need to use title bar
        // set webview as main content only
        mWeb = new WebView(this);
        // set Javascript
        WebSettings settings = mWeb.getSettings();
        // the init state of progress dialog
        mProgress =, "Loading", "Please wait for a moment...");

        // add a WebViewClient for WebView, which actually handles loading data from web
        mWeb.setWebViewClient(new WebViewClient() {
        	// load url
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		return true;

        	// when finish loading page
        	public void onPageFinished(WebView view, String url) {
        		if(mProgress.isShowing()) {
        // set url for webview to load

Hope it helps!

Pete Houston


Remove scrollbars while scrolling the view

July 31, 2011 1 comment

Sometimes it’s very annoying if you use some ScrollView and the scrollbar just appears while you’re scrolling , such as: using HorizontalScrollView to create a toolbar …

Here the trick to remove the scrollbars, and you still can scroll your view nicely:


The attribute will hide the scrollbar on the view.

Pete Houston

Take a screenshot of your View

Quick snippet

    View content = findViewById(;
    Bitmap bitmap = content.getDrawingCache();

Pete Houston

Set line-break in TextView

July 15, 2011 3 comments

A simple trick to set line-break (new-line character insertion) in TextView:


Now just insert new-line character to have a line-break in your view.

textView.setText("First line \nSecond line \nThird line");

Have fun!

Pete Houston

Organize the technical blog

It’s been a month since I’ve created this blog. Somehow it’s getting pretty much helpful not only for me but also many others interested in Android. So love it! Just having a little plan ahead so I decided to organize it a bit for future, maybe.

Also, I’m getting back to my diary traditional style…”Code Breaker Diary” for Android.

Having a fever, just resting for the rest of weekend.

Categories: Of Diary Tags: , , , , ,

ListView in one activity doesn’t refresh data sending another activity

July 15, 2011 1 comment

There’s a problem in my previous post on Auto Receiving SMS that the ListView in MainActivity doesn’t refresh the underlying data which is sent from BroadcastReceiver.

The reason is:

– When application is load, the onCreate() of MainActivity is called, at this time there’s no point of new arriving message, and that’s done! Data will never update more to the ListView.

The solution:
– While SmsReceiver continuously waits for new message and sends it directly to MainActivity, the new message will be stored on the Intent of the automatically-triggered onNewIntent() method.

– What we need to do is to handle new message inside this method.

– This is the update version of MainActivity


import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView mListData;
    ArrayList<SmsInfo> mListSms = new ArrayList<SmsInfo>();
    SmsInfoAdapter mAdapter;

    public void onCreate(Bundle savedInstanceState) {

        mListData = (ListView)findViewById(;

    protected void onNewIntent(Intent data) {
    	ArrayList<SmsInfo> tmp = data.getParcelableArrayListExtra("ListSMS");
    	mListSms.add(tmp.get(tmp.size() - 1));

    	if(mAdapter == null) {
    		mAdapter = new SmsInfoAdapter(this, mListSms);

– Yes, at first time when new message arrives, the adapter is null and it should be created.

– The next time it’s not null but we need to notify the ListView about data changed.

Question here: why do I receive new message storing in a temp ArrayList instead of directly using mListSms itself?

The answer is: I don’t want to change the reference of data in which I already set into the adapter at the first time. Assigning mListSms to the new message ArrayList means to create new ArrayList<SmsInfo> not using the old one. If I do, the, calling of mAdapter.notifyDataSetChanged() won’t respond or do nothing because it doesn’t have reference to the old list. Also, the new message arriving always stays at last position in the sending list from SmsReceiver, so I just need to add the last item from temp list is enough.


Hope you learn something from this!



Pete Houston

Multiple instances of the same activity are created

In my previous post on auto receiving SMS, there’s a problem you can find easily that everytime a new message arrived, a new instance of MainActivity is created (which is a new task launched). It’s kind of wasting memory resources, right?

Here the solution:

1. Add attribute “android:launchMode” for MainActivity in AndroidManifest.xml, I use “singleTask


2. Add an additiona activity launching flag: FLAG_ACTIVITY_SINGLE_TOP, in order to make it always on top of the screen.


That’s it!
Hope you learn something!

Pete Houston