Wednesday, August 17, 2011

What can be done in Intent ?

17 Aug 2011
How to pass Object from Activity A to B ?

import java.util.*;
import android.os.*;

public class Data implements Parcelable {
public int value = 0;

public Data() {}

private Data(Parcel in) {
value = in.readInt();
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel out, int flags) {
out.writeInt(value);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Data createFromParcel(Parcel in) {
return new Data(in);
}

public Data[] newArray(int size) {
return new Data[size];
}
};
}


Intent intent = new Intent(ActivityA.this, Activity.class);
intent.putExtra("data", data);
startActivityForResult(intent, 0);


Intent i = getIntent();
Data data = (Data) i.getParcelableExtra("data");

20 June 2011
How to choose Email writer and fill in ?
Intent i = new Intent(Intent.ACTION_SEND);

i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, "youremail@domain.com");
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT, "message");
startActivity(Intent.createChooser(i, "Select email application."));


30 Sep 2010
How to open a System Screen ?
startActivityForResult(new Intent(Settings.ACTION_SOUND_SETTINGS), 0);


16 Aug 2010
How to open an URL in a native browser ?
Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse("http://www.google.com/"));
startActivity(intent);

How to make a call ?
String url = "tel:1234567";

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(url)));

How to go to Dial keyword to make a call ?
String url = "tel:1234567";

Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(callIntent);

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.

Friday, January 21, 2011

What to do in debug / development ?

21 Jan 2011
How to install application thur adb ?

In DOS, " adb install application.apk "


18 Aug 2010
How to debug on a real device ?

Settings-Application-Development-USB test = on


16 Aug 2010
How to print screen ?

C:\android-sdk-windows\tools\ddms.bat

Execute 'Galvik Debug Monitor' and select 'Device'-'Screen capture'