Android Adventures - Basic Controls And Events
Android Adventures - Basic Controls And Events
Written by Mike James   
Tuesday, 03 March 2015
Article Index
Android Adventures - Basic Controls And Events
Basic Input Controls
Three Buttons
Checkboxes

 

To attach a click event handler on all three buttons you can simply define a function:

public void doButtonClick(View e){
 //process event
}

in the activity and set the onClick property on each button to doButtonClick.

You can find examples of using buttons in this way in earlier chapters. 

Now that we know how to handle events using anonymous classes it is worth doing the same job in this way. 

As we want to assign the same event handler to all three buttons we need to create an instance of OnClickListener. All you have to type is:

View.OnClickListener onClick=new O

and select OnClickListener and let Android Studio generate:

View.OnClickListener myonClickListener=
                    new View.OnClickListener() {
 @Override
 public void onClick(View v) {

 }
}

Notice it doesn't generate a final semicolon so remember to add it. You also need to fill in the details of the onClick event handler - there is only so much Android Studio can do by way of writing your program.

Finally you need to set the instance on each of the buttons:

Button button =
        (Button)findViewById(R.id.button);
Button button2 =
        (Button)findViewById(R.id.button2);
ImageButton imgbutton =
   (ImageButton)findViewById(R.id.imageButton);


button.setOnClickListener(myonClickListener);
button2.setOnClickListener(myonClickListener);
imgbutton.setOnClickListener(
                     myonClickListener);

Now when you click on any of the buttons the myonClickListener is called. 

It is also worth noting that while a button has no other function in life other than to be clicked all of the other controls can also respond to an onClick event. That is you can use anything as a button if you want to.

Expert Properties

If you have looked at any example of using Buttons in the documentation, or if you look at slightly older Android code, you may discover some "tricks" that you can't seem to perform using Android Studio - this isn't actually the case. 

For example, suppose you want to set an icon within a standard text button so that it displays to the left of the text that button contains. This appears to be impossible if you restrict yourself to the properties presented in the property window.

If you lookup the details of the properties that Button inherits you will find 

  • drawableLeft
  • drawableRight
  • drawableStart
  • drawableEnd
  • drawableBottom
    and
  • drawableTop

which are not in the property window list. These allow you to display a drawable, i.e. a graphic, at the location specified relative to the text.

So setting drawableLeft to the icon resource we want to display would do the job but how can we set it?

The solution is that there is an icon to the right of the top of the property window that can be clicked to display "Expert Properties" i.e. all of the properties that the control supports.

 

expertproperties

 

 

If you select the Button and click Expert properties you will see among the new properties listed - drawableLeft. Click on the ellipsis button next to it and select a drawable of your choice to display the icon to the left of the text:

expertpropreties2

In most cases you will only want to work with the properties that are displayed in the property window, but you do need to know how to access the remainder. It is also worth keeping in mind that some of the expert properties will not be displayed correctly by the designer so use with care.

Text Fields

TextFields are the way you get the user to enter some text for you to process and as you can guess they form an important part of most UIs. 

There are a lot of Text Fields provided in the Toolbox but they all work in the same way differing only in the type of input they expect the user to type. In the course of developing apps you will encounter most of them sooner or later but you really only need to know how to use one of them to understand how they all work. 

If you have used another UI Framework then you will have encountered some form of the Text Field before but the Android control is slightly different in that it generally offers the user a customized virtual keyboard - an IME Input Method Editor.

You can build your own IMEs but for the moment lets just use the ones provided by the system. 

If you want the Android simulator to make use of the IME that a real Android device would use then you need to deselect the Hardware Keyboard Present option when you are creating the AVD (Android Virtual device). If the simulator makes use of the host machine's keyboard you don't see the IME at all.

The first thing to clear up is that even though there appear to be a lot of different types of Text Field controls in the Toolbox they are all examples of the EditText control with its inputType property set to a particular value. When you place one on the design surface you will see that it is of type EditText and if you scroll down to its inputType property you can change the type of input the Text Field will handle. 

When you use a particular inputType the user is presented with a virtual keyboard that is suitable for typing a value - e.g. if you specify a numeric value then the keyboard only displays numbers.

You also get an action key at the bottom right of the keyboard that the user can press to complete the action e.g.Send for an SMS message say. 

To select the action button for a particular keyboard, use a Send button for a general text input then you will need to use the Expert Properties view and select a setting for the imeOptions property.

sendset

 

 

For example setting it to actionSend forces a Send button to be displayed:

 

send2

 

There are many other properties that you can use to customise a Text Field but there is one standard task worth explaining in detail -  writing a handler for the onEditorAction event.

 

 



Last Updated ( Sunday, 26 July 2015 )
 
 
Banner

   
Copyright © 2015 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.