0% found this document useful (0 votes)
32 views6 pages

MAD Summer-24 Answer (Assign. Only)

The document contains a series of Android programming assignments that cover various topics such as Toggle Buttons, AutoCompleteTextView, SQLite databases, Fragments, and Google Maps. It includes code snippets for creating user interfaces and functionalities like displaying sensors, calculating factorials, and sending emails. Additionally, it explains the significance of SQLite in Android and the Android Security Model.

Uploaded by

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

MAD Summer-24 Answer (Assign. Only)

The document contains a series of Android programming assignments that cover various topics such as Toggle Buttons, AutoCompleteTextView, SQLite databases, Fragments, and Google Maps. It includes code snippets for creating user interfaces and functionalities like displaying sensors, calculating factorials, and sending emails. Additionally, it explains the significance of SQLite in Android and the Android Security Model.

Uploaded by

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

Assignment No.

4
Q. 2. List any two attributes of Toggle Button.

Answer:
Two attributes of Toggle Button in Android are:
1. android:textOn – Specifies the text to display when the toggle is ON.
2. android:textOff – Specifies the text to display when the toggle is OFF. ✅

Q. 6. Write a program to create first display screen of any search engine using auto
complete text view.

Answer:
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

String[] suggestions = {"Google", "Bing", "DuckDuckGo", "Yahoo", "Ask"};


AutoCompleteTextView actv = findViewById(R.id.autoText);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, suggestions);
actv.setAdapter(adapter);
}
}

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="16dp">

<AutoCompleteTextView
android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search here..." />
</LinearLayout>
Q. 7. Develop android application to enter one number and display factorial of a number
once click on button.
Answer:
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

EditText input = findViewById(R.id.editText);


Button btn = findViewById(R.id.button);
TextView output = findViewById(R.id.result);

btn.setOnClickListener(v -> {
int n = Integer.parseInt(input.getText().toString());
int fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
output.setText("Factorial: " + fact);
});
}
}

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="16dp">

<EditText android:id="@+id/editText"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="Enter number" android:inputType="number" />

<Button android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Find Factorial" />

<TextView android:id="@+id/result"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="18sp" android:paddingTop="10dp" />
</LinearLayout>
Assignment No. 5
Q. 8. Write significance of SQLite database in android.

Answer:

SQLite is widely used in Android apps because it's lightweight, serverless, and comes built-in with Android
OS. Here's its importance:

Point Description
It allows apps to store structured data locally on the device without needing
1. Local Storage
internet.
2. Lightweight & Fast SQLite is a lightweight database engine with minimal setup and fast operations.
3. No Server Required Runs directly in the Android device; no need for a separate DB server.
4. Reliable & ACID Ensures data integrity even during crashes using ACID (Atomicity, Consistency,
Compliant Isolation, Durability) properties.

Example Use-Case:
Used in apps like note-taking apps, to-do lists, offline games, etc., to store user data locally.

Q. 9. What is fragment? Explain with example.

Answer:

1. A Fragment in Android is a modular section of an activity that has its own lifecycle, UI, and behavior,
and can be added or removed dynamically.
2. It helps in designing flexible and reusable components, especially for activities that need to work on
both large and small screen devices.
3. Fragments exist within the activity and must be hosted by an activity; they cannot run independently on
their own.
4. Android provides FragmentManager and FragmentTransaction classes to perform actions like adding,
removing, and replacing fragments at runtime.

Example: // ExampleFragment.java

public class ExampleFragment extends Fragment {


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_example, container, false);
}
}
// fragment_example.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="This is a Fragment!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

// In activity_main.xml

<fragment
android:name="com.example.app.ExampleFragment"
android:id="@+id/exampleFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

12. Write a program to display the list of sensors supported by device.


Answer:
import android.app.Activity;
import android.hardware.*;
import android.os.Bundle;
import android.widget.TextView;
import java.util.*;

public class MainActivity extends Activity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);


List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
TextView tv = findViewById(R.id.textView);

if (sensors != null && !sensors.isEmpty()) {


StringBuilder sb = new StringBuilder("Sensors:\n");
for (Sensor s : sensors)
sb.append(s.getName()).append("\n");
tv.setText(sb.toString());
} else tv.setText("No sensors found.");
}
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</RelativeLayout>
Assignment No. 6
Q. 4. Explain two methods of Google Map.
Answer:

• addMarker() – This method is used to add a marker (pin) at a specific location on the map. It helps highlight
a point of interest.
Example: map.addMarker(new MarkerOptions().position(location).title("My Location"));

• moveCamera() – This method moves the camera to a specified location on the map. It helps focus the view
where needed.
Example: map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));

Q. 9. Elaborate Android Security Model.


Answer:
• The Android Security Model is designed to keep user data and device resources safe by enforcing strict
boundaries between apps and the system. It is based on a combination of Linux-based security, app
sandboxing, and permission control.

• Every Android app runs in its own process and is assigned a unique user ID (UID) by the system. This
ensures application sandboxing, meaning one app cannot access the data of another app directly. Each app
is isolated from others unless it explicitly shares data using content providers, intents, or permissions.

• Android uses a permission-based model where sensitive operations (like accessing the camera, contacts, or
location) require the app to declare permissions in its manifest file. Starting from Android 6.0 (API 23),
users can also grant or deny permissions at runtime.

• The Android system also protects itself and core apps using SELinux (Security-Enhanced Linux) for
enforcing mandatory access control policies.

Key Points:
• Apps run in isolated sandboxes using unique UIDs.
• Data is private unless shared through system mechanisms.
• Permissions control access to sensitive features.
• SELinux enforces low-level system protection.
Q. 13. Write a program to send e-mail.
Answer:
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends Activity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

Button btn = findViewById(R.id.sendBtn);


btn.setOnClickListener(v -> {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:")); // only email apps handle this
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "Test Subject");
i.putExtra(Intent.EXTRA_TEXT, "This is a test email.");
startActivity(i);
});
}
}

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">

<Button android:id="@+id/sendBtn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>

You might also like