0% found this document useful (0 votes)
18 views16 pages

Question

The document outlines the development of various Android applications, including a student information app, a fragment-based UI app, a customer management app using SharedPreferences, and a music playback service. It provides code examples for sending and receiving data between activities, saving data to a database, and implementing background tasks. Additionally, it describes Android resources such as strings.xml, colors.xml, and permissions needed for SMS functionality.

Uploaded by

gozbethnyasio
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)
18 views16 pages

Question

The document outlines the development of various Android applications, including a student information app, a fragment-based UI app, a customer management app using SharedPreferences, and a music playback service. It provides code examples for sending and receiving data between activities, saving data to a database, and implementing background tasks. Additionally, it describes Android resources such as strings.xml, colors.xml, and permissions needed for SMS functionality.

Uploaded by

gozbethnyasio
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/ 16

You have been asked by the management of Dodoma Secondary School to develop a mobile

application that will store the information of the students. The app should store name, region, age,
gender and subjects of the student. The management want to show the reports of how the student
performs his/her subjects (English, Mathematics, Physics, Chemistry and Biology). The management
also wants to show the dates of different events within the school. The management suggests the
following features to be used: radio button for gender, spinner for region, and checkboxes for
subjects. Use progress bar to show the performance of the students and calendar to show the dates
of the events. Based on the given requirements, you found that the application has only two
activities i.e. ActivityOne and ActivityTwo.

a. Using the methods of Intent class show how you will send the student's data to the second
activity.

b. Using the methods of Intent class show how you will receive the student's data within the second
activity.

c. By the help of SQLiteOpenHelper class, show how you can save the student's details in a database.
Use user and student as the name of the database and table respectively.

a. To send the student's data to the second activity using Intent, you can use the putExtra method.
For example:

Copy code

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);

intent.putExtra("name", studentName);

intent.putExtra("region", studentRegion);

intent.putExtra("age", studentAge);

intent.putExtra("gender", studentGender);

intent.putExtra("subjects", studentSubjects);

startActivity(intent);

b. To receive the student's data within the second activity, you can use the getStringExtra,
getIntExtra, etc. method within the onCreate method of the second activity. For example:

Copy code

String studentName = getIntent().getStringExtra("name");

String studentRegion = getIntent().getStringExtra("region");

int studentAge = getIntent().getIntExtra("age", 0);

String studentGender = getIntent().getStringExtra("gender");

String studentSubjects = getIntent().getStringExtra("subjects");


c. To save the student's details in a database using SQLiteOpenHelper class, you need to create a
class that extends SQLiteOpenHelper.

Copy code

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "user.db";

public static final String TABLE_NAME = "student";

public static final String COL_1 = "ID";

public static final String COL_2 = "NAME";

public static final String COL_3 = "REGION";

public static final String COL_4 = "AGE";

public static final String COL_5 = "GENDER";

public static final String COL_6 = "SUBJECTS";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT, REGION TEXT, AGE INTEGER, GENDER TEXT, SUBJECTS TEXT)");

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

onCreate(db);

public boolean insertData(String name, String region, String age, String gender, String subjects) {

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put(COL_2, name);

contentValues.put(COL_3, region);

contentValues.put(COL_4, age);

contentValues.put(COL_5, gender);

contentValues.put(COL_6, subjects);

long result = db.insert(TABLE_NAME, null, contentValues);

if (result == -1)

return false;

else

return true;

You can then call the insertData method in your ActivityOne to save the student's data to the
database.

DatabaseHelper dbHelper = new DatabaseHelper(this);

boolean insertSuccess = dbHelper.insertData(studentName, studentRegion, studentAge,


studentGender, studentSubjects);

if(insertSuccess){

// show success message

}else{

// show failure message

This will insert the student's data into the student table of the user database, and you can then
retrieve the data for reporting or other purposes
Android Fragment represents a behavior or a portion of a user interface in an activity. Multiple
fragments can be combined in a single activity to build a multi-panel User Interface (UI) and reuse a
fragment in multiple activities. As a mobile apps developer, you have been asked to develop an
application with two fragments named FragmentOne and FragmentTwo. When a user click on the
specific fragment, the description of the fragment displays on the fragment container in the main
activity which hosts the fragments. Assume the ids for FragmentOne and FragmentTwo are
btnFragment one and btnFragment two respectively. Hint: use linear layout to create a fragment
container

To develop an application with two fragments named FragmentOne and FragmentTwo, you can
follow these steps:

Create two new fragments, FragmentOne and FragmentTwo, by extending the Fragment class and
overriding the onCreateView method. In the onCreateView method, you can inflate the layout for
the fragment and return the view.

Copy code

public class FragmentOne extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_one, container, false);

Create a LinearLayout as a fragment container in your main activity's layout file. This layout will be
used to display the selected fragment.

Copy code

<LinearLayout

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="match_parent">

</LinearLayout>

In your main activity's onCreate method, set onClickListeners for the buttons that correspond to
FragmentOne and FragmentTwo. In the onClick method for each button, you can create a new
instance of the corresponding fragment and use the FragmentManager and FragmentTransaction to
replace the current fragment in the fragment container with the selected fragment.

Copy code
public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btnFragmentOne = findViewById(R.id.btnFragmentOne);

btnFragmentOne.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

FragmentOne fragment = new FragmentOne();

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.fragment_container, fragment);

fragmentTransaction.commit();

});

Button btnFragmentTwo = findViewById(R.id.btnFragmentTwo);

btnFragmentTwo.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

FragmentTwo fragment = new FragmentTwo();

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.fragment_container, fragment);

fragmentTransaction.commit();

});

}
When the user clicks on the specific fragment button, the description of the fragment will display on
the fragment container in the main activity which hosts the fragments.
The management of CIVE cafeteria wants to implement an android system that will manage its
customers. The application has only one activity named MainActivity with three edit texts and one
button. One of the functionality of the application is to store the customer's information such as
name, email and phone number. The customer's data are in small amount and are in key value pairs.
Write an android application that will store the customer's information in a Shared Preference The
ids for the edit texts and button are etName, etEmail, etPhoneNumber and btnSave

To create an android application that stores customer's information in a SharedPreference, you can
follow these steps:

In the MainActivity's onCreate method, get an instance of the SharedPreference by calling the
getSharedPreferences method.

Copy code

SharedPreferences sharedPreferences = getSharedPreferences("customer_data", MODE_PRIVATE);

In the layout file of MainActivity, add three EditText views for name, email, and phone number, and
one button for saving the data.

Copy code

<LinearLayout>

<EditText

android:id="@+id/etName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Name" />

<EditText

android:id="@+id/etEmail"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Email" />

<EditText

android:id="@+id/etPhoneNumber"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Phone Number" />


<Button

android:id="@+id/btnSave"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Save" />

</LinearLayout>

In the MainActivity class, set an onClickListener for the save button. In the onClick method, you can
get the text from the EditText views, and use the putString method of the SharedPreference.Editor
to store the data in the SharedPreference.

Copy code

Button btnSave = findViewById(R.id.btnSave);

btnSave.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

EditText etName = findViewById(R.id.etName);

EditText etEmail = findViewById(R.id.etEmail);

EditText etPhoneNumber = findViewById(R.id.etPhoneNumber);

String name = etName.getText().toString();

String email = etEmail.getText().toString();

String phoneNumber = etPhoneNumber.getText().toString();

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString("name", name);

editor.putString("email", email);

editor.putString("phoneNumber", phoneNumber);

editor.apply();

Toast.makeText(MainActivity.this, "Data Saved", Toast.LENGTH_SHORT).show();

});
With this implementation, the customer's data will be stored in the SharedPreference and can be
accessed by other applications that have the same package name. You can also retrieve the data by
calling the getString method of the SharedPreference and passing in the key.

String name = sharedPreferences.getString("name", "");

String email = sharedPreferences.getString("email", "");

String phoneNumber = sharedPreferences.getString("phoneNumber", "");


There are tasks that do not really need to be visible to the user.

A good example is playing a music. You don't need to keep the player's screen for song to play.
Those kinds of applications/tasks runs in the background.

i. Suggests the kind of android component that you can use to accomplish such kind of task.

ii. Based on your suggestion in part (i), write a java class to implement a task for playing music.
Assume the music you want to play is located at Settings.System.DEFAULT_RINGTONE_URI

i. To accomplish a task that runs in the background such as playing music, you can use a Service
component in android. A Service is a component that runs in the background, independently of the
activity lifecycle and can perform long-running operations.

ii. Here is an example of a Java class that implements a task for playing music using a Service
component:

Copy code

public class MusicService extends Service {

private MediaPlayer mediaPlayer;

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Uri uri = Settings.System.DEFAULT_RINGTONE_URI;

mediaPlayer = MediaPlayer.create(this, uri);

mediaPlayer.start();

return super.onStartCommand(intent, flags, startId);

@Override

public void onDestroy() {

super.onDestroy();

mediaPlayer.stop();

@Nullable

@Override
public IBinder onBind(Intent intent) {

return null;

In this example, the MusicService class extends the Service class, and overrides the
onStartCommand method. The MediaPlayer class is used to play the music, and the create method is
used to create the MediaPlayer with the URI of the music file. The start method is used to start
playing the music, and the stop method is used to stop the music when the service is destroyed.

To start the service, you can use the startService method, passing in an intent that contains the
context and the class of the service.

Copy code

Intent serviceIntent = new Intent(this, MusicService.class);

startService(serviceIntent);

To stop the service, you can use the stopService method and pass in the same intent.

Copy code

stopService(serviceIntent);
With examples, briefly provide the explanation of the following features:-

a. String.xml

b. Colors.xml

c. Android Virtual Device (AVD) d. TOAST

c. View

a. String.xml is an XML file located in the res/values folder of an Android project. It is used to store
string resources that can be accessed from the Java code. String resources can include text for labels,
buttons, and other UI elements, as well as text for messages and other application-specific strings.
This allows for easy localization and management of strings in the application.

For example, you can define a string in strings.xml:

Copy code

<string name="hello_world">Hello World!</string>

and then you can access it in your Java code like this:

Copy code

String message = getString(R.string.hello_world);

b. Colors.xml is an XML file located in the res/values folder of an Android project. It is used to store
color resources that can be accessed from the Java code or XML layout files. This allows for easy
management of colors in the application.

For example, you can define a color in colors.xml:

Copy code

<color name="colorPrimary">#3F51B5</color>

and then you can access it in your Java code like this:

Copy code
int color = getResources().getColor(R.color.colorPrimary);

c. Android Virtual Device (AVD) is an emulator that allows developers to test and run their Android
applications on different devices and configurations without the need for physical devices.
Developers can use AVD Manager to create, configure and run virtual devices that emulate different
versions of Android and different hardware configurations.

d. TOAST is a view that can be used to display a simple message or notification to the user. Toasts
are displayed on the screen for a short period of time and automatically disappear. They can be used
to display messages such as "Data saved" or "No Internet connection". Toasts can be created and
displayed in the code using the Toast class, and the makeText method.

For example, you can display a toast in your Java code like this:

Copy code

Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show();

e. View is the base class for all UI components in Android. It represents the basic building block for
user interface components such as buttons, text fields, and images. View provides the basic layout
and drawing mechanisms, and can be extended to create custom UI components. The Android
framework provides a number of subclasses of the View class, such as TextView, Button, and
ImageView, that can be used to create common UI elements.
Name the two permissions you need to declare in your AndroidManifest.xml file for sending and
receiving SMS messages.

The two permissions you need to declare in your AndroidManifest.xml file for sending and receiving
SMS messages are:

android.permission.SEND_SMS: This permission is required to send SMS messages.

android.permission.RECEIVE_SMS: This permission is required to receive SMS messages.

You can declare these permissions in your AndroidManifest.xml file like this:

Copy code

<manifest>

<uses-permission android:name="android.permission.SEND_SMS" />

<uses-permission android:name="android.permission.RECEIVE_SMS" />

...

</manifest>
Provide a brief description of the following android project resources: -

(1 Mark Each)

a. res/mipmap

b. Gradle

c. res/drawable

d. Menu

e. Manisfests/

a. res/mipmap: The mipmap folder is used to store app icons in different resolutions. The icons
stored in this folder are used for different screen densities, and it is recommended to use this folder
instead of the res/drawable folder for app icons.

b. Gradle: Gradle is a build automation tool that is used to manage the dependencies and build
process of an Android project. It is used to define the project structure, dependencies, and tasks that
need to be executed in order to build and deploy the app. It also makes it easy to manage and
update dependencies, and automate tasks such as building, testing and deploying.

c. res/drawable: The drawable folder is used to store images and other drawable resources for an
Android project. These resources can include images, shapes, and XML files that define custom
graphics. The images stored in this folder can be used in the layout files, or accessed in the code.

d. Menu: The menu is a resource that defines the options available to the user in an Android app.
Menus are typically displayed in an app bar or action bar, and can include options such as settings,
search, and other actions. Menus can be created using XML files, and can be accessed and
manipulated in the code using the Menu and MenuItem classes.

e. Manifests: The manifests folder contains the AndroidManifest.xml file, which is a central
configuration file for an Android project. This file contains important information about the app such
as the package name, the minimum required SDK, the app's main activity, and the permissions
required by the app. This file also contains the declarations of all the activities, services, broadcast
receivers and content providers of the app.
Implicit Intents are used when you want to perform an action and you don't know which component
should handle it, while Explicit Intents are used when you want to perform an action and you know
which component should handle it.

Explicit

Intent intent = new Intent(this, SecondActivity.class);

startActivity(intent);

Implicit

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse("http://www.example.com"));

startActivity(intent);

onPause() method is used to save the current state of an activity before it is closed or obscured,
while the onSaveInstanceState() method is used to save the current state of an activity before it is
destroyed in order to restore it when the activity is recreated.

You might also like