0% found this document useful (0 votes)
57 views6 pages

Exercise 5 - Handling Action

The document describes how to handle button click actions in Android apps. It shows how to set an onclick listener by specifying the method name in XML and also how to set an anonymous inline listener in the Java code. It provides examples of login apps to demonstrate these two approaches to handling button clicks.
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)
57 views6 pages

Exercise 5 - Handling Action

The document describes how to handle button click actions in Android apps. It shows how to set an onclick listener by specifying the method name in XML and also how to set an anonymous inline listener in the Java code. It provides examples of login apps to demonstrate these two approaches to handling button clicks.
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/ 6

Handling action

 Lab 1: Login app


On Login screen, when click Login button:
If username is “admin” and password is “12345” => display “Login successfully”.
Otherwise, display “Login failed”.

1. Create a project
- File > New > New project > Empty Views Activity > Click Next
- Project name, location, language (java), min SDK, Groovy DSL -> Click Finish
2. Handling action - 1st way: Set event name in xml file
- activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"

1
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp" />

<TextView
android:id="@+id/tvUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User name" />

<EditText
android:id="@+id/editUser"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tvPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password" />

<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="onLogin"/>

</LinearLayout>
- MainActivity.java:

2
import android.os.Bundle;

import android.view.View;
import android.widget.Button; Libraries
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

public Button myButton;


public EditText myUser; Views
public EditText myPass;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom);
return insets;
});

myButton = findViewById(R.id.btnOk); Binding Views in


myUser = findViewById(R.id.editUser); Java file and Xml
myPass = findViewById(R.id.editPassword); file
}
Handling
public void onLogin(View view){ action
if (myUser.getText().toString().equals("admin") &&
for
myPass.getText().toString().equals("12345"))
Button
{
Toast.makeText(getApplicationContext(),"Login
successful",Toast.LENGTH_SHORT).show();
}
else
{

3
Toast.makeText(getApplicationContext(),"Login
failed",Toast.LENGTH_SHORT).show();
}
}
}
3. Handling action – 2nd way: Inline anonymous listener
- Add a new activity: MainActivity1
- main_activity1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity1">

<TextView
android:id="@+id/tvLogin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login 1"
android:textSize="24sp" />

<TextView
android:id="@+id/tvUser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User name" />

<EditText
android:id="@+id/editUser1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tvPassword1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password" />

<EditText
android:id="@+id/editPassword1"
4
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btnOk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>

</LinearLayout>
- MainActivity1.java:
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
Libraries
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity1 extends AppCompatActivity {

public Button myButton1;


public EditText myUser1; Views
public EditText myPass1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main1);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom);
return insets;
});

5
//Views
Handling Binding Views in
myButton1 = findViewById(R.id.btnOk1);
action myUser1 = findViewById(R.id.editUser1); Java file and Xml
for myPass1 = findViewById(R.id.editPassword1); file
Button
Connect Views in
//Handling event
Java file and Xml
myButton1.setOnClickListener(new View.OnClickListener() {
file by id
@Override
public void onClick(View view) {
if (myUser1.getText().toString().equals("admin") &&
myPass1.getText().toString().equals("12345"))
{
Toast.makeText(getApplicationContext(),"Login
successful",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Login
failed",Toast.LENGTH_SHORT).show();
}
}
});
}
}

 Lab 2: Simple Calculator app


Add 2 activities:
- Activity 1: Handling Action – 1st way: Set event name in xml file
- Activity 2: Handling Action – 2st way: Inline anonymous listener

You might also like