0% found this document useful (0 votes)
6 views5 pages

Ex 22

The document contains two programming exercises related to Android development. The first exercise involves creating an app that changes the background color when the device is shaken, utilizing the accelerometer sensor. The second exercise focuses on displaying a list of sensors supported by the mobile device using a ListView.

Uploaded by

Omkar Mankar
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)
6 views5 pages

Ex 22

The document contains two programming exercises related to Android development. The first exercise involves creating an app that changes the background color when the device is shaken, utilizing the accelerometer sensor. The second exercise focuses on displaying a list of sensors supported by the mobile device using a ListView.

Uploaded by

Omkar Mankar
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/ 5

X.

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) {

sensorManager = (SensorManager) view.setBackgroundColor(Color.GREEN);


getSystemService(SENSOR_SERVICE);
} else {
lastUpdate = System.currentTimeMillis();
view.setBackgroundColor(Color.RED);
}
}
color = !color;
@Override
}
public void onSensorChanged(SensorEvent
}
event) {
if (event.sensor.getType() ==
Sensor.TYPE_ACCELEROMETER) { @Override
getAccelerometer(event); public void onAccuracyChanged(Sensor sensor,
int accuracy) {
}

}
}

@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),

float accelationSquareRoot = (x * x + y * y + SensorManager.SENSOR_DELAY_NORMAL);


z * z) / (SensorManager.GRAVITY_EARTH *
}
SensorManager.GRAVITY_EARTH);
long actualTime = event.timestamp;
@Override
if (accelationSquareRoot >= 2) //
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}

Output:
2. Write a program to display the list of sensors supported by the mobile device.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> public class MainActivity extends


AppCompatActivity {
<LinearLayout
xmlns:android="http://schemas.android.com/apk/r
es/android"
ListView listView;
xmlns:tools="http://schemas.android.com/tools"
SensorManager manager;
android:layout_width="match_parent"
List<Sensor> sensors;
android:layout_height="match_parent"
android:orientation="vertical"
@Override
tools:context=".MainActivity">
protected void onCreate(Bundle
savedInstanceState) {
<ListView super.onCreate(savedInstanceState);
android:id="@+id/list" setContentView(R.layout.activity_main);
android:layout_width="match_parent"
android:layout_height="match_parent" /> listView=findViewById(R.id.list);
</LinearLayout>
manager=(SensorManager)getSystemService(Con
text.SENSOR_SERVICE);
MainActivity.xml
sensors=manager.getSensorList(Sensor.TYPE_A
LL);
package com.example.ex2202;
listView.setAdapter(new
ArrayAdapter<Sensor>(this,android.R.layout.sim
import ple_list_item_1,sensors));
androidx.appcompat.app.AppCompatActivity;
import android.content.Context; }
import android.hardware.Sensor; }
import android.hardware.SensorManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import android.os.Bundle;
Output:

You might also like