• Title : Exp22_St1_08 :- Write a program to changes the background color when device
is shuffled.
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Shake to Switch Color"
android:textSize="25dp"
android:textColor="@color/black"
android:paddingLeft="85dp"
android:paddingTop="100dp"/>
</AbsoluteLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :-
package com.example.exp22_st1_08;
import androidx.appcompat.app.AppCompatActivity;
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.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private boolean isColor=false;
private View view;
private long lastUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=findViewById(R.id.textView);
view.setBackgroundColor(Color.GREEN);
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
accelationSquarRoot=(x*x+y*y+z*z)/(SensorManager.GRAVITY_EARTH*SensorManager.
GRAVITY_EARTH);
long actualTime=System.currentTimeMillis();
Toast.makeText(getApplicationContext(),String.valueOf(accelationSquarRoot)+""+SensorM
anager.GRAVITY_EARTH,Toast.LENGTH_SHORT).show();
if(accelationSquarRoot>=2)
{
if(actualTime-lastUpdate<200){
return;
}
lastUpdate=actualTime;
if(isColor){
view.setBackgroundColor(Color.MAGENTA);
}
else{
view.setBackgroundColor(Color.BLUE);
}
isColor=!isColor;
}
}
@Override
protected void onResume(){
super.onResume();
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCEL
EROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause(){
super.onPause();
sensorManager.unregisterListener(this);
}
}
• Output :-
• Title : Exp22_St2_08 :- Write a program to display the list of sensors supported by the
mobile device.
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<TextView
android:id="@+id/sensorslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Sensors"
android:textSize="17dp"
android:textStyle="bold"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :-
package com.example.exp22_st2_08;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
SensorManager mgr;
TextView txtList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
txtList=(TextView)findViewById(R.id.sensorslist);
List<Sensor>sensorList=mgr.getSensorList(Sensor.TYPE_ALL);
StringBuilder strBuilder=new StringBuilder();
for (Sensor s:sensorList){
strBuilder.append(s.getName()+"\n");
}
txtList.setVisibility(View.VISIBLE);
txtList.setText(strBuilder);
}
}
• Output :-