0% found this document useful (1 vote)
388 views4 pages

Practicalno:-22 1. Write A Program To Changes The Background Color When Device Is Shuffled. Acitivity - Main - XML

This document describes two Android programs. The first program changes the background color of a view when the device is shuffled, detecting acceleration with the accelerometer sensor. The second program displays a list of all sensors supported by the mobile device by getting the sensor list from the SensorManager.

Uploaded by

Monika Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
388 views4 pages

Practicalno:-22 1. Write A Program To Changes The Background Color When Device Is Shuffled. Acitivity - Main - XML

This document describes two Android programs. The first program changes the background color of a view when the device is shuffled, detecting acceleration with the accelerometer sensor. The second program displays a list of all sensors supported by the mobile device by getting the sensor list from the SensorManager.

Uploaded by

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

Practicalno:-22

public class MainActivity extends


1. Write a program to changes the Activity implements
background color when device is SensorEventListener {
shuffled. private SensorManager sensorManager;
private boolean isColor = false;
❖ Acitivity_main.xml private View view;
private long lastUpdate;
<?xml version="1.0" encoding="utf-
8"?> @Override
<RelativeLayout public void onCreate(Bundle
xmlns:android="http://schemas.android savedInstanceState) {
.com/apk/res/android" super.onCreate(savedInstanceState);
xmlns:tools="http://schemas.android.co setContentView(R.layout.activity_main)
m/tools" ;
android:layout_width="match_parent" view = findViewById(R.id.textView);
android:layout_height="match_parent" view.setBackgroundColor(Color.GREE
tools:context=".MainActivity" > N);
sensorManager = (SensorManager)
<TextView getSystemService(SENSOR_SERVICE
android:id="@+id/textView" );
android:layout_width="wrap_content" lastUpdate =
android:layout_height="wrap_content" System.currentTimeMillis();
android:text="color" /> }
</RelativeLayout> @Override
public void onAccuracyChanged(Sensor
❖ MainActivity.java sensor, int accuracy) {
}
package com.example.practical22;
@Override
import android.app.Activity; public void
import android.graphics.Color; onSensorChanged(SensorEvent event) {
import android.hardware.Sensor; if (event.sensor.getType() ==
import android.hardware.SensorEvent; Sensor.TYPE_ACCELEROMETER) {
import getAccelerometer(event);
android.hardware.SensorEventListener; }
import }
android.hardware.SensorManager; private void
import android.os.Bundle; getAccelerometer(SensorEvent event) {
import android.view.View; float[] values = event.values;
import android.widget.Toast; float x = values[0];
 [Type the document title]

float y = values[1];
float z = values[2]; @Override
float accelationSquareRoot = (x * x + y protected void onPause() {
* y + z * z) / super.onPause();
(SensorManager.GRAVITY_EARTH * sensorManager.unregisterListener(this);
SensorManager.GRAVITY_EARTH); }
long actualTime = }
System.currentTimeMillis();
Toast.makeText(getApplicationContext(
), String.valueOf(accelationSquareRoot) ❖ Output
+""+
SensorManager.GRAVITY_EARTH,
Toast.LENGTH_SHORT).show();
if (accelationSquareRoot >= 2) //it
will be executed if you shuffle
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
lastUpdate for next shuffle
if (isColor) {
view.setBackgroundColor(Color.GREE
N);
} else {
view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
}
}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor
.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NO
RMAL);
}

 Page 1
 [Type the document title]

2. Write a program to display the list


of sensors supported by the mobile
device.

❖ Activiy_main.xml

<?xml version="1.0" encoding="utf- AppCompatActivity {


8"?> TextView tv;
<RelativeLayout
xmlns:android="http://schemas.android @Override
.com/apk/res/android" protected void onCreate(Bundle
xmlns:tools="http://schemas.android.co savedInstanceState) {
m/tools" super.onCreate(savedInstanceState);
android:layout_width="match_parent" setContentView(R.layout.activity_main)
android:layout_height="match_parent" ;
tools:context=".MainActivity" tv = findViewById(R.id.tv);
android:padding="10dp"> String sensorInfo = "";
SensorManager sensorManager =
<TextView (SensorManager)
android:id="@+id/tv" getSystemService(SENSOR_SERVICE
android:layout_width="wrap_content" );
android:layout_height="wrap_content" List<Sensor> sensorList =
android:text="Hello World!" /> sensorManager.getSensorList(Sensor.T
</RelativeLayout> YPE_ALL);
for(Sensor s : sensorList) {
sensorInfo += s.getName() + "\n";
❖ MainActivity.java
}
package com.example.practical222; tv.setText(sensorInfo);
}
import android.hardware.Sensor; }
import
android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import
androidx.appcompat.app.AppCompatA
ctivity;
import java.util.List;

public class MainActivity extends

 Page 2
 [Type the document title]

❖ Output

 Page 3

You might also like