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

Practical 22

The document contains code snippets to change the background color of a layout when the device is shaken and to display the list of sensors supported by a mobile device. The first part includes XML and Java code to change the background color from cyan to magenta when the device is shaken. The second part includes XML and Java code to get the list of all sensors on the device and display them in a text view.

Uploaded by

Nisha Parchande
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 (0 votes)
8 views3 pages

Practical 22

The document contains code snippets to change the background color of a layout when the device is shaken and to display the list of sensors supported by a mobile device. The first part includes XML and Java code to change the background color from cyan to magenta when the device is shaken. The second part includes XML and Java code to get the list of all sensors on the device and display them in a text view.

Uploaded by

Nisha Parchande
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/ 3

Practical 22

Q1) WAP to change the background color when device is suffled.


XML CODE:-

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="60dp"
android:textSize="30dp"
android:textAllCaps="true"
android:fontFamily="sans-serif-condensed-medium"
android:textColor="@color/white"
android:textStyle="bold"
android:text="Shake to switch color" />
</RelativeLayout>

JAVA CODE:-
package com.example.prac22;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.Toast;
@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private boolean isColor = false;
private RelativeLayout view;
private long lastUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.view);
view.setBackgroundColor(Color.CYAN);
sensorManager= (SensorManager) getSystemService((SENSOR_SERVICE));
lastUpdate= System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor sensor,int accuracy)
{

}
@Override
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event){
float[] values=event.values;
float x= values[0];
float y=values[1];
float z=values[2];
float accelationSquareRoot=(x*x+y*y+z*z)
/(SensorManager.GRAVITY_EARTH*SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
Toast.makeText(getApplicationContext(),accelationSquareRoot+""+
SensorManager.GRAVITY_EARTH,Toast.LENGTH_SHORT).show();
if (accelationSquareRoot>=2) {
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
if (isColor) {
view.setBackgroundColor(Color.CYAN);
} else {
view.setBackgroundColor(Color.MAGENTA);
}
isColor = !isColor;
}
}
@Override
protected void onResume()
{
super.onResume();

sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),Sensor
Manager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause(){
super.onPause();
sensorManager.unregisterListener(this);
}
}
Q2) WAP to display the list of sensors supported by the mobile java device.
XML FILE:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:padding="8dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</ScrollView>

JAVA FILE:-
package com.example.practical222;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
@SuppressLint("MissingInflatedId")
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> deviceSensors= sensorManager.getSensorList (Sensor.TYPE_ALL);
TextView textView=findViewById(R.id.tv);
for(Sensor sensor:deviceSensors){
textView.append(sensor.toString()+"\n\n");
}
}
}

You might also like