0% found this document useful (0 votes)
38 views13 pages

Part A: 2020 March

Bachelor of Computer Application, Semester 6, Android
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)
38 views13 pages

Part A: 2020 March

Bachelor of Computer Application, Semester 6, Android
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/ 13

Part A

Module 2 : Android

2020 March

4. List any four child classes of TextView.


● EditText is a subclass of TextView that provides an editable text field for user input,
such as text or numeric values.

● Button is a subclass of TextView that displays a clickable button with text or an icon
to perform a specific action when pressed.

● CheckBox is a subclass of TextView that provides a checkable option with text to


allow the user to select one or more options from a list.

● Switch is a subclass of TextView that provides a toggle button with text to allow the
user to turn a setting on or off.

5. Write any four properties of List view.


● android:entries is a property that sets an array of string values as the data source
for the ListView.

● android:layout_height and android:layout_width are properties that define the


height and width of the ListView in pixels or as a relative or absolute value.

● android:divider is a property that sets the color of a drawable resource used to draw
a divider line between items in the ListView.

● android:choiceMode is a property that specifies the selection mode for the ListView,
which can be set to singleChoice, multipleChoice, or none to allow or disallow item
selection.

2021 April

4. Why is ‘Id’ important for UI controls in Android?


The 'Id' attribute is important for UI controls in Android because it uniquely identifies the
control within the application. The 'Id' is used to reference the control in the application code,
such as setting its properties or handling user events. Without a unique 'Id', it would be
difficult to differentiate between multiple UI controls and manipulate them programmatically.
5. How do we set up an AutoCompleteTextView?
To set up an AutoCompleteTextView in Android, first, we need to create a data source for
the suggestions. Next, we need to set up an ArrayAdapter with the data source and attach it
to the AutoCompleteTextView. Finally, we need to set the threshold value for the minimum
number of characters needed to trigger the suggestions.

2022 April

4. What is the use of Grid view?


GridView is used to display items in a grid-like format, with a fixed number of columns. It is
useful when you want to show a large number of images or data in a grid-like format.
GridView is flexible, customizable, and easy to use, making it a popular choice for displaying
collections of items in Android applications.

5. List any four properties of DatePicker


Four properties of the DatePicker in Android are:

1. android:minDate: This property sets the minimum selectable date in the


DatePicker.
2. android:maxDate: This property sets the maximum selectable date in the
DatePicker.
3. android:spinnersShown: This property determines whether the DatePicker
displays a set of spinners for date selection or a calendar view.
4. android:calendarViewShown: This property determines whether the DatePicker
displays a calendar view for date selection.

These properties can be used to customize the behavior and appearance of the DatePicker
in an Android application.
Part B
Module 2 : Android

2020 March

15. Explain any five attributes of Checkbox.


Sure. Here are five attributes of Checkbox in Android:

● android:checked: This attribute specifies whether the checkbox is checked or not. If


the checkbox is checked, the attribute will have the value true. If the checkbox is
unchecked, the attribute will have the value false.
● android:drawable: This attribute specifies the drawable to use as the icon for the
checkbox.
● android:text: This attribute specifies the text to display next to the checkbox.
● android:onClick: This attribute specifies the method to call when the checkbox is
clicked.
● android:visibility: This attribute specifies whether the checkbox is visible or not. If
the checkbox is not visible, the attribute will have the value gone. If the checkbox is
visible, the attribute will have the value visible.

16. Demonstrate the use of ArrayAdaptor in Spinner control


with code.
Sure. Here is an example of how to use ArrayAdapter in Spinner control in Android:

// Create an array of strings


String[] colors = {"Red", "Blue", "White", "Yellow", "Black", "Green",
"Purple", "Orange", "Grey"};

// Create an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, colors);

// Set the adapter to the spinner


spinner.setAdapter(spinnerArrayAdapter);

In this example, the spinner will display a list of the colors in the array. When the user
selects a color, the value of the spinner will be set to that color.
2021 April

15. Write a detailed note on the Progress Bar with syntax and
examples.
Progress Bar is a visual representation of the progress of a task. In Android, it is used to
indicate the progress of an operation, such as loading data or uploading a file. To use a
Progress Bar in your Android app, you need to add the ProgressBar view to your layout XML
file. You can customize the appearance of the Progress Bar using attributes such as color,
size, and style.

Example:

XML layout code:


<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
/>
In the above code, we have added a Progress Bar view with an ID of "progressBar" to our
layout. The indeterminate attribute is set to true, which means the Progress Bar will
continuously animate until stopped.

Java code:
ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE); // show the Progress Bar
progressBar.setVisibility(View.GONE); // hide the Progress Bar
In the above code, we have obtained a reference to the Progress Bar view using its ID and
then set its visibility to either VISIBLE or GONE based on our requirements.

16. Write a short note on image view and scroll view.


ImageView is a UI control in Android that allows developers to display an image in their app.
It can be used to display images from resources or from a web URL. Developers can
customize the size, scale type, and other attributes of the ImageView to fit their app's needs.

ScrollView is a UI control in Android that allows developers to create a scrollable view when
the content inside a layout is too large to fit on the screen. ScrollView can contain only one
direct child element, but that child can be a complex layout that contains multiple elements.
Developers can also customize the scroll behavior and appearance of the ScrollView using
various attributes.
Both ImageView and ScrollView are widely used in Android app development for displaying
images and creating scrollable views respectively. By understanding their usage and
customization options, developers can create more engaging and user-friendly apps.

2022 April

15. Why do we use Toast alert? Explain with an example.


Toast alerts are used to show a quick message to the user for a short duration of time,
typically less than 5 seconds. This is useful for providing the user with feedback or a
notification without interrupting their current task. For example, when the user clicks on a
button, a Toast alert can be displayed to show that the action has been completed.

Here is an example of how to display a Toast alert in Android:


Toast.makeText(getApplicationContext(), "Button Clicked",
Toast.LENGTH_SHORT).show();

In this example, we are creating a Toast alert with the message "Button Clicked" and a
duration of LENGTH_SHORT (which is usually 2-3 seconds). The
getApplicationContext() method is used to get the current context of the application,
and the show() method is used to display the Toast alert.

16. Create an application that will pass one number to the next
screen, and on the next screen shows the double of that
number.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private EditText editTextNumber;
private Button buttonSubmit;

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

editTextNumber = findViewById(R.id.editTextNumber);
buttonSubmit = findViewById(R.id.buttonSubmit);

buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int number =
Integer.parseInt(editTextNumber.getText().toString());
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("number", number);
startActivity(intent);
}
});
}
}

SecondActivity.java:
public class SecondActivity extends AppCompatActivity {
private TextView textViewDouble;

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

textViewDouble = findViewById(R.id.textViewDouble);

int number = getIntent().getIntExtra("number", 0);


int doubledNumber = number * 2;
textViewDouble.setText("The doubled number is: " +
doubledNumber);
}
}

In this example, we create an EditText and a Button in the main activity layout. When the
button is clicked, we get the number from the EditText and pass it to the SecondActivity
using an Intent. In the SecondActivity, we receive the number using getIntent() and double it.
Finally, we display the doubled number in a TextView.
Part C
Module 2 : Android

2020 March

23. Explain the following with syntax and suitable examples: a)


Custom toast alert b) Time and date picker c) Image view and
scroll view.
a) Custom Toast Alert: A toast is a small pop-up message that appears on the screen to
inform the user about some activity. A custom toast alert allows you to display a customized
message with a specific design or layout.

Syntax:
Toast toast = Toast.makeText(context, message, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(yourCustomView);
toast.show();

Example:
// create a custom toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.custom_toast_container));
TextView text = layout.findViewById(R.id.text);
text.setText("Custom toast message");

// create and show the custom toast


Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

b) Time and Date Picker: A time and date picker allows the user to select a specific date or
time using a graphical interface. This can be useful in applications that require users to
schedule appointments or
events.

Syntax:
// create a date picker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(context,
listener, year, month, day);

// create a time picker dialog


TimePickerDialog timePickerDialog = new TimePickerDialog(context,
listener, hour, minute, is24HourFormat);

Example:
// create a date picker dialog
DatePickerDialog datePickerDialog = new
DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int
monthOfYear, int dayOfMonth) {
// update text view with selected date
textView.setText(dayOfMonth + "/" + (monthOfYear + 1) +
"/" + year);
}
}, year, month, day);
datePickerDialog.show();

// create a time picker dialog


TimePickerDialog timePickerDialog = new
TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int
minute) {
// update text view with selected time
textView.setText(hourOfDay + ":" + minute);
}
}, hour, minute, false);
timePickerDialog.show();

c) Image View and Scroll View: An image view is a UI component that displays an image
on the screen. A scroll view is a container that allows the user to scroll through a larger UI
component that doesn't fit entirely on the screen.

Syntax:
// create an image view
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.my_image);
// create a scroll view
ScrollView scrollView = findViewById(R.id.scrollView);
scrollView.addView(yourLargeUIComponent);

Example:
<!-- create an image view in XML layout -->
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/my_image" />

<!-- create a scroll view in XML layout -->


<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- add your large UI component here -->


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud
2021 April

23. Write java and xml code to find total price of a fruit when
fruits name and quantity are entered. Keep the unit price of
fruits in memory and an autocomplete text view is used to enter
the fruit name.
Java and XML code to find the total price of a fruit when the fruit name and quantity are
entered:

MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private String[] fruits = {"Apple", "Banana", "Cherry", "Grape",
"Orange", "Peach", "Pear", "Pineapple", "Watermelon"};
private double[] unitPrices = {0.5, 0.3, 0.8, 1.2, 0.6, 1.0, 0.9,
1.5, 2.0};

private AutoCompleteTextView autoCompleteTextView;


private EditText editTextQuantity;
private TextView textViewTotalPrice;

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

autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
editTextQuantity = findViewById(R.id.editTextQuantity);
textViewTotalPrice = findViewById(R.id.textViewTotalPrice);

// create an adapter for the autocomplete text view


FruitArrayAdapter adapter = new FruitArrayAdapter(this, fruits);
autoCompleteTextView.setAdapter(adapter);
}

public void calculateTotalPrice(View view) {


String fruitName = autoCompleteTextView.getText().toString();
int quantity =
Integer.parseInt(editTextQuantity.getText().toString());
double unitPrice = 0;

// find the unit price of the fruit


for (int i = 0; i < fruits.length; i++) {
if (fruits[i].equals(fruitName)) {
unitPrice = unitPrices[i];
break;
}
}

double totalPrice = unitPrice * quantity;

textViewTotalPrice.setText("$" + String.format("%.2f",
totalPrice));
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Fruit Name" />

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Total Price"
android:onClick="calculateTotalPrice"/>

<TextView
android:id="@+id/textViewTotalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:paddingTop="16dp"/>
</LinearLayout>

In this example, an AutoCompleteTextView is used to enter the name of the fruit, and an
EditText is used to enter the quantity. The unit price of each fruit is stored in an array in
memory. When the "Calculate Total Price" button is clicked, the calculateTotalPrice
method is called, which finds the unit price of the fruit based on the name.

2022 April

23. Explain the following: a. Check Box b. Radio Button c.


Radio Group d.Toggle Button e. Progress Bar
a. Check Box:
A checkbox is a UI component that allows the user to select one or more options from a set
of options. A checkbox has two states, checked and unchecked, and the user can switch
between these states by tapping on the checkbox. In Android, a checkbox is implemented
using the CheckBox class. The state of a checkbox can be checked programmatically using
the isChecked() method.

b. Radio Button:
A radio button is a UI component that allows the user to select one option from a set of
mutually exclusive options. Radio buttons are usually used in a Radio Group, which is a
container that groups together multiple radio buttons. In Android, a radio button is
implemented using the RadioButton class. The state of a radio button can be checked
programmatically using the isChecked() method.

c. Radio Group:
A radio group is a container that groups together multiple radio buttons. A radio group
ensures that only one radio button can be selected at a time, and all other radio buttons in
the group are deselected. In Android, a radio group is implemented using the RadioGroup
class.
d. Toggle Button:
A toggle button is a UI component that allows the user to switch between two states, on and
off, with a single tap. A toggle button has a visual indicator that shows the current state, and
the user can switch between the states by tapping on the button. In Android, a toggle button
is implemented using the ToggleButton class.

e. Progress Bar:
A progress bar is a UI component that displays the progress of a task that is being executed
in the background. A progress bar has a visual indicator that shows the progress of the task,
and the user can see how much of the task has been completed. In Android, a progress bar
is implemented using the ProgressBar class. The progress of a progress bar can be set
programmatically using the setProgress() method.

You might also like