Archive

Posts Tagged ‘Android’

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));
			_context.startActivity(callIntent);
		} catch (ActivityNotFoundException e) {
			Log.e("TAG", "Call failed", e);
		}
	}
Advertisements
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) {
			e1.printStackTrace();
		}
		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) {
			e.printStackTrace();
		}
	}
	
	public void calledFromReflection(HashMap<String, String> text){
		System.out.println("---------------|"+text.get("functionName")+"|-------------");
	}


reflectionSample("calledFromReflection");

Learn about reflection
Reflection doc

Categories: Android Tags: ,

Android Hybrid app

                ///webview loads the given url.
		mainWebView.loadUrl("file:///android_asset/mainView.html");
		///to enable javascript functions.
		mainWebView.getSettings().setJavaScriptEnabled(true);
		///open android's function to javascript.
		mainWebView.addJavascriptInterface(this, "DemoActivity");
		///this is used remove two or five pixed gap on right side on view.
		mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

		///to listen javascript's alert function.
		mainWebView.setWebChromeClient(new WebChromeClient(){
			@Override
			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.
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);
			}

			@Override
			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.
			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				Toast.makeText(_context, description, Toast.LENGTH_SHORT).show();
			}

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

		});

		//to bring focus on web view
		//this enable keyboard on screen 
		mainWebView.requestFocus(View.FOCUS_DOWN);
		mainWebView.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_UP:
					if (!v.hasFocus()) {
						v.requestFocus();
					}
					break;
				}
				return false;
			}
		});
///to call javascript function.
mainWebView.loadUrl("javascript:changeValue('"+value+"')");

Javascript file

function showMenu(){
	DemoActivity.showMenu();
}

function hideMenu(name){
	DemoActivity.hideMenu(name);
}

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

You can download the source code from
http://dl.dropbox.com/u/12044966/Demos.zip

Categories: Android Tags:

Android: Check network connection

January 8, 2011 2 comments

This will help to to detect if your android has any internet connection

This will return true i.e your application has internet connection.


 private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

Enjoy.. 😉 !!!!!!!

Categories: Android Tags:

Android: Check if Airplane mode is on

January 8, 2011 Leave a comment

This will help u check if your android is in airplane/fight mode

static boolean isAirplaneEnabled;
isAirplaneEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;

Enjoy.. 😉 !!!!!!!

Categories: Android Tags: ,

Customize button in Android

November 9, 2010 32 comments


hi!!
You can change the appearance of button in android like make the corners of button rounded etc…

button_shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape      xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
    
    <solid   android:color="#EAEAEA"/>
    
    <corners    android:bottomLeftRadius="8dip"
                android:topRightRadius="8dip"
                android:topLeftRadius="1dip"
                android:bottomRightRadius="1dip"
                />
</shape>

here you can change the radius of corners

you can use this button_shape in your button’s code as

android:background=”@drawable/button_shape”

here’s the code

main.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView   android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Hello Android from NetBeans"/>

    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nishant Nair"
            android:padding="5dip"
            android:layout_gravity="center"
            android:background="@drawable/button_shape"
            />

</LinearLayout>

you can add an image to your button by using drawable

android:drawableTop=”@drawable/pdf”

in your button’s xml code

this will look like this

Enjoy.. 😉 !!!!!!!

Categories: Android Tags: ,
%d bloggers like this: