Thursday, June 30, 2011

What to do with AndroidManifest.xml ?

30 June 2011
How to prevent onCreate() from being called when the orientation is changed ?
<activity name="YourActivity" android:configChanges="orientation|keyboardHidden" />

16 Aug 2010
How to allow to access internet ?
<uses-permission android:name="android.permission.INTERNET" />

How to disable orientation change ?
android:screenOrientation="nosensor"

Do not show softkeyboard on Activiry showing ?
<activity name="YourActivity" android:windowSoftInputMode="stateHidden" />

Tuesday, June 28, 2011

How to do them in View ?

16 June 2011
How to remove the button shape of an ImageButton ?
android:background="@null"

07 Sept 2010
How to create Full Screen activity ?

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xxx);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

16 Aug 2010
How to know about screen width and height ?

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

How to show an Alert ?

AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage(R.string.message)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}
)
.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}
).create();
dialog.show();

Monday, June 20, 2011

What to do in I/O and File ?

18 June 2011
How to unzip a .zip file ?

public static void unzip(String filePath, InputStream is) {
ZipInputStream in = null;
try {
in = new ZipInputStream(is);
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
if (entry.isDirectory()) {
File file1 = new File(filePath + entry.getName());
file1.mkdir();
} else {
FileOutputStream outputfile = new FileOutputStream(filePath + entry.getName());
int data = 0;
while ((data = in.read()) != -1) {
outputfile.write( data );
}
outputfile.close();
}
}
} catch (IOException e) {
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ignored) {
}
in = null;
}
}

Friday, June 17, 2011

What to do in Storage ?

18 June 2011
How to save and use a pair of key and value in a reference ?

SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", "yourname");
editor.commit();

String url = settings.getString("name", "noname");

What to do with WebView ?

17 June 2011
How to set Database Path for HTML5 in a WebView ? / Why does openDatabase() in HTML5 return null ?
WebView webview = new WebView(this);

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);

String databasePath = getDir("database", Context.MODE_PRIVATE).getPath();
webSettings.setDatabasePath(databasePath);

webview.setWebChromeClient(new WebChromeClient() {

public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2);
}
});

14 June 2011
How to hide the address / url bar ?
webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});

Friday, June 10, 2011

How to deal with the error message ?

10 June 2011
Err: java.lang.IllegalArgumentException: column '_id' does not exist
You has to set a primary key as _id if you are using SimpleCursorAdapter.

Err: No such table android_metadata
Add SQLiteDatabase.NO_LOCALIZED_COLLATORS to SQLiteDatabase.openDatabase().

Tuesday, June 7, 2011

How to deal with Android Market ?

08 June 2011
How to transfer .APK from Androd Market to Computer ?

1. Install Astro.
2. Open Astro and click menu to backup Apps.
3. Check your SDCard >> Open Backups >> apps.