Sunday, 15 September 2013

Date & Time Picker demo program

com.demo

//please import required files

public class DatePicker extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener()
        {
         public void onClick(View v)
         {
             DatePickerDialog dp=new DatePickerDialog(DatePicker.this,new OnDateSetListener()
             {
                  
                public void onDateSet(android.widget.DatePicker view,
                        int year, int monthOfYear, int dayOfMonth)
                {
                Toast.makeText(DatePicker.this,"Month is"+monthOfYear+"Day is"+dayOfMonth,1000).show();                }
                }, 2013, 7,24);
             dp.show();
            }
        });
        Button bt=(Button) findViewById(R.id.button2);
        bt.setOnClickListener(new OnClickListener()
        {
        public void onClick(View v)
        {
            TimePickerDialog tp=new TimePickerDialog(DatePicker.this,new OnTimeSetListener()
            {
                  
                public void onTimeSet(TimePicker view, int hourOfDay, int minute)
                {
                    Toast.makeText(DatePicker.this,"Hour is"+hourOfDay+"Minute is"+minute,1000).show();          
                }
            },2 ,15 , false);
            tp.show();
        }            
        });               
    }
}

BindService Demo activity

package com.demo;

public class BindServiceDemoact extends Activity
{
    boolean mBounded;
    Myser  mServer;
  
    TextView text;
    Button Bon;
  
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        text=(TextView) findViewById(R.id.textView1);       
        Bon =(Button) findViewById(R.id.button1);
       
    Intent mIntent=new Intent(BindServiceDemoact.this,Myser.class);
    bindService(mIntent,mConnection,BIND_AUTO_CREATE);
       
        Bon.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                text.setText(mServer.getTime());
            }
        });
    }
    ServiceConnection mConnection=new ServiceConnection()
    {
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "My Service is Connected",1000);
            mBounded=true;
            LocalBinder mLB=(LocalBinder)service;
            mServer=mLB.getServerRequest();
        }

        public void onServiceDisconnected(ComponentName name)
        {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "My Service is Connected",1000);
            mBounded=false;
            mServer=null;
        }
    };
   
    protected void onStop()
    {
        super.onStop();
        if(mBounded)
        {
            unbindService(mConnection);
            mBounded=false;
        }
    }
}




package com.demo;

public class Myser extends Service
{
    public class LocalBinder extends Binder
    {
        public Myser getServerRequest()
        {
            return Myser.this;
        }
    }
  
    @Override
    public IBinder onBind(Intent intent)
    {
        // TODO Auto-generated method stub
        return new LocalBinder();
    }
  
    public String getTime()
    {
        // TODO Auto-generated method stub
        SimpleDateFormat mdate=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        return mdate.format(new Date());
    }

}

Myservice Demo activity

package com.demo;

public class MyserviceDemoact extends Activity {
    /** Called when the activity is first created. */
  
    Button Bstart,Bstop;
    Intent i;
  
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        Bstart =(Button) findViewById(R.id.button1);
        Bstop =(Button) findViewById(R.id.button2);
      
        i=new Intent(MyserviceDemoact.this,MySer.class);
      
        Bstart.setOnClickListener(new OnClickListener()
        {
          
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                startService(i);
            }
        });
      
        Bstop.setOnClickListener(new OnClickListener()
        {
          
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopService(i);
            }
        });
      
    }
}

AlertBox Demo activity

package com.demo;

public class AlertBoxDemo extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        AlertDialog.Builder build= new AlertDialog.Builder(this);
        build.setTitle("Select...");
        build.setMessage("Do you Wanna Quit ??");
        build.setIcon(R.drawable.icon);
       
        build.setPositiveButton("YES",new OnClickListener()
        {
          
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(),"You have clicked quit",1000).show();
            }
        });
        build.setNegativeButton("NO",new OnClickListener()
        {
          
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(),"You have not clicked quit", 1000).show();
            }
        });
        build.setNeutralButton("CANCEL",new OnClickListener()
        {

            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(),"You have canceled", 1000).show();
            }
        });
        build.show();
    }
}

Music Player demo program

package com.demo;

public class MySer extends Service
{
    MediaPlayer player;
    public void onCreate()
{
        super.onCreate();
        Toast.makeText(this,"My service created",Toast.LENGTH_LONG);
      
        player=MediaPlayer.create(this, R.raw.aaa);
      
    }
  
    @Override
    public void onStart(Intent intent, int startId)
    {        
        super.onStart(intent, startId);
        Toast.makeText(this,"My service Started",Toast.LENGTH_LONG);
        player.start();
    }
  
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(this,"My service stopped",Toast.LENGTH_LONG);
        player.stop();      
    }
  
    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }   
}

Graphics View & Animation Program

//main activity

package com.demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class Graphicsact extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new GraphicsView(this));
    }

    static public class GraphicsView extends View
    {
        private static final String QUOTE="Now is the time Drop the world"+"good men to come to the aid of their country";

        private final Path circle;
        private final Paint cpaint;
        private final Paint tpaint;
        private Animation anim;
      
        public GraphicsView(Context context)
        {
            super(context);
            circle = new Path();
            circle.addCircle(150,150,100,Direction.CW);
            cpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            cpaint.setStyle(Paint.Style.STROKE);
            cpaint.setColor(Color.GRAY);
            cpaint.setStrokeWidth(3);
          
            tpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            tpaint.setStyle(Paint.Style.FILL_AND_STROKE);
            tpaint.setColor(Color.BLACK);
            tpaint.setTextSize(20f);
            setBackgroundResource(R.drawable.ppp);  //ppp is an image stored in drawable
        }
      
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
          
            if(anim==null)
            {
                createAnim(canvas);
            }
            canvas.drawPath(circle,cpaint);
            canvas.drawTextOnPath(QUOTE,circle,20,0,tpaint);
        }
      
        private void createAnim(Canvas canvas)
        {
            anim = new RotateAnimation(0,360,canvas.getWidth()/2,canvas.getHeight()/2);
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(10000L);
            anim.setInterpolator(new AccelerateDecelerateInterpolator());
          
            startAnimation(anim);
        }
    }
}

//main.xml fileCoding
<?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="@string/hello"
    />
</LinearLayout>


//manifest file Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Graphicsact"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

SurfaceView Demo activity

//Main Activity

package com.demo;

import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewDemo extends Activity implements SurfaceHolder.Callback
{
    Camera c;
    SurfaceView sv;
 
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        sv=(SurfaceView) findViewById(R.id.surfaceView1);
        sv.getHolder().addCallback(this);
        sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
     
        c = Camera.open();
    }

    public void onPause()
    {
        super.onPause();
        c.stopPreview();
    }
 
    public void onDestroy()
    {
        super.onDestroy();
        c.release();
    }
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height)
    {
        Camera.Parameters params = c.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        Camera.Size selected = sizes.get(0);
        params.setPreviewSize(selected.width,selected.height);
        c.setDisplayOrientation(90);
     
        c.startPreview();     
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        try
        {
            c.setPreviewDisplay(sv.getHolder());
        }catch(Exception e)
        {
            e.printStackTrace();
        }     
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.i("PREVIEW","surfaceDestroyed");     
    }
}


//main.xml file Coding
<?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="@string/hello"
    />
<SurfaceView android:layout_height="wrap_content" android:id="@+id/surfaceView1" android:layout_width="wrap_content"></SurfaceView>
</LinearLayout>


//manifest file Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SurfaceViewDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

//also add the user permission for Camera Support

Frame Animation demo program

 //Main Activity Program

package com.demo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class FrameAnimationDemo extends Activity
{
    AnimationDrawable animation;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        ImageView image=(ImageView) findViewById(R.id.image);
        image.setBackgroundResource(R.drawable.frameanim);
        animation = (AnimationDrawable) image.getBackground();
     
        final Button bstart=(Button) findViewById(R.id.btnstart);
        bstart.setOnClickListener(new View.OnClickListener()
        {     
            public void onClick(View v)
            {
                animation.start();
            }
        });
     
        final Button bstop=(Button) findViewById(R.id.btnstop);
        bstop.setOnClickListener(new View.OnClickListener()
        {     
            public void onClick(View v)
            {
                animation.stop();
            }
        });      
    }
}

// main.xml coding
 <?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="@string/hello"
    />
<Button android:text="Start" android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Stop" android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ImageView android:layout_height="wrap_content" android:id="@+id/image" android:layout_width="wrap_content"></ImageView>
</LinearLayout>


// frameanil.xml coding (this must be in drawable folder along with animation images)






<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/a" android:duration="100"/>
    <item android:drawable="@drawable/b" android:duration="100"/>
    <item android:drawable="@drawable/c" android:duration="100"/>
    <item android:drawable="@drawable/d" android:duration="100"/>
    <item android:drawable="@drawable/e" android:duration="100"/>
    <item android:drawable="@drawable/f" android:duration="100"/>
    <item android:drawable="@drawable/g" android:duration="100"/>
    <item android:drawable="@drawable/h" android:duration="100"/>
    <item android:drawable="@drawable/i" android:duration="100"/>
    <item android:drawable="@drawable/j" android:duration="100"/>
</animation-list>
    

//a,b,c,d,e,f,g,h,i,j are the images stored in drawable folder
//Manifest File Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FrameAnimationDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Surface View Demo Activity

//Main Activity

package com.demo;

import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewDemo extends Activity implements SurfaceHolder.Callback
{
    Camera c;
    SurfaceView sv;
  
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        sv=(SurfaceView) findViewById(R.id.surfaceView1);
        sv.getHolder().addCallback(this);
        sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
      
        c = Camera.open();
    }

    public void onPause()
    {
        super.onPause();
        c.stopPreview();
    }
  
    public void onDestroy()
    {
        super.onDestroy();
        c.release();
    }
  
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height)
    {
        Camera.Parameters params = c.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        Camera.Size selected = sizes.get(0);
        params.setPreviewSize(selected.width,selected.height);
        c.setDisplayOrientation(90);
      
        c.startPreview();      
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        try
        {
            c.setPreviewDisplay(sv.getHolder());
        }catch(Exception e)
        {
            e.printStackTrace();
        }      
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.i("PREVIEW","surfaceDestroyed");      
    }
}


//main.xml file Coding
<?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="@string/hello"
    />
<SurfaceView android:layout_height="wrap_content" android:id="@+id/surfaceView1" android:layout_width="wrap_content"></SurfaceView>
</LinearLayout>


//manifest file Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SurfaceViewDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

//also add the user permission for Camera Support

Map demo Activity for getting current Lcation

//Main Activity Coding

package com.demo;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MapdemoActivity extends Activity

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll=new MyLocationListner();
     
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,ll);     
    };
     
    class MyLocationListner implements LocationListener
    {
        public void onLocationChanged(Location location)
        {
            String latlog="My current location is: " +"\n"+ "Latitude= "+location.getLatitude()+"\n"+"Longitude= "+location.getLongitude();
            Toast.makeText(getApplicationContext(),latlog,Toast.LENGTH_LONG).show();
        }
     
        public void onProviderDisabled(String provider)
        {
            Toast.makeText(getApplicationContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();
        }
     
        public void onProviderEnabled(String provider)
        {
            Toast.makeText(getApplicationContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();
        }
     
        public void onStatusChanged(String provider, int status, Bundle extras)
        {         
        }
    } 
}
//dont change in main.xml file
//Create AVD of Google APIs (app level 10)

//Manifest file coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MapdemoActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>