Thursday, 3 April 2014

motion, proximity sensor function in android

Motion sensors are useful for monitoring device movement, such as tilt, shake, rotation ,swing. Motion sensors have x,y,z axis which changes as per the movement of smartphone.

MainActivity.java
 public class MainActivity extends Activity implements SensorEventListener {  
      int sx,sy;  
      TextView t1;  
      TextView t2;  
      TextView t3;  
      TextView t4;  
      int count;  
      int a;  
      private SensorManager sensorMan;  
      private Sensor accelerometer;  
      Sensor proximity ;  
      private float[] mGravity;  
      @SuppressWarnings({ "deprecation"})  
      @SuppressLint("NewApi")  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           //getting the screen size   
     Display display = getWindowManager().getDefaultDisplay();  
                sx = display.getWidth();  
                sy = display.getHeight();;  
                t1=(TextView) findViewById(R.id.textView1);  
                t2=(TextView) findViewById(R.id.textView2);  
                t3=(TextView) findViewById(R.id.textView6);  
                sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);  
                accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
                proximity = sensorMan.getDefaultSensor(Sensor.TYPE_PROXIMITY);  
                t2.setText("x= "+sx+" y= "+sy);  
      }  
       @Override  
       public void onResume()   
       {  
         super.onResume();  
         sensorMan.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_UI);  
         sensorMan.registerListener(this, proximity,SensorManager.SENSOR_DELAY_UI);  
       }  
       @Override  
       protected void onPause()   
       {  
         super.onPause();  
         sensorMan.unregisterListener(this);  
       }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onAccuracyChanged(Sensor arg0, int arg1) {  
           // TODO Auto-generated method stub  
      }  
       @Override  
       public void onSensorChanged(SensorEvent event)   
       {  
         mGravity = event.values;  
           // Shake detection  
           float x = mGravity[0];  
           float y = mGravity[1];  
           float z = mGravity[2];  
           t1.setText("\nx="+x+"\ny="+y+"\nz="+z);  
           if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)  
              {  
                t3.setText(""+event.values[0]);  
                // t3.setText(String.valueOf(event.values[0]));  
              }  
       }  
 }  

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView2"  
     android:layout_alignParentTop="true"  
     android:layout_marginTop="59dp"  
     android:text="@string/t1"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView3"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:text="@string/large_text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView2"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginBottom="138dp"  
     android:text="@string/t2"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView4"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView3"  
     android:layout_centerVertical="true"  
     android:text="@string/ss"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView5"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_alignParentLeft="true"  
     android:layout_marginBottom="32dp"  
     android:text="@string/proximity"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView6"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignBaseline="@+id/textView5"  
     android:layout_alignBottom="@+id/textView5"  
     android:layout_toRightOf="@+id/textView4"  
     android:text="@string/t3"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>  

Tuesday, 11 February 2014

simple calculator in android code

For creating calculator add  TextView and numaric Button into "activity_main.xml" file.
create a android apps with the name "calc".

1.activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:gravity="right"
        android:text="@string/large_fix"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button01"
        android:layout_alignBottom="@+id/Button01"
        android:layout_toRightOf="@+id/Button01"
        android:text="@string/fix2"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button02"
        android:layout_alignBottom="@+id/Button02"
        android:layout_toRightOf="@+id/Button02"
        android:text="@string/fix3"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button03"
        android:layout_alignBottom="@+id/Button03"
        android:layout_toRightOf="@+id/Button03"
        android:text="@string/plus"
        android:onClick="clicked"/>

    <Button
        android:id="@+id/Button05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button01"
        android:layout_centerVertical="true"
        android:text="@string/fix4"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/Button03"
        android:text="@string/fix5"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/Button04"
        android:text="@string/fix6"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button08"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button07"
        android:layout_alignBottom="@+id/Button07"
        android:layout_alignLeft="@+id/Button04"
        android:text="@string/minus"
        android:onClick="clicked"/>

    <Button
        android:id="@+id/Button09"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button05"
        android:layout_toLeftOf="@+id/Button06"
        android:text="@string/fix7"
        android:onClick="click" />

    <Button
        android:id="@+id/Button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button09"
        android:layout_alignBottom="@+id/Button09"
        android:layout_toLeftOf="@+id/Button07"
        android:text="@string/fix8"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button07"
        android:layout_below="@+id/Button07"
        android:text="@string/fix9"
        android:onClick="click"/>

    <Button
        android:id="@+id/Button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button11"
        android:layout_alignBottom="@+id/Button11"
        android:layout_toRightOf="@+id/Button11"
        android:text="@string/mul"
        android:onClick="clicked" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Button05"
        android:layout_alignLeft="@+id/textView1"
        android:text="@string/fix1"
        android:onClick="click" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button10"
        android:layout_toLeftOf="@+id/Button11"
        android:text="@string/zero"
        android:onClick="click"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button11"
        android:layout_toRightOf="@+id/Button11"
        android:text="@string/div"
        android:onClick="clicked"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button10"
        android:layout_toLeftOf="@+id/Button12"
        android:text="@string/cfix"
        android:onClick="clear" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button09"
        android:layout_toLeftOf="@+id/Button10"
        android:text="@string/dot"
        android:onClick="click"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button3"
        android:layout_marginBottom="33dp"
        android:text="@string/large_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


MainActivity.java

package com.example.calc;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView display;
TextView t2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display =(TextView) findViewById(R.id.textView1);
        t2 =(TextView) findViewById(R.id.textView2);
        display.setText("0");
       
    }
   
    static Boolean isempty=true;
    public void click(View sender)
    {
    Button bt=(Button)sender;
   
    if(isempty)
    {
    display.setText(bt.getText());
    isempty=false;
    }
    else
    {
    display.append(bt.getText());
    }
   
    }
    public void clear(View sender)
    {
    display.setText("0");
    t2.setText("");
    }
   
    static float result=0;
    static int opration=0;
   
    public void clicked(View sender)
    {
    Button bt=(Button)sender;
   
    switch(opration)
    {
    case 0:
    result+=Float.parseFloat(display.getText().toString());
    break;
    case 1:
    result-=Float.parseFloat(display.getText().toString());
        break;
    case 2:
    result*=Float.parseFloat(display.getText().toString());
        break;
    case 3:
    result/=Float.parseFloat(display.getText().toString());
        break;
    }
    display.setText(Float.toString(result));
   
    if(bt.getText().equals("+")) opration=0;
    if(bt.getText().equals("-")) opration=1;
    if(bt.getText().equals("*")) opration=2;
    if(bt.getText().equals("/")) opration=3;
    isempty=true;
    }
 
}