Archive

Archive for November, 2011

Lose EditText content when rotating

November 23, 2011 1 comment

One common problem as I’ve seen much is that, the `EditText` loses its content while rotating the phone.

Technically, it doesn’t lose it content, because a new `EditText` or a new `Activity` is created everytime you try to rotate the phone if your application doesn’t handle the orientation change.

Ok, if you don’t know about this, please refer to one of my old article discussing about the orientation problems.

For current situation, this is the solution to keep the text on `EditText`:

public static final String SAVED_TEXT_KEY = "SavedText";

protected void onCreate( Bundle savedInstanceState ) {
		// back to normal
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

		// ok we back, load the saved text
        if ( savedInstanceState != null ) {
            String savedText = savedInstanceState.getString( SAVED_TEXT_KEY );
            edtMyText.setText( savedText );
        }
}

public void onSaveInstanceState( Bundle savedInstanceState ) {
	// now, save the text if something overlaps this Activity
    savedInstanceState.putString( SAVED_TEXT_KEY, edtMyText.getText().toString() );
}

Cheers,

Pete Houston

Advertisements
Categories: Tricks & Tips Tags: , , ,

Clear EditText content on focus

November 23, 2011 Leave a comment

Feel annoying when everytime you input, you need to delete every word in `EditText`, here the tip:

EditText txtData = (EditText) findViewById( R.id.txtData );
txtData.setOnFocusChangeListener( new View.OnFocusChangeListener() {

	public void onFocusChange( View v, boolean hasFocus ) {
		if( hasFocus ) {
			txtData.setText( "", TextView.BufferType.EDITABLE );
		}
	}

} );

Cheers,
Pete Houston

Great once-a-year birthday

November 17, 2011 Leave a comment

Having so much fun at work today since it’s my birthday, well I really found some of my colleagues are really interesting. Also I did enjoy the “Thank-You Teacher” party of the company, thanks everybody so much for all of your wishes for me.

Love you all ❤

Categories: Of Diary Tags: ,

Android 4.0 source released!

November 16, 2011 3 comments

oh oh…Android 4.0 source code has been released!

And yes, lots of work to do for now 🙂

http://source.android.com/source/downloading.html

>> From : http://source.android.com/

News

Source Code Available for Android 4.0

The source code for the Android 4.0 platform and software stack has been released! This release allows OEMs to begin preparing Android 4.0 for installation on new and existing devices, and allows hobbyists, enthusiasts, and researchers to develop custom builds. For information on how to obtain the software, visit our Getting the Source page.

 

 

Categories: Of Diary Tags: , ,

Convert Bitmap to byte array and reverse

November 13, 2011 14 comments

If you want to convert a `Bitmap` to byte array:

public byte[] getByteArray(Bitmap bitmap) {
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	bitmap.compress(CompressFormat.PNG, 0, bos);
	return bos.toByteArray();
}

and if you want the reverse process:

public Bitmap getBitmap(byte[] bitmap) {
	return BitmapFactory.decodeByteArray(bitmap , 0, bitmap.length);
}

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , ,

Calculate memory usage at runtime

November 13, 2011 Leave a comment

If you’re wondering how to calculate memory usage, this tip is what you need

// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memoryUsed = runtime.totalMemory() - runtime.freeMemory();

Cheers,
Pete Houston

Make Your First PhoneGap Application

November 5, 2011 1 comment

PhoneGap implementation for Google Android
==========================================

Install SDK + PhoneGap
———————-
1. Download and install Eclipse Classic
2. Download and install Android SDK
3. Download and install ADT Plugin
4. Download the latest copy of PhoneGap and extract its contents. We will be working with the Android directory.

Setup New Project
—————–
1. Launch Eclipse, then under the File menu select New > Android Project
2. In the root directory of the project, create two new directories:
– /libs
– /assets/www
3. Copy phonegap-1.1.0.js from your PhoneGap download earlier to /assets/www
4. Copy phonegap-1.1.0.jar from your PhoneGap download earlier to /libs
5. Copy xml folder from your PhoneGap download earlier to /res
6. Make a few adjustments too the project’s main Java file found in the src folder in Eclipse:
– Change the class’s extend from Activity to DroidGap
– Replace the setContentView() line with super.loadUrl(“file:///android_asset/www/index.html”);
– Add import com.phonegap.*;
– Remove import android.app.Activity;
You might experience an error here, where Eclipse can’t find phonegap-1.1.0.jar.
In this case, right click on the /libs folder and go to Build Paths/ > Configure Build Paths.
Then, in the Libraries tab, add phonegap-1.1.0.jar to the Project.
If Eclipse is being temperamental, you might need to refresh (F5) the project once again.

7. Right click on AndroidManifest.xml and select Open With > Text Editor
8. Paste the following permissions under versionName:

<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

9. Add android:configChanges=”orientation|keyboardHidden” to the activity tag in AndroidManifest.

10. Now create and open a new file named index.html in the /assets/www directory. Paste the following code:

<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

11.  A. Deploy to Simulator
– Right click the project and go to Run As and click Android Application
– Eclipse will ask you to select an appropriate AVD. If there isn’t one, then you’ll need to create it.

11.  B. Deploy to Device
– Make sure USB debugging is enabled on your device and plug it into your system. (Settings > Applications > Development)
– Right click the project and go to Run As and click Android Application

11. Done!
– You can also checkout more detailed version of this guide http://PhoneGap.com/start

That’s the guideline from README.rm in PhoneGap package for Android.

Cheers,

Pete Houston