EditText Widget

As described in Android website, EditText is a thin veneer over TextView that configures itself to be editable. We use edit text when we want to input some text values.

We have been using EditText in our previous examples. In this section, we will be creating another example just to refresh our familiarity on the EditText component.

In this example, we display the EditText component and let the user type some text values on the field. The typed text will then be displayed back to the user through a Toast message. Below are the steps in creating the sample program.

Step 1:

Create a project named HelloEditText. The project structure will look like this:

Step 2:

In the layout file named activity_main.xml, add the EditText component as follow:

Step 3:

Open the MainActivity.java source code and add event handling. We add a key listener so that when the user presses the Enter key from the keyboard the content of the EditText is displayed back to the user.

The classes used in our program uses the following:

import android.os.Bundle;

import android.app.Activity;

import android.view.KeyEvent;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

Step 4:

Run the program. Try typing any text on the EditText area and press Enter. You will notice that the content of the EditText is displayed as a message.

Leave a Reply

Your email address will not be published. Required fields are marked *