1. Develop a program to perform addition, subtraction, division, multiplication of two numbers and display the result.
(Use appropriate UI controls).
1. XML Layout (activity_main.xml)
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />
<Button
android:id="@+id/addBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/subBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract" />
<Button
android:id="@+id/mulBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply" />
<Button
android:id="@+id/divBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="20sp"
android:paddingTop="20dp" />
</LinearLayout>
2. Java Code ([Link])
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
Button addBtn, subBtn, mulBtn, divBtn;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Linking with UI
num1 = findViewById([Link].num1);
num2 = findViewById([Link].num2);
addBtn = findViewById([Link]);
subBtn = findViewById([Link]);
mulBtn = findViewById([Link]);
divBtn = findViewById([Link]);
result = findViewById([Link]);
// Add Button
[Link](v -> calculate("+"));
// Subtract Button
[Link](v -> calculate("-"));
// Multiply Button
[Link](v -> calculate("*"));
// Divide Button
[Link](v -> calculate("/"));
}
// Function to perform calculation
private void calculate(String operator) {
try {
double number1 = [Link]([Link]().toString());
double number2 = [Link]([Link]().toString());
double res = 0;
switch (operator) {
case "+": res = number1 + number2; break;
case "-": res = number1 - number2; break;
case "*": res = number1 * number2; break;
case "/":
if (number2 == 0) {
[Link](this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
res = number1 / number2;
break;
}
[Link]("Result: " + res);
} catch (NumberFormatException e) {
[Link](this, "Please enter valid numbers", Toast.LENGTH_SHORT).show();
}
}
}
2. Develop an application to display a Google Map. (Write JAVA &
Manifest file)
[Link] code:
<manifest xmlns:android="[Link]
package="[Link]">
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:theme="@style/[Link]"
android:label="@string/app_name">
<!-- Google Maps API Key -->
<meta-data
android:name="[Link].API_KEY"
android:value="YOUR_API_KEY_HERE"/>
<activity android:name=".MapsActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]"/>
</intent-filter>
</activity>
</application>
</manifest>
JAVA CODE:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById([Link]);
[Link](this); // Load map when ready
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myPlace = new LatLng(28.6139, 77.2090); // Example: New Delhi
[Link](new MarkerOptions().position(myPlace).title("Marker in Delhi"));
[Link]([Link](myPlace, 10));
}
}
XML Layout (activity_maps.xml)
<fragment xmlns:android="[Link]
android:id="@+id/map"
android:name="[Link]"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3. Explain property animation method to animate the properties of view object with
example.
A property animation changes a property's (a field in an object) value over a specified length
of time. To animate something, you specify the object property that you want to animate,
such as an object's position on the screen, how long you want to animate it for, and what
values you want to animate between.
The property animation system lets you define the following characteristics of an animation:
Duration: You can specify the duration of an animation. The default length is 300 ms.
Time interpolation: You can specify how the values for the property are calculated as a function of the animation's
current elapsed time.
Repeat count and behavior: You can specify whether or not to have an animation repeat
when it reaches the end of a duration and how many times to repeat the animation. You can
also specify whether you want the animation to play back in reverse. Setting it to reverse
plays the animation forwards then backwards repeatedly, until the number of repeats is
reached.
Animator sets: You can group animations into logical sets that play together or sequentially
or after specified delays.
Frame refresh delay: You can specify how often to refresh frames of your animation. The
default is set to refresh every 10 ms, but the speed in which your application can refresh
frames is ultimately dependent on how busy the system is overall and how fast the system
can service the underlying timer.
Common Properties Animated:
o alpha (fade in/out)
o translationX, translationY (move)
o rotation (rotate)
o scaleX, scaleY (resize)
Example: Fade Out a Button Using Property Animation
// Import required class
import [Link];
// Inside onCreate or onClick
ObjectAnimator fadeOut = [Link](button, "alpha", 1f, 0f);
[Link](1000); // 1 second
[Link]();
Example: Move a Button to the Right
ObjectAnimator moveRight = [Link](button, "translationX", 0f,
300f);
[Link](1000); // 1 second
[Link]();
[Link] an android application to show the list of paired devices by Bluetooth.
Step 1: Add Permissions
In your [Link], add the necessary Bluetooth permissions.
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].BLUETOOTH_ADMIN"/>
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION"/>
Step 2: Create the Layout
In res/layout/activity_main.xml, create a ListView to show the paired Bluetooth devices.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired Bluetooth Devices"
android:textSize="20sp"
android:textStyle="bold"
android:paddingBottom="10dp"/>
<ListView
android:id="@+id/bluetoothListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Now, in [Link], write the code to list the paired Bluetooth devices
// 1. Required Imports
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
ListView listView;
BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
// Link to layout with ListView
setContentView([Link].activity_main);
// Find ListView by ID
listView = findViewById([Link]);
// Get Bluetooth adapter
bluetoothAdapter = [Link]();
// Get paired devices
Set<BluetoothDevice> pairedDevices = [Link]();
// Create array to store names
String[] names = new String[[Link]()];
int i = 0;
// Add device names to array
for (BluetoothDevice device : pairedDevices) {
names[i++] = [Link]();
// Display names in ListView
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, [Link].simple_list_item_1, names);
[Link](adapter);
1) Explain Text View with 4 attributes.
1) TextView
A TextView is used to display text to the user.
Attributes:
android:text: Specifies the text to display.
android:textSize: Sets the size of the text.
android:textColor: Defines the color of the text.
android:gravity: Aligns the text within the TextView (e.g., center, left).
2) Explain Edit View with 4 attributes
2) EditText
An EditText is used to take user input in text form.
Attributes:
android:hint: Displays a hint text when the field is empty.
android:inputType: Defines the type of data (e.g., text, number).
android:maxLength: Sets the maximum number of characters the user can input.
android:password: Hides the text input to create a password field.
3) Explain Button with 4 attributes.
3) Button
A Button is a clickable UI element used to perform actions when clicked.
Attributes:
android:text: The text displayed on the button.
android:onClick: Defines the method to call when the button is clicked.
android:background: Sets the background color or image of the button.
android:textColor: Sets the color of the text on the button.
4) Explain Image Button with 4 attributes.
4) ImageButton
An ImageButton is a button that uses an image as the clickable area.
Attributes:
android:src: Defines the image to display on the button.
android:contentDescription: Provides a description for accessibility services.
android:background: Sets the background of the button.
android:scaleType: Defines how the image should scale (e.g., centerCrop).
5) Explain Toggle Button with 4 attributes.
5) ToggleButton
A ToggleButton is used for switching between two states (on/off).
Attributes:
android:textOn: The text shown when the button is in the "on" state.
android:textOff: The text shown when the button is in the "off" state.
android:checked: Specifies whether the button is initially in the "on" state.
android:background: Sets the background of the toggle button.
6) Explain Radio Group with 4 attributes.
6) RadioGroup
A RadioGroup is used to group RadioButton widgets together, where only one can be selected at a time.
Attributes:
android:orientation: Defines whether the buttons are arranged horizontally or vertically.
android:checkedButton: Specifies which RadioButton is selected by default.
android:layout_width: Defines the width of the RadioGroup.
android:layout_height: Defines the height of the RadioGroup.
7) Explain Checkbox with 4 attributes
7) Checkbox
A Checkbox allows the user to select one or more options.
Attributes:
android:text: The label displayed next to the checkbox.
android:checked: Defines whether the checkbox is checked by default.
android:enabled: Whether the checkbox is clickable or not.
android:layout_width: Defines the width of the checkbox.
8) Explain ProgressBar with 4 attributes
8) ProgressBar
A ProgressBar is used to show the progress of a task.
Attributes:
android:progress: Defines the current progress (between 0 and max).
android:max: The maximum value the progress can reach.
android:indeterminate: Defines whether the progress is indeterminate (spinning) or not.
android:visibility: Sets whether the progress bar is visible or gone.
9) Explain List View with example.
9) ListView
A ListView is used to display a list of items in a scrollable view.
Example:
xml
CopyEdit
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You can bind data to it using an Adapter like ArrayAdapter.
10) Explain GridView with example.
10) GridView
A GridView displays items in a two-dimensional grid.
Example:
xml
CopyEdit
<GridView
android:id="@+id/myGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" />
Use a GridAdapter to bind data to it.
11) Explain Scroll View with example.
11) ScrollView
A ScrollView is used to create a vertically scrollable view for large content.
Example:
xml
CopyEdit
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add more content here -->
</LinearLayout>
</ScrollView>
12) Explain Custom Toast with example.
12) Custom Toast
A Custom Toast displays a message in a custom layout for a short duration.
Example:
java
CopyEdit
Toast toast = new Toast(getApplicationContext());
View view = getLayoutInflater().inflate([Link].custom_toast, null);
[Link](view);
[Link](Toast.LENGTH_SHORT);
[Link]();
Here, custom_toast.xml is a custom layout.