Site icon EdwardsLab

Android: Nuts and Bolts II

In this Nuts and Bolts entry, I’ll be discussing user interaction with User Interface elements, and how to code for it. Examples of most of these can be found in the Notepad Tutorial. There are several pieces of UI functionality that can be coded for, but the simplest and most obvious is the notion of “making the buttons work”. I imagine that, for most people new to Android, the obvious next step after laying out a fancy UI is to actually do something when the user interacts with it.

Check out the read more to find out how to do this and other UI related things!

This is achieved by implemented onEventListeners such as onClickListener, onLongClickListener, etc. These are basically methods which you fill with whatever code performs the functionality that you want the button to have. In the NoteEdit class of the tutorial, there is a confirmButton.setOnClickListener block of code which causes the confirm button to conclude the NoteEdit activity. This is just one example of a button function, the code inside of the OnClickListener would be completely different for a different function.

There are also a number of methods which can be overridden to add even more functionality. For instance, @Override public boolean onCreateOptionsMenu(Menu menu){ } will allow you to code menu buttons for your app that pop up whenever the user presses the Menu key on their phone. There are examples of this in Notepad and also here. If you want to have a professional look to your menu, you can use the Android icons in your menu by looking at one of several examples and using code (as described on one of those sites) like: myMenuItem.setIcon(android.R.drawable.ic_menu_save); I found this to be a particularly exciting piece of code, because when you get your menu working with the official Android icons, the application really starts to look professional.

@Override public void onCreateContextMenu and @Override public boolean onContextItemSelected are another couple of worthwhile methods to write overrides for. These allow you to code functionality for long clicks (besides just adding onLongClickListeners). Most applications in Android make some use of long clicks, and it is another touch which gives some polish to an application. Users begin to expect things to behave certain ways after using the suite of programs that comes preloaded on their phone; and whenever possible we should take advantage of opportunities to code functionality which matches user expectations.

Another little gem that I wish I’d found earlier allows you to tell Android what type of input you expect to receive from your user. In one of my applications I specify that I want numeric input from my user, and Android pops up the onscreen keyboard loaded with numbers instead of the default QWERTY layout. This saves my user the trouble of hitting their numeric input key to switch to that mode, and makes their life slightly easier as a result.

One final UI tweak that took me a while to find (and which unfortunately I don’t still have a link to a good tutorial for) is the InputMethodManager. You declare this like: InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); and then use it in an onClickListener or something to make sure that the onscreen keyboard is not displayed (or is displayed depending on what you tell it to do) with: inputManager.hideSoftInputFromWindow(edittext.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); This is pretty handy when your UI isn’t behaving quite the way you would like for it to; just use these statements to force it to do what you want based on your onClickListeners!

I hope that this whirlwind tour of Android UI interactions has been helpful, and again, don’t stress if you find it a bit much to digest in one sitting! There is a lot of information here, and I accumulated these examples and code snippets over several days worth of work. My goal here is to present the information here, organized by topic, so that future coders can have a place to look first for helpful examples rather than sifting through Google results like I had to.

Exit mobile version