0% found this document useful (0 votes)
3 views3 pages

Practical 22.1

The document provides code for an Android application that changes the background color of a layout when the device is shaken. It includes XML layout code and Java code that utilizes the accelerometer sensor to detect motion and switch colors between cyan, yellow, and red. The application implements the SensorEventListener interface to handle sensor events and update the UI accordingly.

Uploaded by

ytube79120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Practical 22.1

The document provides code for an Android application that changes the background color of a layout when the device is shaken. It includes XML layout code and Java code that utilizes the accelerometer sensor to detect motion and switch colors between cyan, yellow, and red. The application implements the SensorEventListener interface to handle sensor events and update the UI accordingly.

Uploaded by

ytube79120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 22.

1 WAP to change the background color when device is suffeld


XML Code: import android.widget.Toast;
<?xml version="1.0" encoding="utf-8"?> @SuppressLint("ResourceAsColor")
<RelativeLayout public class MainActivity extends Activity
xmlns:android="http://schemas.android.co implements SensorEventListener {
m/apk/res/android" private SensorManager sensorManager;
private boolean isColor = false;
xmlns:app="http://schemas.android.com/a private RelativeLayout view;
pk/res-auto" private long lastUpdate;
@Override
xmlns:tools="http://schemas.android.com/t public void onCreate(Bundle
ools" savedInstanceState) {
android:layout_width="match_parent" super.onCreate(savedInstanceState);
android:layout_height="match_parent"
android:id="@+id/view" setContentView(R.layout.activity_main);
tools:context=".MainActivity"> view = findViewById(R.id.view);
<TextView view.setBackgroundColor(Color.CYAN);
android:id="@+id/textView" sensorManager= (SensorManager)
android:layout_width="match_parent" getSystemService((SENSOR_SERVICE));
android:layout_height="wrap_content" lastUpdate=
android:gravity="center_horizontal" System.currentTimeMillis();
android:layout_marginTop="60dp" }
android:textSize="30dp" @Override
android:textAllCaps="true" public void onAccuracyChanged(Sensor
android:fontFamily="sans-serif- sensor,int accuracy)
condensed-medium" {
android:textColor="@color/black" }
android:textStyle="bold" @Override
android:text="Shake to switch color" /> public void
</RelativeLayout> onSensorChanged(SensorEvent event){

JAVA Code : if(event.sensor.getType()==Sensor.TYPE_AC


package com.example.prac221; CELEROMETER){
import android.annotation.SuppressLint; getAccelerometer(event);
import android.app.Activity; }
import android.graphics.Color; }
import android.hardware.Sensor; private void
import android.hardware.SensorEvent; getAccelerometer(SensorEvent event){
import float[] values=event.values;
android.hardware.SensorEventListener; float x= values[0];
import android.hardware.SensorManager; float y=values[1];
import android.os.Bundle; float z=values[2];
import android.widget.RelativeLayout;
float
accelationSquareRoot=(x*x+y*y+z*z) view.setBackgroundColor(Color.YELLOW);
}
/(SensorManager.GRAVITY_EARTH*Sensor isColor = !isColor;
Manager.GRAVITY_EARTH); }
long actualTime = }
System.currentTimeMillis(); @Override
protected void onResume()
Toast.makeText(getApplicationContext(),ac {
celationSquareRoot+""+ super.onResume();
sensorManager.registerListener(this,sensor
SensorManager.GRAVITY_EARTH,Toast.LEN Manager.getDefaultSensor(Sensor.TYPE_AC
GTH_SHORT).show(); CELEROMETER),SensorManager.SENSOR_D
if (accelationSquareRoot>= ELAY_NORMAL);
if (actualTime - lastUpdate < 200) { }
return; @Override
} protected void onPause(){
lastUpdate = actualTime; super.onPause();
if (isColor) {
sensorManager.unregisterListener(this);
view.setBackgroundColor(Color.RED); }}
} else {
Output :

You might also like