Overview
The following links are very useful to start programming in Android Applications:
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.
- Activity Level
- 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.
- Activity Level
Implement onKeyDown method in the code given below.
- package com.example.administrator.activitiesinteraction;
- import android.app.Activity;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_CENTER:
- Toast.makeText(getBaseContext(), "Center is clicked", Toast.LENGTH_LONG).show();
- break;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- Toast.makeText(getBaseContext(), "Left arrow is clicked", Toast.LENGTH_LONG).show();
- break;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- Toast.makeText(getBaseContext(), "Right arrow is clicked", Toast.LENGTH_LONG).show();
- break;
- case KeyEvent.KEYCODE_DPAD_UP:
- Toast.makeText(getBaseContext(), "Up Arrow is clicked", Toast.LENGTH_LONG).show();
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- Toast.makeText(getBaseContext(), "Down Arrow is clicked", Toast.LENGTH_LONG).show();
- break;
- }
- return false;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- 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.
- import android.view.View.OnClickListener;
- Create an anonymous class to use as a listener when button is clicked
-
- private OnClickListener btnListener = new OnClickListener() {
- @Override
- public void onClick(View view) {
- Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();
- }
- };
Call above anonymous class, as shown below.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button buttonDisplay1 = (Button) findViewById(R.id.buttonDisplay);
- buttonDisplay1.setOnClickListener(btnListener);
- Button buttonCancel1 = (Button) findViewById(R.id.buttonCancel);
- buttonCancel1.setOnClickListener(btnListener);
- }
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.
- Button button1 = (Button) findViewById(R.id.buttonDisplay);
- button1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();
- }
- });
- Button button2 = (Button) findViewById(R.id.buttonCancel);
- button1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Toast.makeText(getBaseContext(), ((Button) view).getText() + " is clicked", Toast.LENGTH_LONG).show();
- }
- });
We can also define an anonymous inner class to handle an event. We can implement OnFocusChange() method for the EditText view.
- EditText editText1 = (EditText) findViewById(R.id.txtName);
- editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View view, boolean b) {
- Toast.makeText(getBaseContext(), ((EditText) view).getId() + " has got focus - " + b, Toast.LENGTH_LONG).show();
- }
- });
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.