What Is EditText In Android | EditText Tutorial With Example

EditText In Android – Introduction

EditText In Android –

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 In Android – Output

Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Online Surveys – Earn Real Money by Online Surveys
Kids Books – 10+ books for Kids | Age group 2 – 10 | Improve knowledge & Productivity

Leave a Reply

Your email address will not be published.