1. Write a program to changes the background color when device is shuffled.
Xml code: if (sensorManager != null) {
<?xml version="1.0" encoding="utf-8"?> Sensor accelerometer =
<RelativeLayout [Link](Sensor.TYPE_ACCELEROMETER);
xmlns:android="[Link] if (accelerometer != null) {
android:layout_width="match_parent" [Link](this, accelerometer,
android:layout_height="match_parent" SensorManager.SENSOR_DELAY_NORMAL);
android:id="@+id/root_layout"> }
}
<TextView }
android:layout_width="wrap_content"
android:layout_height="wrap_content" @Override
android:text="Shake the device to change the background public void onSensorChanged(SensorEvent event) {
color!" if ([Link]() == Sensor.TYPE_ACCELEROMETER) {
android:textSize="18sp" float x = [Link][0];
android:textColor="#FFFFFF" float y = [Link][1];
android:layout_centerInParent="true"/> float z = [Link][2];
</RelativeLayout> long currentTime = [Link]();
Java code: if ((currentTime - lastTime) > 100) {
package [Link]; long diffTime = currentTime - lastTime;
lastTime = currentTime;
import [Link];
import [Link]; float speed = [Link](x + y + z - lastX - lastY - lastZ) /
import [Link]; diffTime * 10000;
import [Link];
import [Link]; if (speed > SHAKE_THRESHOLD) {
import [Link]; changeBackgroundColor();
import [Link]; }
import [Link];
import [Link]; lastX = x;
lastY = y;
public class MainActivity extends AppCompatActivity implements lastZ = z;
SensorEventListener { }
}
private SensorManager sensorManager; }
private View rootView;
private float lastX, lastY, lastZ; private void changeBackgroundColor() {
private long lastTime; Random random = new Random();
private static final int SHAKE_THRESHOLD = 800; int color = [Link]([Link](256),
[Link](256), [Link](256));
@Override [Link](color);
protected void onCreate(Bundle savedInstanceState) { }
[Link](savedInstanceState);
setContentView([Link].activity_main); @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onPause() {
[Link]();
[Link](this);
}
@Override
protected void onResume() {
[Link]();
Sensor accelerometer =
[Link](Sensor.TYPE_ACCELEROMETER);
if (accelerometer != null) {
[Link](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:
[Link] package [Link].exp22_1;
<?xml version=”1.0” encoding=”utf-8”?> import [Link];
<[Link] import [Link];
xmlns:android=”[Link] import [Link];
xmlns:app=”[Link] import [Link];
xmlns:tools=”[Link] import [Link]; import
android:layout_width=”match_parent” [Link]; import [Link]; import
android:layout_height=”match_parent” [Link]; import
tools:context=”.MainActivity”> [Link]; import [Link]; 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)
</[Link]> {[Link](savedInstanceState);
setContentView([Link].activity_main); smm =
(SensorManager)
getSystemService(Context.SENSOR_SERVICE); lv =
(ListView) findViewById ([Link].listView1); sensor =
[Link](Sensor.TYPE_ALL); [Link](new
ArrayAdapter(this,[Link].simple_list_item_1,
sensor)); } }
Output:
[Link] a program to capture an image and display it using image view.
activity_main.xml
<LinearLayout @Override
xmlns:android=”[Link] protected void onActivityResult(int requestCode, int
oid” resultCode, Intent data) {
xmlns:app=”[Link] [Link](requestCode, resultCode,
xmlns:tools=”[Link] 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”> [Link]().get("data");
<ImageView [Link](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” ([Link](
android:layout_width=”match_parent” this, [Link])) {
android:layout_height=”73dp” [Link](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: [Link](this, new
package [Link]; String[]{[Link]},
RequestPermissionCode);
import [Link]; }
import [Link]; }
import [Link];
import [Link];
import [Link]; @Override
import [Link]; public void onRequestPermissionsResult(int
import [Link]; requestCode, String[] permissions, int[]
import [Link];
import [Link];
grantResults) {
import [Link];
import [Link]; [Link](requestCode,
public class MainActivity extends AppCompatActivity { permissions, grantResults);
Button button;
ImageView imageView;
if (requestCode == RequestPermissionCode) {
public static final int RequestPermissionCode = 1; if ([Link] > 0 && grantResults[0]
public static final int CAMERA_REQUEST = 7; == PackageManager.PERMISSION_GRANTED) {
[Link](this, "Permission Granted,
@Override
protected void onCreate(Bundle savedInstanceState) {
Now your application can access the CAMERA.",
[Link](savedInstanceState); Toast.LENGTH_LONG).show();
setContentView([Link].activity_main); } else {
[Link](this, "Permission Denied,
button = findViewById([Link]);
imageView = findViewById([Link]);
Now your application cannot access the CAMERA.",
Toast.LENGTH_LONG).show();
EnableRuntimePermission(); }
}
[Link](new [Link]() {
@Override
}
public void onClick(View view) { }
Intent intent = new
Intent([Link].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="[Link] @Override
-auto" protected void onActivityResult(int
xmlns:tools="[Link] requestCode, int resultCode, Intent data) {
android:layout_width="match_parent" [Link](requestCode,
android:layout_height="match_parent" resultCode, data);
xmlns:android="[Link] if (requestCode == VIDEO_REQUEST &&
/res/android" resultCode == RESULT_OK) {
tools:context=".MainActivity" videoUri = [Link]();
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="[Link]
/> pk/res/android"
<Button
android:layout_width="250dp" xmlns:app="[Link]
android:layout_height="wrap_content" es-auto"
android:text="Play Video"
android:onClick="playVideo" xmlns:tools="[Link]
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 [Link].exp23_2; <VideoView
android:id="@+id/videoView"
import android:layout_width="match_parent"
[Link]; android:layout_height="match_parent"
import [Link]; android:layout_margin="35dp" />
import [Link]; </RelativeLayout>
import [Link];
import [Link]; // [Link]
import [Link]; package [Link].exp23_2;
public class MainActivity extends import
AppCompatActivity { [Link];
private static final int VIDEO_REQUEST = 101; import [Link];
private Uri videoUri = null; import [Link];
import [Link];
@Override
protected void onCreate(Bundle public class VideoPlayActivity extends
savedInstanceState) { AppCompatActivity {
[Link](savedInstanceState); private VideoView mVideoView;
setContentView([Link].activity_main);
} @Override
protected void onCreate(Bundle
public void captureVideo(View view) { savedInstanceState) {
Intent videoIntent = new [Link](savedInstanceState);
Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if setContentView([Link].activity_video_play);
([Link](getPackageManager(
)) != null) { mVideoView =
startActivityForResult(videoIntent, findViewById([Link]);
VIDEO_REQUEST); String videoUriString =
} getIntent().getStringExtra("videoUri");
} if (videoUriString != null) {
Uri videoUri = [Link](videoUriString);
public void playVideo(View view) { [Link](videoUri);
if (videoUri != null) { [Link]();
Intent playIntent = new Intent(this, }
[Link]); }
[Link]("videoUri", }
[Link]());
startActivity(playIntent); // [Link]
<?xml version="1.0" encoding="utf-8"?>
<uses-permission <manifest
android:name="[Link]" /> xmlns:android="[Link]
<uses-permission pk/res/android"
android:name="[Link].RECORD_AU package="[Link].exp23_2">
DIO" />
<uses-permission
android:name="[Link].WRITE_EXTE
RNAL_STORAGE" />
<uses-feature
android:name="[Link].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="[Link]" />
<category
android:name="[Link]
ER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
<LinearLayout public void onClick(View v) {
year = [Link]([Link]);
xmlns:android="[Link] month = [Link]([Link]);
android:layout_width="match_parent" day =
android:layout_height="match_parent" [Link](Calendar.DAY_OF_MONTH);
android:orientation="vertical"
android:padding="20dp"> DatePickerDialog datePickerDialog = new
DatePickerDialog([Link], (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" [Link]("Selected Date: " + date);
android:text="Select Date and Time" }, year, month, day);
android:textSize="18sp" [Link]();
android:padding="10dp"/> }
});
<Button
android:id="@+id/btnSelectDate" [Link](new
android:layout_width="wrap_content" [Link]() {
android:layout_height="wrap_content" @Override
android:text="Select Date"/> public void onClick(View v) {
hour =
<Button [Link](Calendar.HOUR_OF_DAY);
android:id="@+id/btnSelectTime" minute = [Link]([Link]);
android:layout_width="wrap_content"
android:layout_height="wrap_content" TimePickerDialog timePickerDialog = new
android:text="Select Time"/> TimePickerDialog([Link], (view,
selectedHour, selectedMinute) -> {
</LinearLayout> String time = selectedHour + ":" +
Java code: [Link]("%02d", selectedMinute);
package [Link]; [Link]("Selected Time: " + time);
}, hour, minute, true);
import [Link]; [Link]();
import [Link]; }
import [Link]; });
import [Link]; }
import [Link]; }
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button btnSelectDate, btnSelectTime;
private Calendar calendar;
private int year, month, day, hour, minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
textView = findViewById([Link]);
btnSelectDate = findViewById([Link]);
btnSelectTime = findViewById([Link]);
calendar = [Link]();
[Link](new
[Link]() {
@Override
Write a program to create a HelloWorld Activity using all lifecycles methods to display messages
using Log.d.
package [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d(TAG, "onCreate: Activity Created");
}
@Override
protected void onStart() {
[Link]();
Log.d(TAG, "onStart: Activity Started");
}
@Override
protected void onResume() {
[Link]();
Log.d(TAG, "onResume: Activity Resumed");
}
@Override
protected void onPause() {
[Link]();
Log.d(TAG, "onPause: Activity Paused");
}
@Override
protected void onStop() {
[Link]();
Log.d(TAG, "onStop: Activity Stopped");
}
@Override
protected void onRestart() {
[Link]();
Log.d(TAG, "onRestart: Activity Restarted");
}
@Override
protected void onDestroy() {
[Link]();
Log.d(TAG, "onDestroy: Activity Destroyed");
}
}
1. Write a program to create a text field and a button “Navigate”. Whenyou enter
“[Link]” and press navigate button it should open google page.
<LinearLayout
[Link]([Link],
xmlns:android="[Link] "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., [Link])"
android:inputType="textUri"/>
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"/>
java code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText editTextUrl;
private Button btnNavigate;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextUrl = findViewById([Link]);
btnNavigate = findViewById([Link]);
[Link](new
[Link]() {
@Override
public void onClick(View v) {
String url = [Link]().toString().trim();
if (![Link]()) {
if ();
startActivity(intent);
} else {
[Link] a program to create button “Start Dialer”. When u click on this button it should open the
phone dialer.
Xml code: Java code:
<LinearLayout package [Link];
xmlns:android="[Link] import [Link];
roid"
import [Link];
android:layout_width="match_parent"
android:layout_height="match_parent"
import [Link];
android:orientation="vertical" import [Link];
android:gravity="center" import [Link];
android:padding="20dp">
import
<Button [Link]
android:id="@+id/btnStartDialer" ity;
android:layout_width="wrap_content"
android:layout_height="wrap_content" public class MainActivity extends
android:text="Start Dialer"/>
AppCompatActivity {
</LinearLayout>
private Button btnStartDialer;
@Override
protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnStartDialer =
findViewById([Link]);
[Link](new
[Link]() {
@Override
public void onClick(View v) {
Intent intent = new
Intent(Intent.ACTION_DIAL);
[Link]([Link]("[Link]
startActivity(intent);
}
});
}
}
[Link] 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.
<LinearLayout Java code:
package [Link];
xmlns:android="[Link] import [Link];
d" import [Link];
android:layout_width="match_parent" import [Link];
android:layout_height="match_parent" import [Link];
android:orientation="vertical" import [Link];
android:gravity="center" import [Link];
android:padding="20dp"> import
[Link]
<EditText y;
android:id="@+id/editTextNumber" public class MainActivity extends
android:layout_width="match_parent" AppCompatActivity {
android:layout_height="wrap_content" private EditText editTextNumber;
android:hint="Enter a number" private Button btnCalculate;
android:inputType="number"/> @Override
protected void onCreate(Bundle
<Button savedInstanceState) {
android:id="@+id/btnCalculate" [Link](savedInstanceState);
android:layout_width="wrap_content"
android:layout_height="wrap_content" setContentView([Link].activity_main);
android:text="Factorial"/>
</LinearLayout> editTextNumber =
findViewById([Link]);
btnCalculate =
findViewById([Link]);
[Link](new
[Link]() {
@Override
public void onClick(View v) {
String input =
[Link]().toString().trim();
if (![Link]()) {
int number =
[Link](input);
Intent intent = new
Intent([Link],
[Link]);
[Link]("number",
number);
startActivity(intent);
} else {
[Link]([Link], "Please
enter a number",
Toast.LENGTH_SHORT).show();
}
}
});
}
}