How Activities Interact With Users In Android Applications

Overview

The following links are very useful to start programming in Android Applications:

  1. Android Programming - Day One 
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application
  17. Activity Behavior When Screen Orientation Changes in Android Application 
  18. Persevere with State Information during Changes in Configuration
  19. Detect Orientation Changes and Controlling the Orientation of the Activity
  20. Utilizing the Action Bar in Android Application
  21. Activities Interact with User in Android Application

Introduction

In this article, you will learn about how activities interact with the user. There are two levels when the user interacts with the Android Application UI and they are given below.

  1. Activity Level
  2. View Level

At the activity level, the Activity class exposes the methods that you can override. In case of View level, it can fire the events when the users interact with the views.

In the Activity level, there are some methods that you can override in the activities, which include onKeyDown and onKeyUp methods.

  • onKeyDown methods is called, when a key is pressed and not handled by any of the views contained within the activity.
  • onKeyUp is called when a key is released and not handled by any of the views contained with the activity.

Implementation

Create a new project by selecting File->New->New Project.


  1. Activity Level
    Implement onKeyDown method in the code given below.
    1. package com.example.administrator.activitiesinteraction;  
    2. import android.app.Activity;  
    3. import android.support.v7.app.ActionBarActivity;  
    4. import android.os.Bundle;  
    5. import android.view.KeyEvent;  
    6. import android.view.Menu;  
    7. import android.view.MenuItem;  
    8. import android.widget.Toast;  
    9. public class MainActivity extends Activity {  
    10.     @Override  
    11.     protected void onCreate(Bundle savedInstanceState) {  
    12.         super.onCreate(savedInstanceState);  
    13.         setContentView(R.layout.activity_main);  
    14.     }  
    15.     @Override  
    16.     public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {  
    17.         switch (keyCode) {  
    18.             case KeyEvent.KEYCODE_DPAD_CENTER:  
    19.                 Toast.makeText(getBaseContext(), "Center is clicked", Toast.LENGTH_LONG).show();  
    20.                 break;  
    21.             case KeyEvent.KEYCODE_DPAD_LEFT:  
    22.                 Toast.makeText(getBaseContext(), "Left arrow is clicked", Toast.LENGTH_LONG).show();  
    23.                 break;  
    24.             case KeyEvent.KEYCODE_DPAD_RIGHT:  
    25.                 Toast.makeText(getBaseContext(), "Right arrow is clicked", Toast.LENGTH_LONG).show();  
    26.                 break;  
    27.             case KeyEvent.KEYCODE_DPAD_UP:  
    28.                 Toast.makeText(getBaseContext(), "Up Arrow is clicked", Toast.LENGTH_LONG).show();  
    29.                 break;  
    30.             case KeyEvent.KEYCODE_DPAD_DOWN:  
    31.                 Toast.makeText(getBaseContext(), "Down Arrow is clicked", Toast.LENGTH_LONG).show();  
    32.                 break;  
    33.         }  
    34.         return false;  
    35.     }  
    36.     @Override  
    37.     public boolean onCreateOptionsMenu(Menu menu) {  
    38.         // Inflate the menu; this adds items to the action bar if it is present.  
    39.         getMenuInflater().inflate(R.menu.menu_main, menu);  
    40.         return true;  
    41.     }  
    42.     @Override  
    43.     public boolean onOptionsItemSelected(MenuItem item) {  
    44.         // Handle action bar item clicks here. The action bar will  
    45.         // automatically handle clicks on the Home/Up button, so long  
    46.         // as you specify a parent activity in AndroidManifest.xml.  
    47.         int id = item.getItemId();  
    48.         //noinspection SimplifiableIfStatement  
    49.         if (id == R.id.action_settings) {  
    50.             return true;  
    51.         }  
    52.         return super.onOptionsItemSelected(item);  
    53.     }  
    54. }  
  1. View Level
    Views also fire the events when the user interacts with them. When a user touches a Button view, you need to Service the event, so that the appropriate action can be performed. For this, you will have to explicitly register the events for the views.

Here, we have two Button views, Display and Cancel. We can register the button click events, using an anonymous class.

Add reference of OnClickListener class.

  1. import android.view.View.OnClickListener;  
  2. Create an anonymous class to use as a listener when button is clicked  
  3. //---Create an anonymous class to play as a listener when button is clicked  
  4. private OnClickListener btnListener = new OnClickListener() {  
  5.     @Override  
  6.     public void onClick(View view) {  
  7.         Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();  
  8.     }  
  9. };  

Call above anonymous class, as shown below.

  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.activity_main);  
  5.     Button buttonDisplay1 = (Button) findViewById(R.id.buttonDisplay);  
  6.     buttonDisplay1.setOnClickListener(btnListener);  
  7.     Button buttonCancel1 = (Button) findViewById(R.id.buttonCancel);  
  8.     buttonCancel1.setOnClickListener(btnListener);  
  9. }  

Run the Application by pressing F11 button. Click on either Display or Cancel button, the appropriate message will be displayed, as shown below.


It shows that the event is correctly wired up. This is useful, if you have multiple views which will be handled by one event handler. If you have a single view in this case, you can modify the anonymous class for both the Buttons and it can be rewritten, as shown below.

  1. Button button1 = (Button) findViewById(R.id.buttonDisplay);  
  2. button1.setOnClickListener(new View.OnClickListener() {  
  3.     public void onClick(View view) {  
  4.         Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();  
  5.     }  
  6. });  
  7. Button button2 = (Button) findViewById(R.id.buttonCancel);  
  8. button1.setOnClickListener(new View.OnClickListener() {  
  9.     public void onClick(View view) {  
  10.         Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();  
  11.     }  
  12. });  

We can also define an anonymous inner class to handle an event. We can implement OnFocusChange() method for the EditText view.

  1. EditText editText1 = (EditText) findViewById(R.id.txtName);  
  2. editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {  
  3.     @Override  
  4.     public void onFocusChange(View view, boolean b) {  
  5.         Toast.makeText(getBaseContext(), ((EditText) view).getId() + " has got focus - " + b, Toast.LENGTH_LONG).show();  
  6.     }  
  7. });  

Run the Application by pressing F11 button and get the result, as shown below.


Explanation

In the case of Activity level, whenever you press any keys on your mobile device, the view that currently has the focus will try to handle the generated event. When the EditText has the focus and you press a key, the EditText view handles the event and displays the character you have just entered in the view. If you press the up or down directional arrow key, the EditText view does not handle this. In this case, the onKeyDown() method is called. If we observe carefully, it is seen that focus is also transferred to the next view, which is DISPLAY button. If the EditText view already has some text in it and the cursor is at the end of the text, then clicking left arrow key does not fire the onKeyDown() method, it simply moves the cursor one character to the left.

If you press the right arrow key, then the onKeyDown() method will be called because now the EditText view will not be handling the event. This is true when the cursor is at the beginning of the EditText view. Clicking the left arrow will fire the onKeyDown() method, whereas clicking the right arrow will simply move the cursor one character to the right.

Conclusion

In this article, you learned how activities interact with users in Android Applications. In the next article, I will explain about designing user interface with views in Android applications.


Similar Articles