Download Code

Android EditText Example

In android edittext example you will learn how to use Edit Text in android application.Also some basic information about Text Fields and Input Controls.

Input Controls

Input Controls are the interactive app UI components.Android provide some controls like

  • Button
  • Text Fields
  • Check Box
  • Radio
  • Spinner
  • Seek bar
  • Pickers

Text Fields

Text Field allows the user to type text in android application.Text Field can be single line or multiple line.For example Edit Text.There are some different types of Edit Text.

  • Plain Text
  • Person Name
  • Password
  • Email
  • Postal Address
  • Phone
  • Time
  • Date

Now let’s talk about Edit Text.

Edit Text

Basically used to take user input for any particular purpose.Subclass of Text View which turned itself into Edit Text.

So you will code for edit text and display in Toast on button click what user will input in that edit text.

Let’s start go ahead and create android edittext example application project.

EditText,android edittext,android edittext example,edittext example,

Now open your activity_main.xml in Graphical Layout.Then drag and drop plain edit text and then button.Then your XML and Graphical Layout will look like this.

EditText,android edittext,android edittext example,edittext example,

EditText,android edittext,android edittext example,edittext example,

Now go to MainActivity.java class by default your activity will look like this.

package androidcodec.edittext;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Now create variables for edit text and button.Then create a method where you will refer the id’s of edit text and button.Then call your method in onCreate method.Your MainActivity will look like this.

public class MainActivity extends Activity {
	EditText userInput;   // user input variable
	Button displayInput;  // button variable 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
    }

    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);
    	displayInput = (Button) findViewById(R.id.button1);
    }  
}

Now next apply click event on button and use getText() method to get user input from edit text and display in a Toast.

displayInput.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), userInput.getText(), Toast.LENGTH_SHORT).show();
			}
		});

Complete Code MainActivity.java

package androidcodec.edittext;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
	EditText userInput;   // user input variable
	Button displayInput;  // button variable 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();  // calling user defined method
        // apply click event on button
        displayInput.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// displaying toast and also getting user input using getText() method
				Toast.makeText(getApplicationContext(), userInput.getText(), Toast.LENGTH_SHORT).show();
			}
		});
    }
    // user defined method
    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);   // refering id of edit text
    	displayInput = (Button) findViewById(R.id.button1);    // refering id of button 
    }
   
}

Final Output should be like this image.

EditText,android edittext,android edittext example,edittext example,

That’s it.I hope this tutorial might help you alot.Don’t for get to leave your feedback.Thanks for Visiting.