Quantcast
Channel: Envato Tuts+ Code - Mobile Development
Viewing all articles
Browse latest Browse all 1836

Android Essentials: Creating Simple User Forms

$
0
0

Android applications often rely upon data supplied by users. This tutorial walks you through the creation and use of a number of the most common controls for collecting data from the user.

For this tutorial, you will design and implement a membership registration form. Whether you’re in charge of recruiting members for an app, an event, or a club, a simple and well-designed member registration form will speed up the registration process, hence encouraging more users to sign up.

App Overview

Layouts in Android define the structure of the user interface (UI) of your application. The UI consists of elements which are built using a hierarchy of views and view groups. A view consists of UI components such as buttons, text boxes, checkboxes, radio buttons, images, etc. See the diagram below representing a standard application layout.

Views and View Groups

Before you begin the design process, you need to give some thought to what kind of data you want to collect from the user. Consider the types of data you want to collect and choose the appropriate types of control. 

Our membership form will be for a gym and will collect the following information:

  • full name
  • gender
  • current weight
  • height
  • goal weight
  • age
  • phone
  • address

Getting Started

Open Android Studio and create a new project with an empty activity. Wait for Android Studio to finish creating your project, and then open app > res > layout > activity_main.xml. This file defines the layout for the user interface (UI). 

A UI in Android is defined in XML files. The easiest way to build a UI in Android Studio is with the Android Studio Layout Editor. The Layout Editor writes the XML for you as you drag and drop views to build your layout.

In this project, we will use the ConstraintLayout. A ConstraintLayout is a layout that provides a flexible way for creating views.

Add a Title to the Layout

In the Palette panel, click Text and add it to the design. You might need to move it around so that it stays at the top of the layout. 

Any time you drag an element in the visual editor of Android Studio, you will see an error immediately: “This view is not constrained…”. This error simply means that the element is not Constrained and therefore won’t render correctly when you run the App. This only applies to Constraint Layouts. 

This TextView control will display the form's description and purpose to the user. This control displays a string resource called @string/member_title, which must be defined within the /res/values/strings.xml string resource file.

The XML for the TextView should look like this:

Notice the id attribute on the text view; it is recommended that each control element is assigned an id so that it's easy to reference from the Java files.

Add a Full Name Field

An EditText element is used to enter and modify text. Add an EditText control for the name just below the TextView. You must also specify the input type attribute. In this case, we will use android:inputType="text" for plain text. Here is the full XML.

Add the Gender Controls

First, add a TextView for the gender label.

Radio buttons allow the user to select one option from a set. They should be grouped together to ensure only one radio button is selected at any given time; this is done by grouping them inside a RadioGroup. Here is the XML for gender selection.

Add the Current Weight and Height Fields

Add two EditTexts from the palette. These will act as fields for the current weight and height. Since we want numbers to be displayed when the user starts entering data, we will use number as the input type. Set the hint attribute to supply a string to display in the EditText control when it’s empty. Here is the XML for both controls on the same line. 

Add Goal Weight and Age Fields

Add another pair of plain text fields for the goal weight and age. The attribute for both input controls will be android:inputType="number". This will ensure that a numeric keyboard is shown when the user starts entering data. Both input controls will be on the same line. These will be on the same line as above. Apply both vertical and horizontal constraints to it.

Add a Phone Number Field

Add another EditText below the goal weight and age. This EditText will be used to fill in the phone number. The attribute for this element will be android:inputType="phone". Setting the EditText to inputType to phone is helpful since it will be easy to add digits. When the user touches this field, they will be presented with the screen below. 

phone input type
Phone input type

The rest of the XML looks like this:

Add an Address Field to the Layout

Add another EditText below the phone field. This EditText will be used for the address of the user. You might also want to increase the space provided for the address field. The attribute for this element will be android:inputType="text".

Add an "Accept Membership Rules" Checkbox

Next, add a CheckBox control below the address field. This checkbox control is a mandatory field that lets the user accept the membership rules and conditions before submitting their details. You can choose to apply a background color to the checkbox to give it an emphasis. 

Add a Submit Button

Lastly, add the submit button. You can change the background color of the button to fit your brand's identity. The most important attribute of the button is the onClick attribute. 

Reading User Input

Now that we are done with the UI, let's implement a user interaction mechanism in our application. Let's start with the name input field. Open MainActivity.java and use the findViewById() method to return an instance of the full name on the onCreate() method.

Responding to Radio Button Click Events

Whenever a user clicks on a radio button, the RadioButton object receives an on-click event. The click event is defined by adding the android:onClick attribute to the radio button in the XML file. The value of the android:onClick attribute must be the name of the method called in response to a click event. The method is implemented in the Activity file. 

Add the android:onClick attribute to each of the radio buttons in the XML file.

Implement the method in the Java file.

Next, use the findByview() method to return an instance of the rest of the EditText elements on the Oncreate() method. Then use the getText() method to get the values from the input fields.

Read Input From a Checkbox

An Android checkbox generally has two states: checked and unchecked. The methods implemented by a checkbox are:

  • public boolean isChecked()
  • public void setChecked(boolean status)

We will use the isChecked() method to check the current state of the checkbox. The state is a boolean value, either true or false. If a checkbox is checked, it returns true; otherwise, it returns false. 

Listening to Button Click Events

Since we already defined an onClick attribute called submitbuttonHandler on the submit button, we will implement a method called submitbuttonHandler() in MainActivity.java.

Here is the final app.

Membership form

Generate the Appropriate Email Details

Now that you’ve got all your form data, you’re ready to craft a message. Simply process all the data fields and build an appropriate feedback message. For example, you might use some fields in the message subject and others in the message body. You can use format strings to help build the appropriate strings, the specifics of which will be discussed in an upcoming quick tip.

Conclusion

In this tutorial, you learned how to use various input controls to design a membership form within an Android application. The EditText control is versatile and powerful, allowing for many different types of text and input forms. Radio buttons allow the user to choose only one of the given choices at a time. The CheckBox controls help limit the user’s input to a specific set of responses. The Button control is a simple way to generate an event to process the form input.

There are many other controls worth exploring for use within forms. There's a lot more we could cover regarding good form design, how form controls fit into the Activity lifecycle, and how input methods and such factor into things, but for now, we've focused on gaining a good handle on the basics of form controls and how to use them.

2021-03-31T14:22:41.000Z2021-03-31T14:22:41.000ZEsther Vaati

Viewing all articles
Browse latest Browse all 1836

Trending Articles