1)activity_main.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- This is inside LinearLayout -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout Item"
android:textSize="18sp"
android:textColor="#000000" />
<!-- AbsoluteLayout inside LinearLayout -->
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#EFEFEF">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_x="50dp"
android:layout_y="50dp" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_x="200dp"
android:layout_y="150dp" />
</AbsoluteLayout>
</LinearLayout>
Main_activity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2)
Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- FrameLayout for Background and Overlay -->
<FrameLayout
android:id="@+id/frameBackground"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:background="@drawable/img">
<!-- Overlay Text -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the App!"
android:textSize="24sp"
android:textColor="#FFFFFF"
android:layout_gravity="center" />
</FrameLayout>
<!-- TableLayout for Structured Content -->
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/frameBackground"
android:layout_marginTop="16dp"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your name" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your email" />
</TableRow>
</TableLayout>
<!-- RelativeLayout for Buttons -->
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tableLayout"
android:layout_marginTop="16dp">
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toLeftOf="@id/btnSubmit"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
3)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- Static Text -->
<TextView
android:id="@+id/staticText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your name:"
android:textSize="18sp"
android:layout_marginBottom="8dp" />
<!-- User Input -->
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name"
android:textSize="16sp"
android:inputType="textPersonName" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView staticText;
private EditText userInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
staticText = findViewById(R.id.staticText);
userInput = findViewById(R.id.userInput);
// Optionally, set a default text to the EditText
userInput.setText("John Doe");
}
}
4)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- Regular Button -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<!-- ImageButton -->
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"
android:contentDescription="Image Button"
android:layout_marginTop="16dp" />
<!-- ToggleButton -->
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
android:checked="false"
android:layout_marginTop="16dp" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ToggleButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageButton imageButton;
private ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
imageButton = findViewById(R.id.imageButton);
toggleButton = findViewById(R.id.toggleButton);
// Set click listener for regular Button
button.setOnClickListener(v -> {
Toast.makeText(MainActivity.this, "Button Clicked",
Toast.LENGTH_SHORT).show();
});
// Set click listener for ImageButton
imageButton.setOnClickListener(v -> {
Toast.makeText(MainActivity.this, "ImageButton Clicked",
Toast.LENGTH_SHORT).show();
});
// Set checked change listener for ToggleButton
toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
String status = isChecked ? "ON" : "OFF";
Toast.makeText(MainActivity.this, "ToggleButton is " + status,
Toast.LENGTH_SHORT).show();
});
}
}
5)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
android:background="#ffffff">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="24dp" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textPersonName"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginBottom="16dp" />
<CheckBox
android:id="@+id/rememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
android:layout_marginBottom="24dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText usernameEditText, passwordEditText;
Button loginButton;
CheckBox rememberMeCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.editTextUsername);
passwordEditText = findViewById(R.id.editTextPassword);
loginButton = findViewById(R.id.buttonLogin);
rememberMeCheckBox = findViewById(R.id.rememberMe);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username =
usernameEditText.getText().toString().trim();
String password =
passwordEditText.getText().toString().trim();
if (username.equals("admin") && password.equals("1234")) {
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Credentials",
Toast.LENGTH_SHORT).show();
}
if (rememberMeCheckBox.isChecked()) {
Toast.makeText(MainActivity.this, "Credentials Saved",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
6) xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- Checkbox for Terms and Conditions -->
<CheckBox
android:id="@+id/termsCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms and Conditions"
android:textSize="16sp"
android:layout_marginBottom="24dp" />
<!-- Submit Button -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="16sp" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private CheckBox termsCheckBox;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
termsCheckBox = findViewById(R.id.termsCheckBox);
submitButton = findViewById(R.id.submitButton);
// Set click listener for the submit button
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Check if the checkbox is checked
if (termsCheckBox.isChecked()) {
// Proceed with the action
Toast.makeText(MainActivity.this, "Terms accepted.
Proceeding...", Toast.LENGTH_SHORT).show();
} else {
// Inform the user to accept the terms
Toast.makeText(MainActivity.this, "Please accept the
terms and conditions to proceed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
7)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- RadioGroup to group RadioButtons -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- RadioButton for Option 1 -->
<RadioButton
android:id="@+id/radioOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<!-- RadioButton for Option 2 -->
<RadioButton
android:id="@+id/radioOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<!-- RadioButton for Option 3 -->
<RadioButton
android:id="@+id/radioOption3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
<!-- Button to confirm selection -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
radioGroup = findViewById(R.id.radioGroup);
submitButton = findViewById(R.id.submitButton);
// Set OnClickListener for the submit button
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the ID of the selected radio button
int selectedId = radioGroup.getCheckedRadioButtonId();
// Check if a radio button is selected
if (selectedId != -1) {
// Find the selected radio button
RadioButton selectedRadioButton =
findViewById(selectedId);
// Display the selected option
Toast.makeText(MainActivity.this,
"Selected: " + selectedRadioButton.getText(),
Toast.LENGTH_SHORT).show();
} else {
// No option selected
Toast.makeText(MainActivity.this,
"Please select an option",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
8)xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:gravity="center">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress"
android:layout_marginTop="20dp"/>
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private Button startButton;
private int progressStatus = 0;
private Handler handler = new Handler(); // For updating UI from
background thread
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to XML layout
progressBar = findViewById(R.id.progressBar);
startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(view -> {
progressStatus = 0;
new Thread(() -> {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar
handler.post(() ->
progressBar.setProgress(progressStatus));
try {
Thread.sleep(50); // Delay to simulate work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
});
}
}
11)
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">
<!-- Button to pick date -->
<Button
android:id="@+id/pickDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp"
android:onClick="showDatePickerDialog" />
<!-- Button to pick time -->
<Button
android:id="@+id/pickTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
app:layout_constraintTop_toBottomOf="@+id/pickDateButton"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:onClick="showTimePickerDialog" />
<!-- TextView to display selected date and time -->
<TextView
android:id="@+id/selectedDateTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date and Time will appear here"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/pickTimeButton"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java
package com.example.datetimepicker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TextView selectedDateTimeTextView;
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedDateTimeTextView = findViewById(R.id.selectedDateTimeTextView);
public void showDatePickerDialog(android.view.View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, (datePicker, selectedYear,
selectedMonth, selectedDay) -> {
// Display the selected date
String selectedDate = selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear;
selectedDateTimeTextView.setText("Selected Date: " + selectedDate);
}, year, month, day);
datePickerDialog.show();
}
public void showTimePickerDialog(android.view.View view) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, (timePicker, selectedHour,
selectedMinute) -> {
// Display the selected time
String selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute);
selectedDateTimeTextView.append("\nSelected Time: " + selectedTime);
}, hour, minute, true);
timePickerDialog.show();
13)Bluetooth connectivity
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnEnableBluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable Bluetooth"/>
<Button
android:id="@+id/btnShowDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Paired Devices"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Java
package com.example.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetoothAdapter;
ListView listView;
Button btnEnable, btnShow;
@RequiresApi(api = Build.VERSION_CODES.S) // Android 12+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnEnable = findViewById(R.id.btnEnableBluetooth);
btnShow = findViewById(R.id.btnShowDevices);
listView = findViewById(R.id.listView);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnEnable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth not
supported", Toast.LENGTH_SHORT).show();
} else if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
} else {
Toast.makeText(getApplicationContext(), "Bluetooth
already enabled", Toast.LENGTH_SHORT).show();
}
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter.isEnabled()) {
Set<BluetoothDevice> pairedDevices =
bluetoothAdapter.getBondedDevices();
ArrayAdapter<String> adapter = new
ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1);
for (BluetoothDevice device : pairedDevices) {
adapter.add(device.getName() + "\n" +
device.getAddress());
}
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(), "Enable Bluetooth
first", Toast.LENGTH_SHORT).show();
}
}
});
}
}
12) Develop a program to implement sensors.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/sensorTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sensor Data"
android:textSize="24sp"/>
</LinearLayout>
Java
package com.example.myapplication;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements
SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
private TextView sensorTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorTextView = findViewById(R.id.sensorTextView);
// Get sensor manager and accelerometer sensor
sensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
accelerometer =
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}
@Override
protected void onResume() {
super.onResume();
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
sensorTextView.setText("X: " + x + "\nY: " + y + "\nZ: " + z);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not used here
}
}