Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Android Textbox Example

In your Android application, you may want to accept user input. There are many ways you can do that and one of the most basics is the textbox.

In Android you can use the EditText class in order to create a textbox on your screen in which the user will write text. Then, we have to bundle this textbox with a specific key event, so when the user presses the key the application can get the text written in the textbox as an input and perform a specific action.
 
 
 
 
 
For this tutorial, we will use the following tools in a Windows 64-bit platform:

  1. JDK 1.7
  2. Eclipse 4.2 Juno
  3. Android SKD 4.2

1. Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project. You have to specify the Application Name, the Project Name and the Package name in the appropriate text fields and then click Next.

In the next window make sure the “Create activity” option is selected in order to create a new activity for your project, and click Next. This is optional as you can create a new activity after creating the project, but you can do it all in one step.

Select “BlankActivity” and click Next.

You will be asked to specify some information about the new activity. In the Layout Name text field you have to specify the name of the file that will contain the layout description of your app. In our case the file res/layout/main.xml will be created.Then, click Finish.

1. Create an EditText component

Navigate to res/layout/main.xml file.

Open the main.xml file and paste the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/mytextText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>

</LinearLayout>

2. Set an OnKeyListener for the textbox

Go to the java file of the activity and paste the following code:

package com.javacodegeeks.android.textboxexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

 private EditText edittext;

 @Override
 public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	addKeyListener();
 }

 public void addKeyListener() {

	// get edittext component
	edittext = (EditText) findViewById(R.id.myText);

	// add a keylistener to monitor the keaybord avitvity...
	edittext.setOnKeyListener(new OnKeyListener() {
	public boolean onKey(View v, int keyCode, KeyEvent event) {

		// if the users pressed a button and that button was "0"
		if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_0)) {

			// display the input text....
			Toast.makeText(MainActivity.this,edittext.getText(), Toast.LENGTH_LONG).show();
			return true;

	    // if the users pressed a button and that button was "9"
		} else if ((event.getAction() == KeyEvent.ACTION_DOWN) 	&& (keyCode == KeyEvent.KEYCODE_9)) {

			// display message
			Toast.makeText(MainActivity.this, "Number 9 is pressed!", Toast.LENGTH_LONG).show();
			return true;
		}

		return false;
	}
 });
}
}

In the above code, we get the EditText component we created in the main.xml file, and we are bundling it with a KeyListener. So, when the user presses any key the onKey function is executed. In that function usually we put a bunch of if-else if statements that will be taken depending on the key that was pressed.

3. Run the application

This is the main screen of our application. You can type some text in the textbox and then press “0″ :

Now, if you press “9″ :

Download Eclipse Project

This was an Android TextBox Example. Download the Eclipse Project of this tutorial: AndroidTextBoxExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close