Finally, you added the LinearLayout object to the activity:
this.addContentView(layout,layoutParam);
As you can see, using code to create the UI is quite a laborious affair. Hence, dynamically generate your UI using code only when necessary.
android:layout_height=”wrap_content”
android:text=”Your Name”
/>
<EditText
android:id=”@+id/txt1”
android:layout_width=”214dp”
android:layout_height=”wrap_content”
/>
<Button
android:id=”@+id/btn1”
android:layout_width=”106dp”
android:layout_height=”wrap_content”
android:text=”OK”
/>
<Button
android:id=”@+id/btn2”
android:layout_width=”106dp”
android:layout_height=”wrap_content”
android:text=”Cancel”
/>
</LinearLayout>
3 .
Add the following statements in bold to the MainActivity.java file:packagenet.learn2develop.UIActivity;
importandroid.app.Activity;
importandroid.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
publicclassMainActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
Toast.makeText(getBaseContext(), “Center was clicked”, Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Toast.makeText(getBaseContext(), “Left arrow was clicked”,
Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Toast.makeText(getBaseContext(), “Right arrow was clicked”, Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_UP:
Toast.makeText(getBaseContext(), “Up arrow was clicked”, Toast.LENGTH_LONG).show();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Toast.makeText(getBaseContext(), “Down arrow was clicked”, Toast.LENGTH_LONG).show();
break;
}
return false;
} }
4 .
Press F11 to debug the application on the Android Emulator.5 .
When the activity is loaded, type some text into it, as shown on the left of Figure 3-22. Next, click the down arrow key on the directional pad. Observe the message shown on the screen, as shown in the black area on the right of Figure 3-22.Figure 3-22
How It Works
When the activity is loaded, the cursor will be blinking in the EditText view, as it has the focus.
In the MainActivitiy class, you override the onKeyDown() method of the base Activity class, like this:
@Override
publicbooleanonKeyDown(intkeyCode,KeyEventevent)
{
switch(keyCode)
{
caseKeyEvent.KEYCODE_DPAD_CENTER:
//...
break;
caseKeyEvent.KEYCODE_DPAD_LEFT:
//...
break;
caseKeyEvent.KEYCODE_DPAD_RIGHT:
//...
break;
caseKeyEvent.KEYCODE_DPAD_UP:
//...
break;
caseKeyEvent.KEYCODE_DPAD_DOWN:
//...
break;
}
returnfalse;
}
In Android, whenever you press any keys on your device, the view that currently has the focus will try to handle the event generated. In this case, when the EditText has the focus and you press a key, the EditText view will handle the event and display the character you have just pressed in the view.
However, if you press the up or down directional arrow key, the EditText view does not handle this, and instead passes the event to the activity. In this case, the onKeyDown() method is called. In this case, you checked the key that was pressed and displayed a message indicating the key pressed. Observe that the focus is now also transferred to the next view, which is the OK button.
Interestingly, if the EditText view already has some text in it and the cursor is at the end of the text (see Figure 3-23), then clicking the left arrow key does not fire the onKeyDown() event; it simply moves the cursor one character to the left. This is because the EditText view has already handled the event.
If you press the right arrow key instead, then the onKeyDown() method will be called (because now the EditText view will not be handling the event).
The same applies when the cursor is at the beginning of the EditText view.
Clicking the left arrow will fire the onKeyDown() event, whereas clicking the right arrow will simply move the cursor one character to the right.
With the OK button in focus, press the center button in the directional pad. Observe that the message
“Center was clicked” is not displayed. This is because the Button view itself is handling the click event.
Hence the event is not caught by the onKeyDown() method. However, if none of the views is in focus at the moment (you can achieve this by clicking on the background of the screen), then pressing the center key will show the “Center was clicked” message (see Figure 3-24).
Figure 3-23
Figure 3-24
Note that the onKeyDown() method returns a boolean result. You should return true when you want to tell the system that you are done with the event and that the system should not proceed further with it.
For example, consider the case when you return true after each key has been matched:
@Override
publicbooleanonKeyDown(intkeyCode,KeyEventevent)
{
switch(keyCode)
{
caseKeyEvent.KEYCODE_DPAD_CENTER:
Toast.makeText(getBaseContext(),
“Centerwasclicked”,
Toast.LENGTH_LONG).show();
return true;
caseKeyEvent.KEYCODE_DPAD_LEFT:
Toast.makeText(getBaseContext(),
“Leftarrowwasclicked”,
Toast.LENGTH_LONG).show();
return true;
caseKeyEvent.KEYCODE_DPAD_RIGHT:
Toast.makeText(getBaseContext(),
“Rightarrowwasclicked”,
Toast.LENGTH_LONG).show();
return true;
caseKeyEvent.KEYCODE_DPAD_UP:
Toast.makeText(getBaseContext(),
“Uparrowwasclicked”,
Toast.LENGTH_LONG).show();
return true;
caseKeyEvent.KEYCODE_DPAD_DOWN:
Toast.makeText(getBaseContext(),
“Downarrowwasclicked”,
Toast.LENGTH_LONG).show();
return true;
}
returnfalse;
}
If you test this, you will see that now you cannot navigate between the views using the arrow keys.