Assignment 3
1) Write and explain an event handling mechanism.
¬ Event Handlers − When an event happens and we have registered an event
listener for the event, the event listener calls the Event Handlers, which is the
method that actually handles the event.
¬ Event Listeners & Event Handlers
Event Handler Event Listener & Description
onClick() OnClickListener()
This is called when the user either clicks or touches or focuses
upon any widget like button, text, image etc. You will use
onClick() event handler to handle such event.
onLongClick() OnLongClickListener()
This is called when the user either clicks or touches or focuses
upon any widget like button, text, image etc. for one or more
seconds. You will use onLongClick() event handler to handle such
event.
onFocusChange OnFocusChangeListener()
()
This is called when the widget looses its focus ie. user goes away
from the view item. You will use onFocusChange() event handler
to handle such event.
onKey() OnFocusChangeListener()
This is called when the user is focused on the item and presses or
releases a hardware key on the device. You will use onKey() event
handler to handle such event.
onTouch() OnTouchListener()
This is called when the user presses the key, releases the key, or
any movement gesture on the screen. You will use onTouch()
event handler to handle such event.
onMenuItemCli OnMenuItemClickListener()
ck()
This is called when the user selects a menu item. You will use
onMenuItemClick() event handler to handle such event.
onCreateConte onCreateContextMenuItemListener()
xtMenu()
This is called when the context menu is being built(as the result of a
sustained "long click)
2) Create an application for demonstration of Button.
activity_main.xml
<LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the button!"
android:textSize="18sp"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = findViewById(R.id.myButton);
TextView textView = findViewById(R.id.textView);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Button Clicked!");
});
3) Write an android program to perform different arithmetic Operations.
<LinearLayout>
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"
android:layout_marginBottom="10dp"/>
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_marginBottom="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/subBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/mulBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginStart="10dp"/>
<Button
android:id="@+id/divBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginStart="10dp"/>
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
Button addBtn, subBtn, mulBtn, divBtn;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing UI components
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
addBtn = findViewById(R.id.addBtn);
subBtn = findViewById(R.id.subBtn);
mulBtn = findViewById(R.id.mulBtn);
divBtn = findViewById(R.id.divBtn);
result = findViewById(R.id.result);
// Set click listeners for buttons
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("+");
});
subBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("-");
});
mulBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("*");
});
divBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("/");
});
// Method to perform arithmetic operations
private void performOperation(String operator) {
String input1 = num1.getText().toString();
String input2 = num2.getText().toString();
if (input1.isEmpty() || input2.isEmpty()) {
Toast.makeText(this, "Please enter both numbers",
Toast.LENGTH_SHORT).show();
return;
double number1 = Double.parseDouble(input1);
double number2 = Double.parseDouble(input2);
double output = 0;
switch (operator) {
case "+":
output = number1 + number2;
break;
case "-":
output = number1 - number2;
break;
case "*":
output = number1 * number2;
break;
case "/":
if (number2 == 0) {
Toast.makeText(this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show();
return;
output = number1 / number2;
break;
result.setText("Result: " + output);