ANDROID APPLICATION SOLVED UE 22, 23.
ANDROID APPLICATION SOLVED UE 22, 23.
ANDROID APPLICATION SOLVED UE 22, 23.
ANDROID APPLICATION
SOLVED
UE-2022, UE-2023.
R.F.Hyacent
manyumbaremmy@gmail.com
ANDROID APPLICATION SOLVED
UE-2022
QUESTION ONE
Explicit Intent: It's used to start a specific component within the same application,
specifying the target component's class name. For example, starting a new activity
within the same app:
startActivity(intent);
Intent intent =
newIntent(Intent.ACTION_VIEW,Uri.parse("www.example.com"));
startActivity(intent);
b. Splash Screen:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
onPause(): Called when the activity is going into the background, but before it loses
focus. It's typically used to pause or adjust ongoing activities.
onSaveInstanceState(): Called before the activity is killed or destroyed, giving it a
chance to save its state data via a Bundle. It's primarily used to store transient data
that the activity will need if it is later recreated.
b. Gradle: Gradle is an advanced build automation tool used primarily for Java
projects and Android development. It automates the process of building, testing,
publishing, and deploying software packages and other artifacts.
d. Menu: In Android, menus provide actions that affect the content of the activity.
They can be defined either statically in XML or dynamically in code. Menus typically
appear in the app bar or as a floating context menu.
<resources>
<string name="app_name">MyApp</string>
</resources>
b. Colors.xml: Similarly, colors.xml is a resource file used to store color values. It
centralizes color definitions, making it convenient to reuse and update colors across
an application. For example:
<resources>
<color name="primary_color">#6200EE</color>
<color name="accent_color">#03DAC5</color>
</resources>
c. Android Virtual Device (AVD): AVD is an emulator provided by the Android SDK
that allows developers to test their applications on various virtual device
configurations. Developers can create custom device configurations with specific
hardware profiles and system images. This helps in testing the application's
compatibility and performance across different Android devices.
d. TOAST: Toast is a small message that appears temporarily at the bottom of the
screen in Android applications. It is used to display brief notifications or feedback to
the user. For example:
Answers.
intent.putExtra("name", studentName);
intent.putExtra("region", selectedRegion);
intent.putExtra("age", studentAge);
intent.putExtra("gender", selectedGender);
intent.putExtra("subjects", selectedSubjects);
startActivity(intent);
Answer.
+ ")";
db.execSQL(CREATE_TABLE);
}
@Override
onCreate(db);
SQLiteDatabase db = this.getWritableDatabase();
values.put(COLUMN_NAME, student.getName());
values.put(COLUMN_REGION, student.getRegion());
values.put(COLUMN_AGE, student.getAge());
values.put(COLUMN_GENDER, student.getGender());
db.close();
}
Qn5.The prime purpose of a content provider is to serve as a central repository of
data where users can store and can fetch the data. The access of this repository is
given to other applications also but in a safe manner in order to serve the different
requirements of the user. Write an application that will allow users store their
details such as name and phone numbers in a content provider. The application
should also allow other applications to retrieve the stored user's information. The
application has one activity called MainActivity, two edit texts for name and phone
number and two buttons for save data and view data. The ids for the views are
etName, etPhone Number, btnSave and btnView.
Answer
• Define a Content Provider to manage the storage and retrieval of user details.
• Implement the MainActivity with EditText fields for name and phone number,
and buttons to save and view data.
• Define necessary permissions in the AndroidManifest.xml file.
• Implement functionality to save and retrieve user details using the Content
Provider.
static {
}
private UserDbHelper dbHelper;
@Override
return true;
@Nullable
@Override
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor;
switch (uriMatcher.match(uri)) {
case USERS:
break;
default:
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
@Nullable
@Override
long id;
switch (uriMatcher.match(uri)) {
case USERS:
break;
default:
getContext().getContentResolver().notifyChange(uri, null);
@Nullable
@Override
return null;
• //Implement MainActivity.java:
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.etName);
etPhoneNumber = findViewById(R.id.etPhoneNumber);
values.put(UserContentProvider.KEY_NAME, name);
values.put(UserContentProvider.KEY_PHONE_NUMBER, phoneNumber);
getContentResolver().insert(UserContentProvider.CONTENT_URI, values);
startActivity(intent);
<manifest ...>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...>
<provider
android:name=".UserContentProvider"
android:authorities="com.example.userprovider"
android:exported="true" />
</application>
</manifest>
QN 6. 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
Fragment Two are btnFragment_one and btnFragment_two respectively. Hint: use
linear layout to create a fragment container.
ANSWER
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_fragment_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="center"/>
fragment_two.xml:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_fragment_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="center"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnFragment_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One"
android:onClick="onClickFragmentOne"/>
<Button
android:id="@+id/btnFragment_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Two"
android:onClick="onClickFragmentTwo"/>
</LinearLayout>
//FragmentOne.java:
@Override
//FragmentTwo.java:
@Override
//MainActivity.java:
LinearLayout fragmentContainer;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
fragmentContainer = findViewById(R.id.fragment_container);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragmentOne)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragmentTwo)
.commit();
QN 8.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. The kind of Android component that you can use to accomplish tasks that
run in the background without the need for a visible user interface is a
Service.
ii. Below is a Java class implementation of a Service for playing music:
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
@Override
super.onCreate();
@Override
try {
mediaPlayer.setDataSource(getApplicationContext(), defaultRingtoneUri);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
return START_NOT_STICKY;
@Override
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
@Override
return null;
IN JAVA CLASS
You can start this service from any component like an Activity or another Service
using startService()
b). 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.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="text" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:layout_marginTop="16dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etEmail"
android:layout_marginTop="16dp"
android:hint="Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etPhoneNumber"
android:layout_marginTop="16dp"
android:text="Save"
android:onClick="saveCustomerInfo" />
</RelativeLayout>
//MainActivity.java
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
etPhoneNumber = findViewById(R.id.etPhoneNumber);
sharedPreferences = getSharedPreferences("customer_info",
Context.MODE_PRIVATE);
editor.putString("name", name);
editor.putString("email", email);
editor.putString("phone_number", phoneNumber);
editor.apply();
}
UE-2023
QUESTION ONE.
Choose the most correct answer and write its letter in the answer booklet provided.
(1 Mark Each)
A. Android.widget
B. Android.view.View
C. Android.view.ViewGroup
D. Android.view. Widget
A. A file that contains all information about the layout of the android application.
B. A file that contains all the information about the android application.
C. A file that contains all the information about the activities of the android
application.
iv. While developing android application developers can test their apps on:-
B. Android phone.
C. Third-party emulator.
vi. Identify among the following which is not a state in the service lifecycle.
A. Running.
B. Start.
C. Paused.
D. Destroyed.
A. DatePicker.
B. AlertDialog.
C. ProgressDialog.
A. send intent().
B. onReceive.
C. implicitBroadcast().
A. Intents.
B. Content Providers.
C. Services.
D. Applications.
A. Cupcake.
B. Gingerbread.
C. Honeycomb.
D. Android.
QUESTION 2.
Write true if the statement is correct and false if the statement is incorrect.
(a) Mobile Apps On Both Android And IOS Platforms Should Not Perform Long
Lasting Tasks, Such As Network FALSE
(b) Android is built upon the Java Micro Edition (J2ME) version of java FALSE.
(c) Native libraries is one of the core component of the .apk in android TRUE.
(d) The code which is compiled to run the android application is always contained
within the xml layout file. FALSE.
(e) The xml file that contains all the texts that your application uses is called
text.xml FALSE.
(f) There is no guarantee that an activity will be stopped prior to being destroyed.
TRUE.
(g) In an explicit intent, the sender specifies the type of receiver. FALSE.
(h) There can be only one running activity in a given time. FALSE.
(i) Java is the only programming language used to make an android application.
FALSE.
(j) The only database that android application can work with is SQLite. FALSE.
QUESTION 3.
a) Assume you have a running application that is already running an Activity called
Activity1. ActivityIstarts another Activity called
Activity2. Name one Activity lifecycle method that will be called on Activity1 after
this point, but before Activity2 starts.
onPause() method will be called on Activity1 after this point, but before
Activity2 starts.
(b) Suppose you have an application that is running an Activity called Activity1.
Suppose that Activity1 executes and starts other Activities, but that the user never
quits or backs out of the Activity. How many times can Activityl's onCreate() method
get called?
Activity1's onCreate() method can be called multiple times, but only when the
system needs to create a new instance of Activity1 due to memory constraints
or configuration changes. It can be called several times during the lifetime of
the application.
(c) Suppose that there are two activities in an application named ActivityOne and
ActivityTwo. You want to invoke ActivityTwo from ActivityOne. What code you will
write?
startActivity(intent);
(d) How will you reference a textbox control in java file, that is available in XML file
and the ID is txtName. (4 Marks)
intent.putExtra("websiteName", "www.udom.ac.tz");
startActivity(intent);
(f) How will you get the data in secondActivity? Refer to part (e)
Many students now days fail to manage loan that they get from Higher Education
Students Loan Board (HESLB). Most of them finish the money even before the
period they are supposed to use. Based on the mentioned problem, HESLB wants a
mobile application called Budget Planner that will helps their beneficiaries to
manage their money. As a mobile apps developer, you have been consulted with
HESLB to develop the Budget Planner mobile app that will do the following: receive
the amount from the student (initial balance), the student will enter the number of
days that he/she supposed to use the money. Every time the student spends a
certain amount of money the app should deduct from the initial balance. Once the
student is about to finishes the money, the app should alert him/her. The app
should not allow the student to spend more than his/her initial balance.
Answer
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
setContentView(R.layout.activity_budget_planner);
editTextInitialBalance = findViewById(R.id.editText_initial_balance);
editTextDaysLeft = findViewById(R.id.editText_days_left);
editTextSpending = findViewById(R.id.editText_spending);
textViewRemainingBalance = findViewById(R.id.textView_remaining_balance);
buttonSpend.setOnClickListener(new View.OnClickListener() {
@Override
spendMoney();
});
initialBalance =
Double.parseDouble(editTextInitialBalance.getText().toString());
daysLeft = Integer.parseInt(editTextDaysLeft.getText().toString());
remainingBalance -= spending;
} else {
showAlert("Insufficient funds!");
builder.setMessage(message)
.setCancelable(false)
dialog.dismiss();
});
alert.show();
}
QUESTION 5.
Currently, UDOM SACCOS have no tool to automate its operations as a result there
is a delay in providing services to its customers. You have been asked by the
management of UDOOM SACCOS to develop a mobile application that will store the
information of its customers. The app should store name, college, age, gender and
monthly contribution of its customers. The management want to show the reports
of how customers are contributing monthly. The management also wants to show
the dates of different events within SACCOS such as the date of annual meeting etc.
The management suggests the following features to be used: radio button for gender
and spinner for college. Use progress bar to show the monthly contributions 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 Activity Two.
(a) To send the customer's data to the second activity using the Intent class, you
would use the putExtra() method to add the data to the Intent object before starting
the second activity. Here's how you can do it:
intent.putExtra("name", customerName);
intent.putExtra("college", selectedCollege);
intent.putExtra("age", customerAge);
intent.putExtra("gender", selectedGender);
intent.putExtra("monthlyContribution", monthlyContribution);
startActivity(intent);
(b) To receive the customer's data within the second activity using the Intent
class, you would use the getIntent() method to retrieve the Intent object that
started the activity, and then use the getXXXExtra() methods to extract the data
from the Intent object. Here's how you can do it:
double monthlyContribution =
intent.getDoubleExtra("monthlyContribution", 0.0);
}
@Override
db.execSQL(SQL_CREATE_CUSTOMER_TABLE);
@Override
// Implement if needed
public void addCustomer(String name, String college, int age, String gender, double
monthlyContribution) {
SQLiteDatabase db = this.getWritableDatabase();
values.put(COLUMN_NAME, name);
values.put(COLUMN_COLLEGE, college);
values.put(COLUMN_AGE, age);
values.put(COLUMN_GENDER, gender);
values.put(COLUMN_MONTHLY_CONTRIBUTION, monthlyContribution);
db.close();
This will save the customer's details in the "customer" table of the "user"
database.
QUESTION SIX
REFEER TO UE 2022 QN 5.
QUESTION SEVEN
REFEER TO UE 2022 QN 6.