0% found this document useful (0 votes)
26 views22 pages

MAD-Lec 7 Chronometer Listners and Dialogue Box and Menu

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)
26 views22 pages

MAD-Lec 7 Chronometer Listners and Dialogue Box and Menu

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/ 22

Android

Application Development
Samreen Razzaq
Chronometer
In Android, Chronometer is a class that
implements a simple timer. Chronometer is
a subclass of TextView. This class helps us
to add a timer in our app. You can give
Timer start time in the elapsedRealTime ()
timebase and it start counting from that.
Event Listeners
Events are a useful way to collect data
about a user's interaction with interactive
components of Applications. Like button
presses or screen touch etc. The Android
framework maintains an event queue as
first-in, first-out (FIFO) basis. You can
capture these events in your program and
take appropriate action as per requirements.
• 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.
Event Listeners & Event
Handlers
Event Handler Event Listener & Description
OnClickListener()
This is called when the user either clicks or touches or focuses upon any widget like button, text,
onClick()
image etc. You will use onClick() event handler to handle such event.

OnLongClickListener()
This is called when the user either clicks or touches or focuses upon any widget like button, text,
onLongClick() image etc. for one or more seconds. You will use onLongClick() event handler to handle such
event.

OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes away from the view item. You will
onFocusChange()
use onFocusChange() event handler to handle such event.

OnFocusChangeListener()
This is called when the user is focused on the item and presses or releases a hardware key on
onKey()
the device. You will use onKey() event handler to handle such event.

OnTouchListener()
This is called when the user presses the key, releases the key, or any movement gesture on the
onTouch()
screen. You will use onTouch() event handler to handle such event.

OnMenuItemClickListener()
onMenuItemClick() This is called when the user selects a menu item. You will use onMenuItemClick() event handler
to handle such event.
onCreateContextMenuItemListener()
onCreateContextMenu() This is called when the context menu is being built(as the result of a sustained "long click)
@Override
@Override is a Java annotation. It tells the
compiler that the following
method overrides a method of its
superclass.
Example

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Submit"
android:layout_gravity="center"
android:onClick=" "/>
package com.hybrid.myapplication;

public class MainActivity extends AppCompatActivity {

@Override
Button btn;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Registarion
btn = findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener(){

@Override

public void onclick(View v){

Toast.makeText(MainActivity.this, "Btn click", Call back


Toast.LENGTH_SHORT).show();
method
}

});

}
}
Alert Dialogue
package com.hybrid.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void display(View v){

AlertDialog.Builder alert = new AlertDialog.Builder(this);


alert .setTitle("Warning");
alert.setMessage("Do you wany to continue:");
alert.setIcon(R.drawable.ic_launcher_foreground);
alert.setCancelable(false);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "ok clicked", Toast.LENGTH_SHORT).show();

}
});
alert.setNegativeButton(“Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "cancelled clicked", Toast.LENGTH_SHORT).show();

}
});
alert.show();

}
}
PASS view in alert
Code for New Res Layout File

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
LayoutInflater
•LayoutInflater is used to manipulate Android screen using predefined XML
layouts.
•This class is used to instantiate layout XML file into its corresponding View
objects.
•The LayoutInflater class is used to instantiate the contents of layout XML
files into their corresponding View objects. In other words, it takes an XML
file as input and builds the View objects from it.

LayoutInflater inf = (this).getLayoutInflater();


View v = inf.inflate(R.layout.edit_dialog, null);
alert.setView(v);
Menu
1. Click on
Write name for Directory
Click on menu
Name the file
Make items and writer title for item and id.

<item android:id="@+id/i1"
android:title="sargodha"/>
Menu -popup
public void display(View v)
{
PopupMenu pop = new PopupMenu(MainActivity.this, v);
pop.getMenuInflater().inflate(R.menu.menu_list, pop.getMenu());
pop.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {

return false;
}
});

You might also like