Archive

Archive for July, 2011

Load WebView with ProgressDialog

July 31, 2011 8 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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // no need to use title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set webview as main content only
        mWeb = new WebView(this);
        setContentView(mWeb);
        // set Javascript
        WebSettings settings = mWeb.getSettings();
        settings.setJavaScriptEnabled(true);
        // the init state of progress dialog
        mProgress = ProgressDialog.show(this, "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) {
        		view.loadUrl(url);
        		return true;
        	}

        	// when finish loading page
        	public void onPageFinished(WebView view, String url) {
        		if(mProgress.isShowing()) {
        			mProgress.dismiss();
        		}
        	}
        });
        // set url for webview to load
        mWeb.loadUrl("http://www.cnn.com");
    }
}

Hope it helps!

Cheers,
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:


android:scrollbars="none"

The attribute will hide the scrollbar on the view.
Cheers,

Pete Houston

Take a screenshot of your View


Quick snippet

    View content = findViewById(R.id.layoutroot);
    Bitmap bitmap = content.getDrawingCache();

Cheers,
Pete Houston

Set line-break in TextView

July 15, 2011 2 comments

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

	<TextView
		android:id="@+id/tvContent"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:singleLine="false"
	/>

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

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

Have fun!

Cheers,
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

package pete.android.study;

import java.util.ArrayList;

import android.app.Activity;
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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mListData = (ListView)findViewById(R.id.lvData);
    }

    @Override
    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);
    		mListData.setAdapter(mAdapter);
    		return;
    	}
    	mAdapter.notifyDataSetChanged();
    }
}

– 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!

 

Cheers,

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

android:launchMode="singleTask"

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

data.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

That’s it!
Hope you learn something!

Cheers,
Pete Houston