Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Unit-4 Advanced UI

Programming
1

4/19/2023
2 Event Handling

 Events are the actions performed by the user in order to


interact with the application, for e.g. pressing a button
or touching the screen.
 The events are managed by the android framework in
the FIFO manner i.e. First In – First Out.
 Handling such actions or events by performing the
desired task is called Event Handling.

4/19/2023
3 Event Handling
 There are following three concepts related to Android Event
Management
 Event Listeners − An event listener is an interface in the View class
that contains a single callback method. These methods will be
called by the Android framework when the View to which the
listener has been registered is triggered by user interaction with the
item in the UI.
 Event Listeners Registration − Event Registration is the process by
which an Event Handler gets registered with an Event Listener so
that the handler is called when the Event Listener fires the event.
 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.

4/19/2023
4 Different Ways to Register a Listener

 Using anonymous Inner Class

 Activity class implements Listener Interace

 Using Layout file

4/19/2023
5 Event Registration using anonymous
inner class
Button button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action when button is clicked
// This code will execute when the button is clicked
}
});

4/19/2023
6 Event Registration using anonymous
inner class
 In the code above, we first find the button using its ID (R.id.button) and
store it in a variable. Then, we set an OnClickListener on the button using an
anonymous inner class.

 The OnClickListener interface has one method, onClick(), which is called


when the button is clicked. In the anonymous inner class, we override this
method and provide the code that should be executed when the button is
clicked.

4/19/2023
7 Activity class implements Listener
Interace
public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = findViewById(R.id.button);


button.setOnClickListener(this);
}

4/19/2023
8 Activity class implements Listener
Interace
@Override
public void onClick(View v) {
// Perform action when button is clicked
// This code will execute when the button is clicked
}
}
 In the code above, we implement the OnClickListener interface in our activity
by adding implements OnClickListener to the class declaration. We then
override the onClick() method, which will be called when the button is
clicked.
 We set the button's OnClickListener to this, which refers to the current instance
of the MainActivity class, since it implements the OnClickListener interface.
 In the onClick() method, we can write actual logic when button is clicked4/19/2023
9 Using Layout file

Activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
androidonClick="onButtonClick" />

 In the code above, we define a Button element in our layout file and set its
android:onClick attribute to "onButtonClick". This specifies the name of the
method that should be called when the button is clicked.

4/19/2023
10 Using Layout file
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onButtonClick(View view) {


// Perform action when button is clicked
// This code will execute when the button is clicked
}
} 4/19/2023
11 Using Layout file
 In the code above, we define a public method called onButtonClick that
takes a View parameter. This method will be called when the button is
clicked, since we specified its name in the android:onClick attribute of the
button.

 In the onButtonClick method, we can write the code that should be


executed when the button is clicked. The View parameter is the button that
was clicked, so we can use it to get information about the button or modify
its properties.

4/19/2023
Summary Table
12

Widget Event listeners Event listener registration Event handler

onFocusChange()
editText.setOnFocusChangeL
View.OnFocusChangeListener istener(listener)
EditText
beforeTextChanged()
TextWatcher editText.addTextChangedList
onTextChanged()
ener(listener)
afterTextChanged()

button.setOnClickListener(list
Button View.OnClickListener onClick()
ener)

RadioGroup. Radiogroup.setOnCheckedC onCheckedChanged()


RadioButton
OnCheckedChangeListener hangeListener(lstener)

4/20/2023
Summary Table
13

Widget Event listeners Event listener registration Event handler

CheckBox
CompoundButton.OnChecked checkBox.setOnCheckedChangeL
onCheckedChanged()
ChangeListener istener(listener)

AdapterView.OnItemSelectedListe spinner.setOnItemSelectedListener onItemSelected()


Spinner
ner (listener) onNothingSelected()

listView.setOnItemClickListener(liste
ListView AdapterView.OnItemClickListener onItemClick()
ner)

4/20/2023
14 Button
< Button android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:onClick="onButtonClick" />

In Java file :
public void onButtonClick(View view) {
// Perform some action when the button is clicked
}

4/20/2023
15 Button Program-visit Website on button click

4/20/2023
16 RadioButton
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />

4/20/2023
17 RadioButton
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />

</RadioGroup>
In Java File:
RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);

4/19/2023
18 Program---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"
tools:context="example.javatpoint.com.radiobutton.MainActivity">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">

4/19/2023
//Radio Button 1
19
<RadioButton
android:id="@+id/radioMale"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:text=" Male"
android:layout_marginTop="10dp"
android:checked="false"
android:textSize="20dp" />

4/19/2023
20 //RadioButton 2

<RadioButton
android:id="@+id/radioFemale"
android:layout_width=" match_parent "
android:layout_height="wrap_content"
android:text=" Female"
android:layout_marginTop="20dp"
android:checked="false"

android:textSize="20dp" />
</RadioGroup>
</LinearLayout>

4/19/2023
21 Program---java file
package example.javatpoint.com.radiobutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

4/19/2023
public class MainActivity extends Activity implements
22 OnCheckedChangeListener{

RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);

4/19/2023
23 Public void onCheckedChanged(RadioGroup rg, int id)
{
switch(id){
case R.id. radioMale:
Toast.makeText(this,”Male”,Toast.LENGTH_LONG).show();
case R.id. radioFemale:

Toast.makeText(this,”Female”,Toast.LENGTH_LONG).show();

}
}

4/19/2023
24 EditText –program

4/19/2023
25 EditText –program

4/20/2023
26 CheckBox
<CheckBox
android:id="@+id/myCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the terms and conditions" />

In Java file :

CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);


if (myCheckBox.isChecked()) {
// Perform some action when the checkbox is checked
}
4/20/2023
27 CheckBox

4/20/2023
28 CheckBox

4/20/2023
29 Spinner

4/20/2023
30 Spinner

4/20/2023
31 Spinner

4/20/2023
32 ListView using XML file
 First Create listview in XML file
 Code in java File

4/20/2023
33 ListView using XML file

4/20/2023
34 ListView using ListActivity class
 Code in java File

4/20/2023
35 ListView using ListActivity

4/20/2023
36 Date Time Picker

4/20/2023
37 Date Time Picker

4/20/2023
38 GTU Questions
 Write a code for android application that changes the text size of TextView
from 10 to 24, when button is clicked. (4 M)
 Write a code for android application that display the text entered in
EditText in the Toast, when button is clicked. (4 M)
 Explain event handling using anonymous inner class method.(4M)
 Explain different android application priorities.(3M)
 Explain Toast with example.(4M)
 Write a code for android application that create login module & display
appropriate message on second activity as "Login Successful" or "Login
Failed", when user submit userid and password.(7M)
 Explain List View with example.(4M)
 Explain ArrayAdapter.(4M)

4/19/2023
39 GTU Questions
 Give an example to change value of TextView inside your Java Code.(3 M)
 Create an application which will accept a string through EditText and
display it via Toast. (4 M)
 Create LOGIN application where you have to validate password. Till the
password is not validated login button should remain disabled.(4M)
 Create an Android application to demonstrate User Defined Toast (4M)
 Create a Login application which takes username and password from user.
Whenever password is admin, redirect to second activity and display
Welcome User message to second activity, otherwise toast message Invalid
Credentials.(7M)
 What is ArrayAdapter? How to implement ListView using
ArrayAdapter?(3M)

4/19/2023
40 GTU Questions
 Create an android application which takes one message from user in
EditText.On click of button toast the same message in the same activity.(3 M)
 Create an android application to change text size of TextView on click of
button. (4 M)
 List different ways of event handling techniques. Explain any one with
example(4M)
 Create a Mobile Apps to perform Addition Operation. (3M)
 Create an Android application to demonstrate Text Change Event.(4M)
 What is Toast? How to positioning Toast for making customized?(3M)
 Explain the concept of Event Driven programming in Android?(3M)
 Create an application with following criteria (1) Screen 1(First Screen) accept
unit price and quantity purchased by customer for 2 different products using
EditText. (2) Screen 1 has button “PAY”. When this button is pressed Screen
2(Second Screen) will appear.(7M)

4/19/2023

You might also like