In this tutorial we will learn about how to create a edittext  android, how to get the text value from the user ,show a toast message of text value when button is clicked and their example .

EditText is an user interface design to insert and modify the text value . EditText is an android support widget which is used to get the text value from the user .

Using EditText we can handle the keyboard or we can specify the type of Input data like number,password,numericPassword,text etc using inputType attribute. We can also specify the maximum number of line , placeholder/hint , background using the attribute maxLines , hint , background respectively.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Add EditText in android xml layout :

Add EditText to your XML Layout file i.e. activity_main.xml to create an edit box to get the value from user for better user experience or just add the following code to activity_main.xml file .

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

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="24dp"
        android:hint="Enter your name here"/>

    <Button
        android:background="@android:color/holo_red_light"
        android:id="@+id/btn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginRight="24dp"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:text="Button" />

</RelativeLayout>

3. Implement getText method of EditText :

Now get the text value from EditText using getText method in MainActivity.java file.

EditText edittext = (EditText) findViewById(R.id.edittext);
//To get the value of EditText
String textValue = edittext.getText().toString();

MainActivity.java :

package net.technxt;

import android.os.Bundle;
import android.app.Activity;

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 edittext;
   Button btn;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      edittext = (EditText) findViewById(R.id.edittext);
      
      btn = (Button) findViewById(R.id.button);
      btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //To get the value of EditText
                String textValue = edittext.getText().toString();
                Toast.makeText(MainActivity.this,"Hello "+textValue,Toast.LENGTH_LONG).show();
            }
        });
   }
}

Now run the project/app in android device or any Emulator to add EditText in android app.

%d bloggers like this: