Android requestFocus and requestFocusFromTouch

Nov 22 2012

The android application on my current project has a layout that includes tabs at the top and each tab in turn will display a fragment when clicked. With this setup we started seeing problems entering data into EditText fields. The problem presented in the emulator and some devices and consisted on not being able to focus on these fields. In order to solve the problem we added a OnTouchListener in which we requested focus,

The android application on my current project has a layout that includes tabs at top and each tab in turn will display a fragment when clicked. With this setup we started seeing problems entering data into EditText fields. The problem presented in the emulator and some devices and it consisted on not being able to get focus on these fields. In order to solve the problem we added a OnTouchListener in which we requested focus,
<Code>
This worked well for all our tests until we had to add a new piece of functionality that required an AutoCompleteText field. In this case the OnTouchListener display some weird behavior in some of our testing devices. While trying to figure out the reason for this strange behavior, the following information prove to be very useful:
- Android Touch Mode: http://android-developers.blogspot.com/2008/12/touch-mode.html
- View requestFocus: http://developer.android.com/reference/android/view/View.html#requestFocus()
- View requestFocusFromTouch: http://developer.android.com/reference/android/view/View.html#requestFocusFromTouch() 
With this and my testing I found that requstFocus works in my setup for every situation but one: when using physical keyboards. When the physical keyboard of the device is exposed requestFocusFromTouch obtains the focus for the field touched. Which lead me to modify my listener as follows:
<Code>
The listener for my AutoCompleteText field now detects if the physical keyboard is exposed and reacts accordingly. we added a OnTouchListener in which we requested focus,
editText1.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				v.requestFocusFromTouch();
				return false;
			}
		});

This worked well for all our tests until we had to add a new piece of functionality that required an AutoCompleteText field. In this case the OnTouchListener displayed some odd behavior in some of our testing devices. While trying to figure out the reason for this, the following information proved to be very useful:

This along with some testing helped me determine that requestFocus works in my setup for every situation but one: when using physical keyboards. In the case where the physical keyboard of the device was exposed requestFocusFromTouch was able to obtain the foucs. Which lead me to modify my listener as follows:

autoCompleteText1.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Configuration config = getResources().getConfiguration();
				if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
					v.requestFocusFromTouch();
				} else {
					v.requestFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
				}
				return false;
			}
		});

The listener for my AutoCompleteText field now detects if the physical keyboard is exposed and uses the correct requestFocus method. I also had to add some code to display the soft keyboard when using this approach since the behavior was not consistent between devices. 

About the Author

Jairo Vazquez is a Senior Manager at Captech with over 12 years of experience in JEE application development and he is an experienced Android developer.

 

Disclaimer

The words and opinions expressed here are those of each article's respective author, and do not necessarily represent the views of CapTech Ventures.