0% found this document useful (0 votes)
216 views

Practical 22

Uploaded by

hackeromsutar
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)
216 views

Practical 22

Uploaded by

hackeromsutar
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/ 6

Practical 22

Q1)
XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@color/black"
android:layout_centerInParent="true"/>
</RelativeLayout>

JAVA FILE

package com.example.practical221;
import androidx.appcompat.app.AppCompatActivity;
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 AppCompatActivity implements
SensorEventListener {
SensorManager sm;
Sensor s;
boolean isShuffled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm= (SensorManager) getSystemService(SENSOR_SERVICE);
if (sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {s =
sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
else {
Toast.makeText(this, "Sensor Not Found !", Toast.LENGTH_SHORT).show();
}}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if (!isShuffled && (Math.abs(x) > 15 || Math.abs(y) > 15 || Math.abs(z) >15)) {
changeBackgroundColor();
isShuffled = true;
}}
private void changeBackgroundColor() {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.rgb((int) (Math.random() * 256), (int)
(Math.random() * 256), (int) (Math.random() * 256)));
Toast.makeText(this, "Color Changed ...", Toast.LENGTH_SHORT).show();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sm.unregisterListener(this);
}}
OUTPUT:-

Q2)
XML file
<?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">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA FILE

package com.example.practical222;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
SensorManager smm;
List<Sensor>sensors;
ListView lv;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
lv = (ListView) findViewById(R.id.listView1);
sensors = smm.getSensorList(Sensor.TYPE_ALL);
lv.setAdapter(new ArrayAdapter<Sensor>(this,
android.R.layout.simple_dropdown_item_1line,sensors));
}
}

OUTPUT:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy