Archive for May, 2012

Make a phone call from Android app

Pass phone number to ‘call’ function, android will make a call

private void call(String number) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + number));
		} catch (ActivityNotFoundException e) {
			Log.e("TAG", "Call failed", e);
Categories: Android Tags:

Send email from android app

private void sendMail(String mailID) {
	Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailID, null));
        _context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Categories: Android Tags:

Call a function in android using string value(Using Java Reflection)

public void reflectionSample(String functionName){
		///this is used to call java methods using string value.		    
		Class c = MyClassName.class;
		///paraTypes: used to pass parameter to java function, its just a declaration.
		Class paraTypes[] = new Class[1];
		///the type of parameter is hash map.
		paraTypes[0] = HashMap.class;
		Method method = null;
		try {
			///class.getDeclaredMethod(java-function-name, java-function-parameter-type)
			///@param1: the string name of java function in class c
			///@param2: the type of parameter, like hashmap, string, int, etc
			method = c.getDeclaredMethod(functionName, paraTypes);
		} catch (Exception e1) {
		HashMap<String, String> argument = new HashMap<String, String>();
		argument.put("functionName", functionName);

		Object arg[] = new Object[1];
		arg[0] = new HashMap(argument);
		try {
			///method.invoke calls the java function
			method.invoke(this, arg);
		} catch (Exception e) {
	public void calledFromReflection(HashMap<String, String> text){


Learn about reflection
Reflection doc

Categories: Android Tags: ,

Android Hybrid app

                ///webview loads the given url.
		///to enable javascript functions.
		///open android's function to javascript.
		mainWebView.addJavascriptInterface(this, "DemoActivity");
		///this is used remove two or five pixed gap on right side on view.

		///to listen javascript's alert function.
		mainWebView.setWebChromeClient(new WebChromeClient(){
			public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
				return super.onJsAlert(view, url, message, result);

		mainWebView.setWebViewClient(new WebViewClient(){
			///listens all external urls.
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);

			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);

			///is called when url not found or any other error while loading the page.
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				Toast.makeText(_context, description, Toast.LENGTH_SHORT).show();

			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				//This is used to ignore ssl error(it will not check ssl certificate and proceeds).


		//to bring focus on web view
		//this enable keyboard on screen 
		mainWebView.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_UP:
					if (!v.hasFocus()) {
				return false;
///to call javascript function.

Javascript file

function showMenu(){

function hideMenu(name){

function changeValue(name){
	document.getElementById("messageView").innerHTML = name;

You can download the source code from

Categories: Android Tags:
%d bloggers like this: