Android Programming In Kotlin: Basic Controls
Written by Mike James   
Monday, 23 September 2019
Article Index
Android Programming In Kotlin: Basic Controls
Resources
Text Fields

Text Fields

Text Fields 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 referred to as an IME (Input Method Editor). You can build your own IMEs, but for the moment let’s just use the ones provided by the system. 

If you want the Android emulator 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 emulator 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:

input

For example if you select a numeric type you will present the user with a simplified IME that only shows numeric keys:

 numeric

You also get an action key at the bottom right of the keyboard that the user can press to complete the action - Send in the case of an SMS message, for example. To select the action button for a particular keyboard you will need to use the All Attributes view and select a setting for the imeOptions property:

prop

For example setting it to actionSend forces a Send button, the green paper dart button, to be displayed: 

 dart

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

Not included in this extract:

  • The onEditorAction Event
  • CheckBoxes
  • Switches and Toggle buttons
  • Radio Buttons 

 

Summary

  • The basic controls that make up most of the simple Android UI are:

    Buttons
    Text Fields
    CheckBoxes
    Radio Buttons
    Toggle Buttons
    Switches

  • Each control is customized using its attributes and event handlers.

  • Some attributes are hidden from you by Android Studio unless you select the All Attributes button.

 

 

Android Programming In Kotlin
Starting with an App

Covers Android Studio 3 and Constraint Layout.

Is now available as a print book:

coverKotlinsmall

Buy from: Amazon

Contents

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
        Extract: Activity & UI  
  3. Building The UI and a Calculator App
        Extract: A First App
  4. Android Events
  5. Basic Controls
        Extract Basic Controls
        Extract More Controls ***NEW!
  6. Layout Containers
        Extract Layouts - LinearLayout
  7. The ConstraintLayout 
        Extract Bias & Chains
  8. Programming The UI
        Extract Programming the UI
        Extract Layouts and Autonaming Components
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
        Extract Conditional Resources
  12. Beginning Bitmap Graphics
        Extract Animation
  13. Staying Alive! Lifecycle & State
        Extract  State Managment
  14. Spinners
  15. Pickers
  16. ListView And Adapters
  17. Android The Kotlin Way

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962544>

<ASIN:1871962536>



Last Updated ( Monday, 23 September 2019 )