<blockquote >
<p ><b >Events are a useful way to collect data about a user's interaction with interactive components of Applications.</b>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.</p>
</blockquote>
<p >There are following three concepts related to Android Event Management −</p>
<ul class="list" >
<li >
<p ><b >Event Listeners</b>− 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.</p>
</li>
<li >
<p ><b >Event Listeners Registration</b>− 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.</p>
</li>
<li >
<p ><b >Event Handlers</b>− 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.</p>
</li>
</ul>
<h2 >Event Listeners & Event Handlers</h2>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Event Handler</th>
<th >Event Listener & Description</th>
</tr>
<tr >
<td >onClick()</td>
<td ><b >OnClickListener()</b>
<p >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.</p>
</td>
</tr>
<tr >
<td >onLongClick()</td>
<td ><b >OnLongClickListener()</b>
<p >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.</p>
</td>
</tr>
<tr >
<td >onFocusChange()</td>
<td ><b >OnFocusChangeListener()</b>
<p >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.</p>
</td>
</tr>
<tr >
<td >onKey()</td>
<td ><b >OnFocusChangeListener()</b>
<p >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.</p>
</td>
</tr>
<tr >
<td >onTouch()</td>
<td ><b >OnTouchListener()</b>
<p >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.</p>
</td>
</tr>
<tr >
<td >onMenuItemClick()</td>
<td ><b >OnMenuItemClickListener()</b>
<p >This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.</p>
</td>
</tr>
<tr >
<td >onCreateContextMenu()</td>
<td ><b >onCreateContextMenuItemListener()</b>
<p >This is called when the context menu is being built(as the result of a sustained "long click)</p>
</td>
</tr>
</tbody>
</table>
<p >There are many more event listeners available as a part of<b >View</b>class like OnHoverListener, OnDragListener etc which may be needed for your application. So I recommend to refer official documentation for Android application development in case you are going to develop a sophisticated apps.</p>
<h2 >Event Listeners Registration</h2>
<p >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. Though there are several tricky ways to register your event listener for any event, but I'm going to list down only top 3 ways, out of which you can use any of them based on the situation.</p>
<ul class="list" >
<li >
<p >Using an Anonymous Inner Class</p>
</li>
<li >
<p >Activity class implements the Listener interface.</p>
</li>
<li >
<p >Using Layout file activity_main.xml to specify event handler directly.</p>
</li>
</ul>
<p >Below section will provide you detailed examples on all the three scenarios −</p>
<h2 >Touch Mode</h2>
<p >Users can interact with their devices by using hardware keys or buttons or touching the screen.Touching the screen puts the device into touch mode. The user can then interact with it by touching the on-screen virtual buttons, images, etc.You can check if the device is in touch mode by calling the View class’s isInTouchMode() method.</p>
<p><img alt="Event" src="http://www.tutorialspoint.com/android/images/event.png" /></p>
<h4 align="center" >TOUCH EVENT</h4>
<h2 >Focus</h2>
<p >A view or widget is usually highlighted or displays a flashing cursor when it’s in focus. This indicates that it’s ready to accept input from the user.</p>
<ul class="list" >
<li >
<p ><b >isFocusable()</b>– it returns true or false</p>
</li>
<li >
<p ><b >isFocusableInTouchMode()</b>– checks to see if the view is focusable in touch mode. (A view may be focusable when using a hardware key but not when the device is in touch mode)</p>
</li>
</ul>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pln" >android</span><span class="pun" >:</span><span class="pln" >foucsUp</span><span class="pun" >=</span><span class="str" >"@=id/button_l"</span></pre>
<h2 >onTouchEvent()</h2>
<pre class="prettyprint prettyprinted" >
<span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onTouchEvent</span><span class="pun" >(</span><span class="pln" >motionEvent </span><span class="kwd" >event</span><span class="pun" >){</span><span class="pln" >
</span><span class="kwd" >switch</span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getAction</span><span class="pun" >()){</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > TOUCH_DOWN</span><span class="pun" >:</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >"you have clicked down Touch button"</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > TOUCH_UP</span><span class="pun" >:</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >"you have clicked up touch button"</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > TOUCH_MOVE</span><span class="pun" >:</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >"you have clicked move touch button"</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onTouchEvent</span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<h2 >Event Handling Examples</h2>
<h3 >Event Listeners Registration Using an Anonymous Inner Class</h3>
<p >Here you will create an anonymous implementation of the listener and will be useful if each class is applied to a single control only and you have advantage to pass arguments to event handler. In this approach event handler methods can access private data of Activity. No reference is needed to call to Activity.</p>
<p >But if you applied the handler to more than one control, you would have to cut and paste the code for the handler and if the code for the handler is long, it makes the code harder to maintain.</p>
<p >Following are the simple steps to show how we will make use of separate Listener class to register and capture click event. Similar way you can implement your listener for any other required event type.</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th class="fivepct" >Step</th>
<th >Description</th>
</tr>
<tr >
<td >1</td>
<td >You will use Android studio IDE to create an Android application and name it as<i >myapplication</i>under a package<i >com.example.myapplication</i>as explained in the<i >Hello World Example</i>chapter.</td>
</tr>
<tr >
<td >2</td>
<td >Modify<i >src/MainActivity.java</i>file to add click event listeners and handlers for the two buttons defined.</td>
</tr>
<tr >
<td >3</td>
<td >Modify the detault content of<i >res/layout/activity_main.xml</i>file to include Android UI controls.</td>
</tr>
<tr >
<td >4</td>
<td >No need to declare default string constants.Android studio takes care default constants.</td>
</tr>
<tr >
<td >5</td>
<td >Run the application to launch Android emulator and verify the result of the changes done in the aplication.</td>
</tr>
</tbody>
</table>
<p >Following is the content of the modified main activity file<b >src/com.example.myapplication/MainActivity.java</b>. This file can include each of the fundamental lifecycle methods.</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >ActionBarActivity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >ProgressDialog</span><span class="pln" > progress</span><span class="pun" >;</span><span class="pln" >
</span><span class="typ" >Button</span><span class="pln" > b1</span><span class="pun" >,</span><span class="pln" >b2</span><span class="pun" >;</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" >
setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" >
progress </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ProgressDialog</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >);</span><span class="pln" >
b1</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button</span><span class="pun" >);</span><span class="pln" >
b2</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button2</span><span class="pun" >);</span><span class="pln" >
b1</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >TextView</span><span class="pln" > txtView </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >textView</span><span class="pun" >);</span><span class="pln" >
txtView</span><span class="pun" >.</span><span class="pln" >setTextSize</span><span class="pun" >(</span><span class="lit" >25</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >});</span><span class="pln" >
b2</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >TextView</span><span class="pln" > txtView </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >textView</span><span class="pun" >);</span><span class="pln" >
txtView</span><span class="pun" >.</span><span class="pln" >setTextSize</span><span class="pun" >(</span><span class="lit" >55</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >});</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" >
getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" >
</span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" >
</span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" >
</span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" >
</span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" >
</span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<p >Following will be the content of<b >res/layout/activity_main.xml</b>file −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/tools"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" >
</span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" >
</span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >".MainActivity"</span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Event Handling "</span><span class="pln" >
</span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Tutorials point "</span><span class="pln" >
</span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >"#ff87ff09"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" >
</span><span class="atn" >android:layout_above</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_marginBottom</span><span class="pun" >=</span><span class="atv" >"40dp"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><ImageButton</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >"@drawable/abc"</span><span class="pln" >
</span><span class="atn" >android:layout_centerVertical</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Small font"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/button"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Large Font"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/button"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/button"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/button"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Hello World!"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"25dp"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></RelativeLayout></span></pre>
<p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><resources></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"app_name"</span><span class="tag" >></span><span class="pln" >myapplication</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"action_settings"</span><span class="tag" >></span><span class="pln" >Settings</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ></resources></span></pre>
<p >Following is the default content of<b >AndroidManifest.xml</b>−</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >"com.example.myapplication"</span><span class="pln" >
</span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >"1"</span><span class="pln" >
</span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >"1.0"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><uses-sdk</span><span class="pln" >
</span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >"8"</span><span class="pln" >
</span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >"22"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >"@drawable/ic_launcher"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" >
</span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >"@style/AppTheme"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><activity</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.example.myapplication.MainActivity"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><intent-filter></span><span class="pln" >
</span><span class="tag" ><action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.action.MAIN"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.category.LAUNCHER"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></intent-filter></span><span class="pln" >
</span><span class="tag" ></activity></span><span class="pln" >
</span><span class="tag" ></application></span><span class="pln" >
</span><span class="tag" ></manifest></span></pre>
<p >Let's try to run your<b >myapplication</b>application. I assume you had created your<b >AVD</b>while doing environment setup. To run the app from Android Studio, open one of your project's activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.png" />icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −</p>
<p><img alt="Android Event Handling" src="http://www.tutorialspoint.com/android/images/event1.png" /></p>
<p >Now you try to click on two buttons, one by one and you will see that font of the<b >Hello World</b>text will change, which happens because registered click event handler method is being called against each click event.</p>
<h2 >Exercise</h2>
<p >I will recommend to try writing different event handlers for different event types and understand exact difference in different event types and their handling. Events related to menu, spinner, pickers widgets are little different but they are also based on the same concepts as explained above.</p>