Professional Documents
Culture Documents
Lab4 Spring22 05042022 112721am 16032023 024112pm
Lab4 Spring22 05042022 112721am 16032023 024112pm
Mobile Devices
Lab No. 4
Basic Event Handling
Objectives
Introduction
In android application we have two important files Layout files or .xml file and Activity or .java
files. Xml files are used to design a UI for our app whereas .java file is used to write
functionality or logics. By default, we have MainActivity.java files under the Java folder in
project directory and activity_main.xml file under layout folder of res directory. To clearly
understand open android studio, create new project and apply following Examples:
Buttons
A button consists of text or an icon (or both text and an icon) that communicates what action
occurs when the user touches it.
Depending on whether you want a button with text, an icon, or both, you can create the button in
your layout in three ways:
To define the click event handler for a button, add the android:onClick attribute to the <Button>
element in your XML layout. The value for this attribute must be the name of the method you
want to call in response to a click event. The Activity hosting the layout must then implement the
corresponding method.
The method you declare in the android:onClick attribute must have a signature exactly as shown
above. Specifically, the method must:
Be public
Return void
Define a View as its only parameter (this will be the View that was clicked)
EXAMPLE 01:
.xml Code:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:onClick="sendMessage"/>
.java Code:
package com.example.basiceventhandling;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
A toast provides simple feedback about an operation in a small popup. It only fills the amount of
space required for the message and the current activity remains visible and interactive. Toasts
automatically disappear after a timeout.
For example, clicking Send on an email triggers a "Sending message..." toast, as shown in the
following screen capture:
First, instantiate a Toast object with one of the makeText() methods. This method takes three
parameters: the application Context, the text message, and the duration for the toast. It returns a
properly initialized Toast object. You can display the toast notification with show(), as shown in
the following example:
.Java File:
import android.widget.Toast;
A standard toast notification appears near the bottom of the screen, centered horizontally. You
can change this position with the setGravity(int, int, int) method. This accepts three parameters: a
Gravity constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left corner, you can set the
gravity like this:
T.setGravity(0,0,0);
Using an OnClickListener:
You can also declare the click event handler programmatically rather than in an XML layout.
This might be necessary if you instantiate the Button at runtime or you need to declare the click
behavior in a Fragment subclass.
To declare the event handler programmatically, create an View.OnClickListener object and
assign it to the button by calling setOnClickListener(View.OnClickListener). For example:
.xml Code:
<Button
android:id="@+id/button_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="My Button 1"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="15sp"
/>
.java Code:
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getButtonText = (Button) findViewById(R.id.button_text1);
getButtonText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Result: " +
getButtonText.getText().toString(), Toast.LENGTH_LONG).show();
}
});
Time Boxing
Activity Name Activity Time Total Time
Login Systems + Setting up android studio 3 mints + 5 mints 8 mints
Environment
Objectives/Outcomes
The purpose of this lab is to familiarize with basic of Android studio IDE
• Understanding Linear Layout
2. Create an application having three buttons. Bind those buttons with the same callback
method. On pressing any button, identify which button was pressed.
3. Create an application which takes a string message from user and create a toast of it on
pressing button. Your GUI must look as follows:
4. Create a calculator application for two integers. The output must look as follows