Thursday, 18 December 2014

simple call Intent in android


Intent callIntent = new Intent(Intent.ACTION_CALL);                    callIntent.setData(Uri.parse("tel:"+"1234567890");
startActivity(callIntent);

simple email Intent in android



Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL,new String[] { test@gmail.com });
                email.putExtra(Intent.EXTRA_SUBJECT, "subject");
                email.putExtra(Intent.EXTRA_TEXT, "message");
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email,"Choose an Email client :"));

Skype Intent in android


initiateSkypeUri(context, " ");

//****************************************************************
public void initiateSkypeUri(Context myContext, String mySkypeUri) {

          // Make sure the Skype for Android client is installed.
          if (!isSkypeClientInstalled(myContext)) {
            goToMarket(myContext);
             

          return;
          }

          // Create the Intent from our Skype URI.
          Uri skypeUri = Uri.parse(mySkypeUri);
          Intent myIntent = new Intent(Intent.ACTION_VIEW, skypeUri);

          // Restrict the Intent to being handled by the Skype for Android client only.
          myIntent.setComponent(new ComponentName("com.skype.raider", "com.skype.raider.Main"));
          myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

          // Initiate the Intent. It should never fail because you've already established the
          // presence of its handler (although there is an extremely minute window where that
          // handler can go away).
          myContext.startActivity(myIntent);

          return;
        }


//***********************************************************************
//Determine whether the Skype for Android client is installed on this device.
public boolean isSkypeClientInstalled(Context myContext) {
          PackageManager myPackageMgr = myContext.getPackageManager();
          try {
            myPackageMgr.getPackageInfo("com.skype.raider", PackageManager.GET_ACTIVITIES);
          }
          catch (PackageManager.NameNotFoundException e) {
            return (false);
          }
          return (true);
        }

//**********************************************************************
//Install the Skype client through the market: URI scheme.
        public void goToMarket(Context myContext) {
          Uri marketUri = Uri.parse("market://details?id=com.skype.raider");
          Intent myIntent = new Intent(Intent.ACTION_VIEW, marketUri);
          myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          myContext.startActivity(myIntent);
   
          return;
        }

Saturday, 22 November 2014

how to create simple alert dialog in android


AlertDialog

// Internet Connection Error alertDialog
            final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
            alertDialog.setTitle("Internet Connection Error");
            alertDialog.setMessage("Please connect to working Internet connection");
            alertDialog.setIcon(R.drawable.ic_alert);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {   
                    alertDialog.dismiss();
                    MainActivity.this.finish();
                }
            });
            alertDialog.show();

how to use simple spinner in android example


main_activity.xml

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_prompt" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private Spinner spinner1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        List<String> list = new ArrayList<String>();
        list.add("aaaaa");
        list.add("bbbbb");
        list.add("ccccc");
        list.add("ddddd");
        list.add("eeeee");
        
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                     (this, android.R.layout.simple_spinner_item,list);
                     
        dataAdapter.setDropDownViewResource
                     (android.R.layout.simple_spinner_dropdown_item);
                     
        spinner1.setAdapter(dataAdapter);
         }

    }

Thursday, 13 November 2014

how to paly youtube video for webview in andriod example

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


MainActivity.java


public class MainActivity extends Activity {

WebView mWebView;

       @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

 mWebView = (WebView) findViewById(R.id.webView1);

/** unfortunately, we have to check sdk version ***/
        if (Build.VERSION.SDK_INT < 8) {
            mWebView.getSettings().setPluginsEnabled(true);
        } else {
            mWebView.getSettings().setPluginState(PluginState.ON);
        }
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setUserAgent(0);
        mWebView.setWebChromeClient(new WebChromeClient() {
        });


        /**
         * <iframe id="ytplayer" type="text/html" width="640" height="360"
         * src="https://www.youtube.com/embed/WM5HccvYYQg" frameborder="0"
         * allowfullscreen>
         **/


 String html = "<iframe class=\"youtube-player\" "
         + "style=\"border: 0; width: 100%; height: 95%;"
         + "padding:0px; margin:0px\" "
         + "id=\"ytplayer\" type=\"text/html\" "
         + "src=\"//www.youtube.com/embed/" + videoId
         + "?fs=1\" frameborder=\"0\" " + "allowfullscreen autobuffer "
         + "controls onclick=\"this.play()\">\n" + "</iframe>\n";


        final String mimeType = "text/html";
        final String encoding = "UTF-8";                                  

        mWebView.loadDataWithBaseURL("", html, mimeType, encoding, "");

   }
}

AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" />

 <application
        android:allowBackup="true"

        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

</application>



how to use for basic gesture detection in andriod

Scroll Up & Down Event

//
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

   // Gesture detection
   gestureDetector = new GestureDetector(this, new MyGestureDetector());
   gestureListener = new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {
               return gestureDetector.onTouchEvent(event);
            }
        };


        view.setOnTouchListener(gestureListener);
        view.setOnClickListener(this);


//Scroll Up & Down Event
  class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
        try {
             if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)

                    return false;
                if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {

                    Toast.makeText(MainActivity.this, "UP EVENT",Toast.LENGTH_SHORT).show();
                   
                } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {

                    Toast.makeText(MainActivity.this, "DOWN EVENT",Toast.LENGTH_SHORT).show();
                  
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
       
    }