Write A Program To Changes The Background Color When Device Is Shuffled
Write A Program To Changes The Background Color When Device Is Shuffled
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
Sensor accelerometer =
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
}
2. Write a program to display the list of sensors supported by the mobile device.
actvity_main.xml Java code:
MainActivity.java package com.example.exp22_1;
<?xml version=”1.0” encoding=”utf-8”?> import androidx.annotation.RequiresApi;
<androidx.constraintlayout.widget.ConstraintLayout import androidx.appcompat.app.AppCompatActivity;
xmlns:android=”http://schemas.android.com/apk/res/android” import android.content.Context;
xmlns:app=”http://schemas.android.com/apk/res-auto” import android.hardware.Sensor;
xmlns:tools=”http://schemas.android.com/tools” import android.hardware.SensorManager; import
android:layout_width=”match_parent” android.os.Build; import android.os.Bundle; import
android:layout_height=”match_parent” android.widget.ArrayAdapter; import
tools:context=”.MainActivity”> android.widget.ListView; import java.util.List; public class
<ListView MainActivity extends AppCompatActivity
android:id=”@+id/listView1” {SensorManager smm; List sensor; ListView lv;
android:layout_width=”match_parent” @RequiresApi(api = Build.VERSION_CODES.M) @Override
android:layout_height=”match_parent” /> protected void onCreate(Bundle savedInstanceState)
</androidx.constraintlayout.widget.ConstraintLayout> {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); smm =
(SensorManager)
getSystemService(Context.SENSOR_SERVICE); lv =
(ListView) findViewById (R.id.listView1); sensor =
smm.getSensorList(Sensor.TYPE_ALL); lv.setAdapter(new
ArrayAdapter(this,android.R.layout.simple_list_item_1,
sensor)); } }
Output:
1.Write a program to capture an image and display it using image view.
activity_main.xml
<LinearLayout @Override
xmlns:android=”http://schemas.android.com/apk/res/andr protected void onActivityResult(int requestCode, int
oid” resultCode, Intent data) {
xmlns:app=”http://schemas.android.com/apk/res-auto” super.onActivityResult(requestCode, resultCode,
xmlns:tools=”http://schemas.android.com/tools” data);
android:layout_width=”match_parent” if (requestCode == CAMERA_REQUEST && resultCode
android:layout_height=”match_parent” == RESULT_OK && data != null) {
tools:context=”.MainActivity” Bitmap bitmap = (Bitmap)
android:orientation=”vertical”> data.getExtras().get("data");
<ImageView imageView.setImageBitmap(bitmap);
android:id=”@+id/imageView” }
android:layout_width=”fill_parent” }
android:layout_height=”403dp”
android:layout_centerHorizontal=”true” /> public void EnableRuntimePermission() {
<Button if
android:id=”@+id/button” (ActivityCompat.shouldShowRequestPermissionRationale(
android:layout_width=”match_parent” this, Manifest.permission.CAMERA)) {
android:layout_height=”73dp” Toast.makeText(this, "CAMERA permission allows
android:fontFamily=”@font/poppins_medium” us to access the CAMERA app",
android:text=”Click here to capture image using camera”/> Toast.LENGTH_LONG).show();
</LinearLayout> } else {
Java code: ActivityCompat.requestPermissions(this, new
package com.example; String[]{Manifest.permission.CAMERA},
RequestPermissionCode);
import androidx.appcompat.app.AppCompatActivity; }
import androidx.core.app.ActivityCompat; }
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager; @Override
import android.graphics.Bitmap; public void onRequestPermissionsResult(int
import android.os.Bundle; requestCode, String[] permissions, int[]
import android.view.View;
import android.widget.Button;
grantResults) {
import android.widget.ImageView;
import android.widget.Toast; super.onRequestPermissionsResult(requestCode,
public class MainActivity extends AppCompatActivity { permissions, grantResults);
Button button;
ImageView imageView;
if (requestCode == RequestPermissionCode) {
public static final int RequestPermissionCode = 1; if (grantResults.length > 0 && grantResults[0]
public static final int CAMERA_REQUEST = 7; == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted,
@Override
protected void onCreate(Bundle savedInstanceState) {
Now your application can access the CAMERA.",
super.onCreate(savedInstanceState); Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main); } else {
Toast.makeText(this, "Permission Denied,
button = findViewById(R.id.button);
imageView = findViewById(R.id.imageView);
Now your application cannot access the CAMERA.",
Toast.LENGTH_LONG).show();
EnableRuntimePermission(); }
}
button.setOnClickListener(new View.OnClickListener() {
@Override
}
public void onClick(View view) { }
Intent intent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
3. Write a program to record a video using various camera methods.
activity_main.xml }
<?xml version="1.0" encoding="utf-8"?> }
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res @Override
-auto" protected void onActivityResult(int
xmlns:tools="http://schemas.android.com/tools" requestCode, int resultCode, Intent data) {
android:layout_width="match_parent" super.onActivityResult(requestCode,
android:layout_height="match_parent" resultCode, data);
xmlns:android="http://schemas.android.com/apk if (requestCode == VIDEO_REQUEST &&
/res/android" resultCode == RESULT_OK) {
tools:context=".MainActivity" videoUri = data.getData();
android:orientation="vertical"> }
<Button }
android:layout_width="250dp" }
android:layout_height="wrap_content"
android:text="Capture Video" // activity_video_play.xml
android:onClick="captureVideo" <?xml version="1.0" encoding="utf-8"?>
android:layout_gravity="center_horizontal" <RelativeLayout
android:layout_marginTop="150dp" xmlns:android="http://schemas.android.com/a
/> pk/res/android"
<Button
android:layout_width="250dp" xmlns:app="http://schemas.android.com/apk/r
android:layout_height="wrap_content" es-auto"
android:text="Play Video"
android:onClick="playVideo" xmlns:tools="http://schemas.android.com/tool
android:layout_gravity="center_horizontal" s"
android:layout_marginTop="25dp" android:layout_width="match_parent"
/> android:layout_height="match_parent"
</LinearLayout> tools:context=".VideoPlayActivity">
Java code:
package com.example.exp23_2; <VideoView
android:id="@+id/videoView"
import android:layout_width="match_parent"
androidx.appcompat.app.AppCompatActivity; android:layout_height="match_parent"
import android.content.Intent; android:layout_margin="35dp" />
import android.net.Uri; </RelativeLayout>
import android.os.Bundle;
import android.provider.MediaStore; // VideoPlayActivity.java
import android.view.View; package com.example.exp23_2;
<uses-feature
android:name="android.hardware.camera2"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_roun
d"
android:supportsRtl="true"
android:theme="@style/Theme.Exp23_2">
<activity android:name=".VideoPlayActivity"
/>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCH
ER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
<LinearLayout public void onClick(View v) {
year = calendar.get(Calendar.YEAR);
xmlns:android="http://schemas.android.com/apk/res/android" month = calendar.get(Calendar.MONTH);
android:layout_width="match_parent" day =
android:layout_height="match_parent" calendar.get(Calendar.DAY_OF_MONTH);
android:orientation="vertical"
android:padding="20dp"> DatePickerDialog datePickerDialog = new
DatePickerDialog(MainActivity.this, (view,
<TextView selectedYear, selectedMonth, selectedDay) -> {
android:id="@+id/textView" String date = selectedDay + "/" +
android:layout_width="wrap_content" (selectedMonth + 1) + "/" + selectedYear;
android:layout_height="wrap_content" textView.setText("Selected Date: " + date);
android:text="Select Date and Time" }, year, month, day);
android:textSize="18sp" datePickerDialog.show();
android:padding="10dp"/> }
});
<Button
android:id="@+id/btnSelectDate" btnSelectTime.setOnClickListener(new
android:layout_width="wrap_content" View.OnClickListener() {
android:layout_height="wrap_content" @Override
android:text="Select Date"/> public void onClick(View v) {
hour =
<Button calendar.get(Calendar.HOUR_OF_DAY);
android:id="@+id/btnSelectTime" minute = calendar.get(Calendar.MINUTE);
android:layout_width="wrap_content"
android:layout_height="wrap_content" TimePickerDialog timePickerDialog = new
android:text="Select Time"/> TimePickerDialog(MainActivity.this, (view,
selectedHour, selectedMinute) -> {
</LinearLayout> String time = selectedHour + ":" +
Java code: String.format("%02d", selectedMinute);
package com.example.datetimepicker; textView.setText("Selected Time: " + time);
}, hour, minute, true);
import android.app.DatePickerDialog; timePickerDialog.show();
import android.app.TimePickerDialog; }
import android.os.Bundle; });
import android.view.View; }
import android.widget.Button; }
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
btnSelectDate = findViewById(R.id.btnSelectDate);
btnSelectTime = findViewById(R.id.btnSelectTime);
calendar = Calendar.getInstance();
btnSelectDate.setOnClickListener(new
View.OnClickListener() {
@Override
Write a program to create a HelloWorld Activity using all lifecycles methods to display messages
using Log.d.
package com.example.helloworld;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Activity Created");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: Activity Started");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: Activity Resumed");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: Activity Paused");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: Activity Stopped");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: Activity Restarted");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: Activity Destroyed");
}
}
1. Write a program to create a text field and a button “Navigate”. Whenyou enter
“www.google.com” and press navigate button it should open google page.
<LinearLayout
Toast.makeText(MainActivity.this,
xmlns:android="http://schemas.android.com/apk/res/android" "Please enter a valid URL",
android:layout_width="match_parent" Toast.LENGTH_SHORT).show();
android:layout_height="match_parent" }
android:orientation="vertical" }
android:padding="20dp"> });
}
<EditText }
android:id="@+id/editTextUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL (e.g., www.google.com)"
android:inputType="textUri"/>
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"/>
java code:
package com.example.openwebpage;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUrl = findViewById(R.id.editTextUrl);
btnNavigate = findViewById(R.id.btnNavigate);
btnNavigate.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editTextUrl.getText().toString().trim();
if (!url.isEmpty()) {
if (!url.startsWith("http://") &&
!url.startsWith("https://")) {
url = "https://" + url;
}
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
} else {
2.Write a program to create button “Start Dialer”. When u click on this button it should open the
phone dialer.
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartDialer =
findViewById(R.id.btnStartDialer);
btnStartDialer.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new
Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"));
startActivity(intent);
}
});
}
}
3.Write a program to create two screens. First screen will take one number input from user. After
click on Factorial button, second screen will open and it should display factorial of the same number.
Also specify which type of intent you will use in this case.
btnCalculate.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String input =
editTextNumber.getText().toString().trim();
if (!input.isEmpty()) {
int number =
Integer.parseInt(input);
Intent intent = new
Intent(MainActivity.this,
FactorialActivity.class);
intent.putExtra("number",
number);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Please
enter a number",
Toast.LENGTH_SHORT).show();
}
}
});
}
}