Thursday, 18 December 2014

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;
        }
       
    }

Saturday, 1 November 2014

how to create swipe view in android example

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</android.support.v4.view.ViewPager>


swipe_fregmant.xml

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:gravity="center_vertical|center_horizontal"
        android:padding="20dp"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textIsSelectable="false"
        android:textSize="14sp" />

</ScrollView>

 MainActivity.java

package com.swiptesting;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnClickListener{
    static final int NUM_ITEMS = 9;
    PlanetFragmentPagerAdapter planetFragmentPagerAdapter;
    ViewPager viewPager;
    private int positions = 6;
   
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        planetFragmentPagerAdapter = new PlanetFragmentPagerAdapter(getSupportFragmentManager());
        viewPager = (ViewPager)findViewById(R.id.pager);
        viewPager.setAdapter(planetFragmentPagerAdapter);
       
        // Gesture detection
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
               
               
                return gestureDetector.onTouchEvent(event);
            }
        };
        viewPager.setOnTouchListener(gestureListener);
        viewPager.setCurrentItem(positions);
    }

  

    public static class PlanetFragmentPagerAdapter extends FragmentPagerAdapter {
        public PlanetFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            SwipeFragment fragment = new SwipeFragment();
            return fragment.newInstance(position);
        }
    }

    public static class SwipeFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
           
            View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false);
           
            TextView tv = (TextView)swipeView.findViewById(R.id.text);
            ImageView img = (ImageView)swipeView.findViewById(R.id.imageView);
           
           
            Bundle args = getArguments();
            int position = args.getInt("position");
            String planet = Planet.PLANETS[position];
            int imgResId = getResources().getIdentifier(planet, "drawable", "com.swiptesting");
           
            img.setImageResource(imgResId);
            tv.setText(Planet.PLANET_DETAIL.get(planet));
           
            int mposition = 0;
            Bundle margs = getArguments();
            mposition = margs.getInt("position");
                     
           
            return swipeView;
        }

        SwipeFragment newInstance(int position) {
            SwipeFragment swipeFragment = new SwipeFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            swipeFragment.setArguments(args);
            return swipeFragment;
        }
    }
    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;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   
                    Toast.makeText(MainActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                   
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   
                    Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       
    }
}


Planet.java

package com.swiptesting;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Planet {
    public static final String[] PLANETS = { "one", "two", "three", "for_",
            "five", "six", "seven", "eight", "nine" };
    public static final Map<String, String> PLANET_DETAIL;
    static {
        Map<String, String> planets = new HashMap<String, String>();
        planets.put("one", "1" + " 1");
        planets.put("two", "2" + " 2");
        planets.put("three", "3" + "3");
        planets.put("for_", "4" + "4");
        planets.put("five", "5" + " 5");
        planets.put("six", "6" + "6");
        planets.put("seven", "7" + "7");
        planets.put("eight", "8" + " 8");
        planets.put("nine", "9" + " 9");
        PLANET_DETAIL = Collections.unmodifiableMap(planets);
    }
}