Part A: 2020 March
Part A: 2020 March
Module 2 : Android
2020 March
● Button is a subclass of TextView that displays a clickable button with text or an icon
to perform a specific action when pressed.
● Switch is a subclass of TextView that provides a toggle button with text to allow the
user to turn a setting on or off.
● 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
2022 April
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
// Create an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, colors);
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:
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.
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
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);
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
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");
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);
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();
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" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
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;
@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);
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
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.