Practical no 17
MainActivity.java
package com.example.pract17; import
android.os.Bundle; import
android.util.Log; import
android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; public class
MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Toast.makeText(this,"On Create", Toast.LENGTH_LONG).show();
Log.d("Activity Lifecycle", "Activity is created");}
@Override
protected void onStart() { super.onStart();
Toast.makeText(this,"On Start", Toast.LENGTH_LONG).show();}
@Override
protected void onResume() {super.onResume();
Toast.makeText(this,"On Resume", Toast.LENGTH_LONG).show();}
@Override
protected void onPause() {super.onPause();
Toast.makeText(this,"On Pause", Toast.LENGTH_LONG).show();}
@Override
protected void onStop() {super.onStop();
Toast.makeText(this,"On Stop", Toast.LENGTH_LONG).show();}
@Override
protected void onRestart() {super.onRestart();
Toast.makeText(this,"On Restart", Toast.LENGTH_LONG).show();}
@Override
protected void onDestroy() {super.onDestroy();
Toast.makeText(this,"On Destroy", Toast.LENGTH_LONG).show();}}
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">
<TextView
android:id="@+id/textView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pract17">
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Practical no 18
MainActivity.java
package com.example.android;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button explicit_btn, implicit_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explicit_btn = findViewById(R.id.explicit_btn);
implicit_btn = findViewById(R.id.implicit_btn);
// Implement OnClick event for Explicit Intent
explicit_btn.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this,
ActivitySecond.class);
startActivity(intent);});
// Implement OnClick event for Implicit Intent
implicit_btn.setOnClickListener(v -> { When we click on button explicit intent example the new
screen is open shows the message the second activity.
Intent intent = new
Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.abhiandroid.com"));
startActivity(intent);
});}}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="If you click on Explicit example we will navigate to
second activity within App and if you click on Implicit example
Google Homepage will open in Browser"
android:clickable="false"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:background="#3e7d02"
android:textColor="#ffffff"
tools:ignore="HardcodedText,VisualLintLongText,VisualLintBounds"
/> <Button
android:id="@+id/explicit_btn"
android:text="Open Second Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
tools:ignore="HardcodedText,VisualLintBounds,VisualLintOverlap"
/> <Button
android:id="@+id/implicit_btn"
android:text="Open Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/explicit_btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
tools:ignore="HardcodedText" />
</RelativeLayout>
Click on implicit intent
example the google.com home
page will open in a browser.
Practical no 22
MainActivity.java
package com.example.sensorsexample;
import android.annotation.SuppressLint;
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 androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager mgr = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
@SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView txtList =
findViewById(R.id.sensorslist);
List<Sensor> sensorList = mgr.getSensorList(Sensor.TYPE_ALL);
StringBuilder strBuilder = new StringBuilder();
for (Sensor s : sensorList) {
strBuilder.append(s.getName()).append("\n"); }
txtList.setVisibility(View.VISIBLE);
txtList.setText(strBuilder);}}
Activity_main.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:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/sensorslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Sensors"
android:textSize="20dp"
android:textStyle="bold"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
MainActivity.java
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 androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
SensorEventListener {
private SensorManager mgr;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
List<Sensor> deviceSensors = mgr.getSensorList(Sensor.TYPE_ALL);}
@Override
protected void onResume() {
super.onResume();
// Register the listener
if (sensor != null) {
mgr.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);}}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
float sensorValue = event.values[0]; }}
Activity_main.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:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/sensorValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sensor Value:"
android:textSize="18sp"/>
</LinearLayout>