Ex 22
Ex 22
Exercise
1. Write a program to change the background colour when device is shuffled.
activity_main.xml MainActivity.java
package com.example.ex2201;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout import
xmlns:android="http://schemas.android.com/apk/r androidx.appcompat.app.AppCompatActivity;
es/android"
import android.graphics.Color;
import android.hardware.Sensor;
xmlns:app="http://schemas.android.com/apk/res-
auto" import android.hardware.SensorEvent;
xmlns:tools="http://schemas.android.com/tools" import android.hardware.SensorEventListener;
android:layout_width="match_parent" import android.hardware.SensorManager;
android:layout_height="match_parent" import android.view.View;
android:gravity="center" import android.view.Window;
tools:context=".MainActivity"> import android.view.WindowManager;
import android.widget.Toast;
<TextView import android.os.Bundle;
android:id="@+id/textView"
android:layout_width="411dp" public class MainActivity extends
AppCompatActivity implements
android:layout_height="675dp"
SensorEventListener {
android:layout_gravity="center"
android:text="Shake to get a toast and to
private SensorManager sensorManager;
switch color"
private boolean color = false;
android:textAlignment="center"
private View view;
private long lastUpdate;
@Override
android:textColor="#131313"
protected void onCreate(Bundle
android:textSize="20sp" />
savedInstanceState) {
</LinearLayout>
requestWindowFeature(Window.FEATURE_NO_
TITLE);
getWindow().setFlags(WindowManager.LayoutPa
rams.FLAG_FULLSCREEN, {
WindowManager.LayoutParams.FLAG_FULLSC
if (actualTime - lastUpdate < 200) {
REEN);
return;
super.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main);
lastUpdate = actualTime;
Toast.makeText(this, "Device was
view = findViewById(R.id.textView);
shuffled", Toast.LENGTH_SHORT).show();
view.setBackgroundColor(Color.GREEN);
if (color) {
}
}
@Override
private void getAccelerometer(SensorEvent
event) { protected void onResume() {
float[] values = event.values; super.onResume();
float x = values[0]; sensorManager.registerListener(this,
float y = values[1];
sensorManager.getDefaultSensor(Sensor.TYPE_A
float z = values[2];
CCELEROMETER),
Output:
2. Write a program to display the list of sensors supported by the mobile device.
activity_main.xml