0% found this document useful (0 votes)
10 views8 pages

AP Assignment 3

The document outlines an event handling mechanism in Android, detailing various event handlers and listeners such as onClick, onLongClick, and onTouch. It provides code examples for creating a simple application with a button that updates a TextView when clicked, as well as an application for performing arithmetic operations using buttons and EditTexts. The code snippets demonstrate how to set up UI components and handle user interactions effectively.

Uploaded by

akar080928
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)
10 views8 pages

AP Assignment 3

The document outlines an event handling mechanism in Android, detailing various event handlers and listeners such as onClick, onLongClick, and onTouch. It provides code examples for creating a simple application with a button that updates a TextView when clicked, as well as an application for performing arithmetic operations using buttons and EditTexts. The code snippets demonstrate how to set up UI components and handle user interactions effectively.

Uploaded by

akar080928
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/ 8

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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy