Android EditText Example

Android EditText Tutorial – EditText Example

EditText: In Android, you can use “EditText” class to create an editable textbox to accept user input.This tutorial shows you how to create a textbox in XML file, and demonstrates the use of key listener to display message typed in the textbox.

Step 1: Create a project, Open res -> layout -> activity_main.xml and paste this code below or you simple drag and drop spinner from widget window to app layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
        <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        </EditText>

</RelativeLayout>

Step 2: Open app-> java -> package -> MainActivity.java and Add the below to code in it.

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

 private EditText edittext;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 // get edittext component
    edittext = (EditText) findViewById(R.id.editText);
    String edit = edittext.getText().toString;
    Toast.makeText(MainActivity.this,edit,Toast.LENGTH_SHORT).show();

 }


   
    
}