EditText Tutorial With Example In Android Studio

EditText Tutorial – Introduction

The EditText is used to take input from the users where users can input a name, mobile number, email address, home address, etc. EditText is also useful when you want the user to be able to edit the text.EditText is the predefined subclass of TextView. You can use the hint attribute to guide the user.

Create activity_edittext_layout file in XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
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=”wrap_content”
android:orientation=”vertical”
android:layout_gravity=”center”
tools:context=”.editTextLayout”>

<EditText
android:id=”@+id/fName”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”Enter First Name”
android:inputType=”text”/>

<EditText
android:id=”@+id/lName”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”15dp”
android:hint=”Enter Last Name”
android:inputType=”text”/>

<EditText
android:id=”@+id/Email”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”15dp”
android:hint=”Enter Your Email Address”
android:inputType=”textEmailAddress” />

<EditText
android:id=”@+id/mNumber”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”15dp”
android:hint=”Enter Your Mobile Number”
android:inputType=”phone” />

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:layout_marginTop=”50dp”>

<Button
android:id=”@+id/submit”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”Submit”
android:textSize=”20sp”/>

<Button
android:id=”@+id/reset”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”Reset”
android:textSize=”20sp”/>
</LinearLayout>

</LinearLayout>

Create editTextLayout file in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class editTextLayout extends AppCompatActivity {

EditText fName, lName, Email, mNumber; // Edit Text Objects
Button submit, reset; // Button Objects

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text_layout);

// We need to find our elements by IDs

fName = findViewById(R.id.fName);
lName = findViewById(R.id.lName);
Email = findViewById(R.id.Email);
mNumber = findViewById(R.id.mNumber);

submit = findViewById(R.id.submit);
reset = findViewById(R.id.reset);

// Button Code.
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fname = fName.getText().toString().trim(); //We collect the user input in a string
String lname = lName.getText().toString().trim();
String mnumber = mNumber.getText().toString().trim();
String email = Email.getText().toString().trim();

Toast.makeText(editTextLayout.this, “First Name: ” +fname+ “\n\n Last Name: “+lname+ “\n\n Mobile Number” +mnumber+
“\n\n Email Address” +email , Toast.LENGTH_LONG).show();
}
});

reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fName.getText().clear(); // clear method is used to clear the user input
lName.getText().clear();
Email.getText().clear();
mNumber.getText().clear();
}
});
}
}

Android Manifest file

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.editTextLayout” />
</application>

</manifest>

EditText Tutorial – Output

Check Out the Headphones – 50% to 70% Off
Check Out the Earbuds – 50% to 70% Off
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Check Out The LAB Configuration eBook – Class A, Class B, Class C, MiN worth $78 Get at $18

Leave a Reply

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