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);
}
}
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..."));
}
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
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
On off switcher android
Its bit complicated but good to use.
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FACC2E"> <RelativeLayout android:id="@+id/onoffContainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerHorizontal="true"> <ViewFlipper android:id="@+id/onoffSwitchContainer" android:layout_width="wrap_content" android:layout_height="25dip" android:layout_marginTop="10dip"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background_on_off"> <Button android:id="@+id/onButton" style="@style/disable_button" android:text="ON" /> <Button style="@style/enable_button" android:text="OFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background_on_off"> <Button style="@style/enable_button" android:text="ON" /> <Button android:id="@+id/offButton" style="@style/disable_button" android:text="OFF" /> </LinearLayout> </ViewFlipper> </RelativeLayout> <RelativeLayout android:id="@+id/onoffContainer2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/onoffContainer"> <ViewFlipper android:id="@+id/onoffSwitchContainer2" android:layout_width="wrap_content" android:layout_height="25dip" android:layout_marginTop="10dip" android:background="@drawable/background_on_off1"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/onButton2" style="@style/disable_button2" /> <Button style="@style/enable_button2" android:text="OFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background_on1"> <Button style="@style/enable_button2" android:text="ON" /> <Button android:id="@+id/offButton2" style="@style/disable_button2"/> </LinearLayout> </ViewFlipper> </RelativeLayout> </RelativeLayout>
activity
package org.nishant.nair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ViewFlipper;
public class OnOffSwittcherActivity extends Activity {
/** Called when the activity is first created. */
private ViewFlipper viewflip;
boolean fromRight = false;
boolean fromLeft = true;
private Button onButton;
private Button offButton;
/////////////////////////////////////////////////////
/////
boolean fromRightImage = false;
boolean fromLeftImage = true;
private ViewFlipper viewflip2;
private Button onButton2;
private Button offButton2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewflip = (ViewFlipper) findViewById(R.id.onoffSwitchContainer);
onButton = (Button) findViewById(R.id.onButton);
offButton = (Button) findViewById(R.id.offButton);
viewflip2 = (ViewFlipper) findViewById(R.id.onoffSwitchContainer2);
onButton2 = (Button) findViewById(R.id.onButton2);
offButton2 = (Button) findViewById(R.id.offButton2);
/*
* boolean values is used to control view to go left and right.
* viewflip.showNext() is used to show next view in viewflipper.
* viewflip.showPrevious() is used to show previous view in viewflipper.
*/
onButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// /when user clicks on ON button, ON view slides in
if (fromLeft == true) {
viewflip.setInAnimation(AnimationHelper.inFromLeftAnimation());
viewflip.setOutAnimation(AnimationHelper.outToRightAnimation());
viewflip.showNext();
fromLeft = false;
fromRight = true;
}
}
});
offButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// /when user clicks on OFF button, OFF view slides in
if (fromRight == true) {
viewflip.setInAnimation(AnimationHelper.inFromRightAnimation());
viewflip.setOutAnimation(AnimationHelper.outToLeftAnimation());
viewflip.showPrevious();
fromLeft = true;
fromRight = false;
}
}
});
onButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// /when user clicks on ON button, ON view slides in
if (fromLeftImage == true) {
viewflip2.setInAnimation(AnimationHelper.inFromLeftAnimation());
viewflip2.setOutAnimation(AnimationHelper.outToRightAnimation());
viewflip2.showNext();
fromLeftImage = false;
fromRightImage = true;
}
}
});
offButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// /when user clicks on OFF button, OFF view slides in
if (fromRightImage == true) {
viewflip2.setInAnimation(AnimationHelper.inFromRightAnimation());
viewflip2.setOutAnimation(AnimationHelper.outToLeftAnimation());
viewflip2.showPrevious();
fromLeftImage = true;
fromRightImage = false;
}
}
});
}
}
AnimationHelper class
package org.nishant.nair;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
public class AnimationHelper {
public static Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(200);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(200);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
// for the next movement
public static Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(200);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
public static Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(200);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
}
stlyes.xml (res/values/styles.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="enable_button"> <item name="android:layout_width">40dip</item> <item name="android:layout_height">25dip</item> <item name="android:textColor">#f8f8f8</item> <item name="android:textStyle">bold</item> <item name="android:background">@drawable/background_enable_switch_button</item> </style> <style name="disable_button"> <item name="android:layout_width">40dip</item> <item name="android:layout_height">25dip</item> <item name="android:textColor">#CCCCCC</item> <item name="android:textStyle">bold</item> <item name="android:background">@drawable/background_disable_switch_button</item> </style> <style name="enable_button2"> <item name="android:layout_width">40dip</item> <item name="android:layout_height">25dip</item> <item name="android:textColor">#A4A4A4</item> <item name="android:textStyle">bold</item> <item name="android:background">@null</item> </style> <style name="disable_button2"> <item name="android:layout_width">25dip</item> <item name="android:layout_height">25dip</item> <item name="android:background">@drawable/background_disable_switch_button1</item> </style> </resources>
background_disable_switch_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#e8e8e8"/>
<corners android:radius="3dip"/>
</shape>
background_disable_switch_button1.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#BDBDBD"/>
<corners android:radius="20dip"/>
<stroke android:width="0.5dp" android:color="#848484"/>
</shape>
background_enable_switch_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#303030"/>
<corners android:radius="4dip"/>
</shape>
background_enable_switch_button1.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#303030"/>
<corners android:radius="4dip"/>
</shape>
background_on_off.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" shape="rectangle"> <corners android:radius="5dip" /> </shape>
background_on_off1.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" shape="rectangle"> <corners android:radius="20dip"/> <solid android:color="#eeeeee"/> </shape>
background_on1.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" shape="rectangle"> <corners android:radius="20dip" /> <solid android:color="#205691"/> </shape>
Sliding Panel in IPad like twitter
Hi! folks,
Have you used twitter app in IPAD ?

it has a awesome sliding panel, if you want try something like that then here is the link
it supports :
Orientation (both Landscape & Portrait)
Swipe Gestures
Bouncing Effect
this is develop by my two colleagues Reefaq and Nikhil.
another link blog click here .
Enjoy …
Android: Check network connection
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..
!!!!!!!




