Hello firends!!! In this tutorial we are going to implement Motion Sensor .
- All we will do in this tutorial is on shaking the device we will navigate from one page to another.
- So follow the Steps :
- Create a new project and for example name it as AppCheck.
- Your MainActivity and activity_main files will be generated respectively.
- Now go to your activity_main.xml file and in your graphical Layout take LargeText , and in the text property write “Shake to get a toast and to switch Image”,and add the following statement in RelativeLayout :
- Hence for this add images in your res-> drawable-hdpi folder
- So now your activity_main.xml file graphical layout will look as shown below :
- The code of activity_main.xml file is given below :
- Now go to your MainActivity.Java file write the following code as explained:
- So now your window will look as shown below :
- Now create another class and name it NextPage2 and also create an android xml file for this class and name it as next2.
- Write the following code in NextPage2.Java :
- Hence the window will look as shown below :
- Now go to your next2.xml file and only set the background image in the layout by adding the following statement:
android:background=”@drawable/your image name” - Hence the code is:
- Your window will look as shown below :
- Now go to your AndroidManifest.xml file and add an activity into it to make the intent successful :
- Now run your app and you will have the following output :
- Now shake your device and you will be navigated to the next page:
- Thus we have successfully implemented sensors.
[code language=”xml”]android:background=”@drawable/your image name”
[/code]
[code language=”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"
tools:context=".MainActivity"
android:background="@drawable/q6(your image name)">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Shake to Open Next Activity"
android:textColor="#800000"
android:textSize="20sp" />
</RelativeLayout>
[/code]
[code language=”java”]public class MainActivity extends Activity implements SensorEventListener {
SensorManager sm;
long lastTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
lastTime = System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO Auto-generated method stub
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event)
{
float[] value = event.values;
float x = value[0];
float y = value[1];
float z = value[2];
float accelationSquareRoot = (x*x + y*y + z*z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if(accelationSquareRoot >= 2)
{
if(actualTime-lastTime < 200)
{
return;
}
lastTime = actualTime;
// Perform your Action Here..
Intent i = new Intent(MainActivity.this, NextPage.class);
startActivity(i);
Toast.makeText(this, "Your Next Activity is successfully called",Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
}
[/code]
[code language=”java”]public class NextPage2 extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next2);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
startActivity(new Intent(NextPage2.this,MainActivity .class));
}
}
[/code]
[code language=”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:orientation="vertical"
android:background="@drawable/q7(your image name)">
</LinearLayout>
[/code]