Professional Documents
Culture Documents
Department of Software Engineering: Bahria University, Islamabad Campus
Department of Software Engineering: Bahria University, Islamabad Campus
INSTRUCTIONS:
I. Write name and enrollment numbers on the question paper in the designated space only.
II. All questions are compulsory.
III. PLAGIARIZED/COPIED submissions will be considered as 0 marks and will be dealt with Academic
dishonesty/cheating policy of Examination.
Q. No. 1 2 3
CLO 1 2 3
Total Marks 9 5 6
Marks Obtained
a. Write the default constructors for implicit intent and explicit intent. You can use
Google webpage as a reference. Write the tag you have to declare in android manifest
file.
Explicit Intent:
Intent browserintent = new Intent(this, BrowserActivity.class);
startActivity(browserintent);
Implicit Intent:
Intent intent = new Intent(Intent. ACTION_VIEW,
Uri.parse(https://www.google.com));
startActivity(browserintent);
<Intent-filter> is the tag that we have to declare in manifest file. An intent filter is an
expression in an app's manifest file that specifies the type of intents that the component would
like to receive. For instance, by declaring an intent filter for an activity, you make it possible
for other apps to directly start your activity with a certain kind of intent
Page 1 of 17
%
Enrollment Number: ____________________________
b. Write a code to display a toast that shows “Programing in Android is Fun” and
explain each parameter of Toast.
Toast.makeText(getApplicationContext(),"Programming in Android is
Fun",Toast.LENGTH_SHORT).show();
Context: it is the context of the toast ie. The context where it is to be used
c. Write a java code to display day name on Toast using Switch case statement. e.g:
input MONDAY in textbox and display “Today is Monday”.
package com.example.samdq3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ediText = (EditText)findViewById(R.id.text1);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int chk=0;
String days = ediText.getText().toString().toLowerCase();
switch(days) {
case "monday":
Toast.makeText(getApplicationContext(),"Today is
Monday",Toast.LENGTH_SHORT).show();
5
Page 2 of 17
%
Enrollment Number: ____________________________
break;
case "tuesday":
Toast.makeText(getApplicationContext(),"Today is
Tuesday",Toast.LENGTH_SHORT).show();
break;
case "wednesday":
Toast.makeText(getApplicationContext(),"Today is
Wednesday",Toast.LENGTH_SHORT).show();
break;
case "thursday":
Toast.makeText(getApplicationContext(),"Today is
Thursday",Toast.LENGTH_SHORT).show();
break;
case "friday":
Toast.makeText(getApplicationContext(),"Today is Friday",
Toast.LENGTH_SHORT).show();
break;
case "saturday":
Toast.makeText(getApplicationContext(),"Today is
Saturday",Toast.LENGTH_SHORT).show();
break;
case "sunday":
Toast.makeText(getApplicationContext(),"Today is
Sunday",Toast.LENGTH_SHORT).show();
break;
case "":
Toast.makeText(getApplicationContext(),"Enter a
weekday!",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(),"Enter a valid
weekday!!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
5
Page 3 of 17
%
Enrollment Number: ____________________________
Page 4 of 17
%
Enrollment Number: ____________________________
Q: 2. Write a java code to display the following output shown in figure below: CLO 2
package com.example.q2samd;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk1=findViewById(R.id.checkBox1);
chk2=findViewById(R.id.checkBox2);
chk3=findViewById(R.id.checkBox3);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(chk1.isChecked())
{
s="iPhone check: true\n";
}
else
{
s="iPhone check: false\n";
}
Page 5 of 17
%
Enrollment Number: ____________________________
if(chk2.isChecked())
{
s+="Android Check: true\n";
}
else
{
s+="Android check: false\n";
}
if(chk3.isChecked())
{
s+="Windows Phone check: true";
}
else
{
s+="Window Phone check: false";
}
Toast
toast=Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG);
toast.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_marginTop="80dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:id="@+id/LL1">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="iPhone"
android:layout_marginBottom="20dp"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:layout_marginBottom="20dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Page 6 of 17
%
Enrollment Number: ____________________________
<Button
android:id="@+id/button"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
</LinearLayout>
</RelativeLayout>
Page 7 of 17
%
Enrollment Number: ____________________________
Q3. Examine the following scenario in which you are required to develop a
mobile application. [CLO3]
You are requested to paste android code along with output screen shots.
Bancroft Company wants a contact management system which require a sign-up with contact
details including name, country, contact number, profile image and email address of their
clients. Store all these attributes in customize ListView.
Activity_main.xml
<ImageView
Page 8 of 17
%
Enrollment Number: ____________________________
android:id="@+id/image_view"
android:layout_width="195dp"
android:layout_height="171dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="90dp"
android:layout_marginLeft="90dp"
android:layout_marginEnd="106dp"
android:layout_marginRight="106dp"
android:layout_marginBottom="502dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/btnPickImg"
android:layout_width="197dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="106dp"
android:layout_marginRight="106dp"
android:layout_marginBottom="444dp"
android:text="Select a photo" />
<LinearLayout
android:layout_width="384dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="137dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="84dp"
android:layout_height="wrap_content"
android:text="First Name:" />
<EditText
android:id="@+id/edtFirstName"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:layout_width="301dp"
android:layout_height="wrap_content"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
Page 9 of 17
%
Enrollment Number: ____________________________
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="84dp"
android:layout_height="wrap_content"
android:text="Last Name:" />
<EditText
android:id="@+id/edtLastName"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:layout_width="302dp"
android:layout_height="wrap_content"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="84dp"
android:layout_height="wrap_content"
android:text="Contact:" />
<EditText
android:id="@+id/edtContact"
android:digits="0123456789"
android:layout_width="302dp"
android:hint="03XXXXXXXXX"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:paddingTop="15dp"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:text="Email:" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="45dp"
android:layout_marginRight="45dp"
android:inputType="textEmailAddress" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
Page 10 of 17
%
Enrollment Number: ____________________________
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="84dp"
android:layout_height="wrap_content"
android:text="CNIC:" />
<EditText
android:id="@+id/edtCNIC"
android:layout_width="299dp"
android:layout_height="40dp"
android:digits="0123456789-"
android:hint="XXXXX-XXXXXXX-X"
android:maxLength="15" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="41dp">
<EditText
android:id="@+id/in_date"
android:layout_width="181dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:hint="DOB: MM-DD-YYYY" />
<Button
android:id="@+id/btn_date"
android:layout_width="172dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="35dp"
android:layout_marginEnd="202dp"
android:layout_marginRight="202dp"
android:text="SELECT Date" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="143dp"
android:layout_marginRight="143dp"
android:layout_marginBottom="26dp"
android:text="Submit" />
</RelativeLayout>
Page 11 of 17
%
Enrollment Number: ____________________________
MainActivity.java
package com.example.q3samd;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.icu.util.Calendar;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
intent.putExtra("email", email.getText().toString());
intent.putExtra("gender",
gender.getSelectedItem().toString());
intent.putExtra("country",
gender.getSelectedItem().toString());
intent.putExtra("dob", dob);
intent.putExtra("contact", contact.getText().toString());
intent.putExtra("cnic", cnic.getText().toString());
intent.putExtra("imgUri", uriImg.toString());
intent.putExtra("hobbies", getHobbies());
startActivity(intent);
}
else
Toast.makeText(MainActivity.this, "Please fill all the
fields", Toast.LENGTH_SHORT).show();
}
});
@Override
public void onTextChanged(CharSequence s, int start, int before, int
count) {
if(s.length() == 5)
{
cnic.append("-");
}
Page 13 of 17
%
Enrollment Number: ____________________________
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void setHobbies()
{
ArrayList<Integer> temp = new ArrayList<Integer>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose your Hobbies");
}
});
builder.setNegativeButton("Cancel", null);
Page 14 of 17
%
Enrollment Number: ____________________________
@RequiresApi(api = Build.VERSION_CODES.N)
private void setDate()
{
// Get Current Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
@Override
public void onDateSet(DatePicker view, int year, int
monthOfYear, int dayOfMonth) {
dob = (dayOfMonth + "-" + (monthOfYear + 1) + "-" +
year);
inDOB.setText(dob);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == 100)
{
uriImg = data.getData();
imageView.setImageURI(uriImg);
}
}
Page 15 of 17
%
Enrollment Number: ____________________________
Page 16 of 17
%
Enrollment Number: ____________________________
End Paper
Page 17 of 17