Posts Tagged ‘tips’

Get Current IP Address of Device

November 1, 2011 1 comment

Here a quick snippet I wrote to get current IP of device.

public String getDeviceCurrentIPAddress() {
	static final String DOT = ".";
	WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
	int IP = wifiManager.getConnectionInfo().getIpAddress();
	return (
		(( IP >> 24 ) && 0xFF ) + DOT +
		(( IP >> 16 ) && 0xFF ) + DOT +
		(( IP >>  8 ) && 0xFF ) + DOT +
		(  IP         && 0xFF )


Pete Houston


How to Kill Your Application

November 1, 2011 Leave a comment

If your want to kill your application, there are three ways AFAIK:

>> Method #1


>> Method #2


>> Method #3

finish(); // one provided method of 'Activity' class

There’s an old usual method is to use `ActivityManager.restartPackage()`; however, it’s already deprecated from API Level 3, suggesting not to use it anymore.

If you know any other ways, feel free to share!

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

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

APK is just a packed ZIP file

July 14, 2011 1 comment

Something many of you might not know is that “an APK file is just a single packed ZIP file“.

APK is a ZIP file

APK is a ZIP file

See that?

One good tip: “You can reduce size of your APK file by removing unnecessary files inside“.

Have fun!



Pete Houston



Categories: Tricks & Tips Tags: , , , , ,

ListView doesn’t respond to OnItemClickListener

July 14, 2011 33 comments

There will be case that your custom list item doesn’t respond when you click…so what’s the reason and what’s the solution?

Here several problems and solutions:

1. Scenario: list item layout contains CheckBox

Problem: OnItemClickListener doesn’t respond.

Reason: CheckBox is also having its own click listener by default to change its state, and it overrides the container ListView.

Solution: remove focus on CheckBox by setting these attributes to “false”


2. Scenario: random

Problem: OnItemClickListener just doesn’t repond any at all!!!!

Reason: No idea..

Solution: in code, just set OnItemClickListener before setting Adapter. It works randomly @@!

3. Scenario: list item contains ImageButton

Problem: OnItemClickListener just doesn’t repond any at all!!!!

Reason: No idea!!!

Solution: in code, set ImageButton focus to “false”

ImageButton button = (ImageButton) convertView.findViewById(;

4. Scenario: list item contain TextView

Problem: OnItemClickListener just doesn’t repond.

Reason: I think you have set this attribute to TextView: android:inputType=”textMultiLine”

Solution: just remove that attribute, using android:minLines/android:maxLines instead.

5. Scenario: list item contain a TextView that is linked to website URL or whatever “mailto:” things

Problem: OnItemClickListener just doesn’t repond.

Reason: the TextView overrides focus of list item.

Solution: just remove attribute “android:autoLink” on TextView.


Hope you solve your problems!


Pete Houston