0% found this document useful (0 votes)
15 views10 pages

mad code essy

The document outlines the development of various Android applications, including a calculator for basic arithmetic operations, a Google Maps display, and a Bluetooth device list viewer. It provides XML layouts and Java code for each application, along with explanations of UI components like TextView, EditText, Button, and others, detailing their attributes and usage. Additionally, it covers property animations and custom Toast messages in Android development.

Uploaded by

maseera429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

mad code essy

The document outlines the development of various Android applications, including a calculator for basic arithmetic operations, a Google Maps display, and a Bluetooth device list viewer. It provides XML layouts and Java code for each application, along with explanations of UI components like TextView, EditText, Button, and others, detailing their attributes and usage. Additionally, it covers property animations and custom Toast messages in Android development.

Uploaded by

maseera429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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="http://schemas.android.com/apk/res/android"
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 (MainActivity.java)


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText num1, num2;


Button addBtn, subBtn, mulBtn, divBtn;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Linking with UI
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
addBtn = findViewById(R.id.addBtn);
subBtn = findViewById(R.id.subBtn);
mulBtn = findViewById(R.id.mulBtn);
divBtn = findViewById(R.id.divBtn);
result = findViewById(R.id.result);

// Add Button
addBtn.setOnClickListener(v -> calculate("+"));

// Subtract Button
subBtn.setOnClickListener(v -> calculate("-"));

// Multiply Button
mulBtn.setOnClickListener(v -> calculate("*"));

// Divide Button
divBtn.setOnClickListener(v -> calculate("/"));
}

// Function to perform calculation


private void calculate(String operator) {
try {
double number1 = Double.parseDouble(num1.getText().toString());
double number2 = Double.parseDouble(num2.getText().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) {
Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
res = number1 / number2;
break;
}

result.setText("Result: " + res);


} catch (NumberFormatException e) {
Toast.makeText(this, "Please enter valid numbers", Toast.LENGTH_SHORT).show();
}
}
}

2. Develop an application to display a Google Map. (Write JAVA &


Manifest file)
AndroidManifest.xml code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapapp">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:label="@string/app_name">

<!-- Google Maps API Key -->


<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE"/>
<activity android:name=".MapsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
JAVA CODE:
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

SupportMapFragment mapFragment = (SupportMapFragment)


getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // Load map when ready
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myPlace = new LatLng(28.6139, 77.2090); // Example: New Delhi
mMap.addMarker(new MarkerOptions().position(myPlace).title("Marker in Delhi"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPlace, 10));
}
}

XML Layout (activity_maps.xml)

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
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 android.animation.ObjectAnimator;

// Inside onCreate or onClick

ObjectAnimator fadeOut = ObjectAnimator.ofFloat(button, "alpha", 1f, 0f);

fadeOut.setDuration(1000); // 1 second

fadeOut.start();

Example: Move a Button to the Right


ObjectAnimator moveRight = ObjectAnimator.ofFloat(button, "translationX", 0f,
300f);
moveRight.setDuration(1000); // 1 second
moveRight.start();

4.Design an android application to show the list of paired devices by Bluetooth.


Step 1: Add Permissions
In your AndroidManifest.xml, add the necessary Bluetooth permissions.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.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="http://schemas.android.com/apk/res/android"
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 MainActivity.java, write the code to list the paired Bluetooth devices

// 1. Required Imports

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

ListView listView;

BluetoothAdapter bluetoothAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Link to layout with ListView

setContentView(R.layout.activity_main);
// Find ListView by ID

listView = findViewById(R.id.bluetoothListView);

// Get Bluetooth adapter

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Get paired devices

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

// Create array to store names

String[] names = new String[pairedDevices.size()];

int i = 0;

// Add device names to array

for (BluetoothDevice device : pairedDevices) {

names[i++] = device.getName();

// Display names in ListView

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);

listView.setAdapter(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(R.layout.custom_toast, null);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();

Here, custom_toast.xml is a custom layout.

You might also like