Native Mobile App Development With Java

Android EditText

15-Sep-2024

Learn how to use the EditText widget for user input in Android studio project with Java

Android EditText examples (plain text, password, password (numeric), email, phone, address, multiline text, time, date, numbers, etc.)


Here is the tutorial for Kotlin


There Are Some Common Attributes for EditText :


id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
drawableLeft
Set Icon in Left
drawableRightSet Icon in Right
drawableTopSet Icon in Top
drawableBotomSet Icon in Botom
textStyle
bold, italic, normal -> Style Change
background
Set background Color




File open res/layout/activity_main.xml file −



<?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"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">


<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Name" />

<!--inputType= textEmailAddress,phone,textMultiLine,number, time, date-->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="12345678"
android:inputType="textPassword" /><!-- textEmailAddress,phone,textMultiLine,number, time, date-->


<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/textViewShowName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Show Name" />

<TextView
android:id="@+id/textViewShowPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Show Password" />


</LinearLayout>



</RelativeLayout>


File open com.microappvalley.newtestproject/MainActivity.java file −



package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editTextName;
EditText editTextPassword;
Button buttonSubmit;
TextView textViewShowName;
TextView textViewShowPassword;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName=(EditText) findViewById(R.id.editTextName);
editTextPassword=(EditText) findViewById(R.id.editTextPassword);
buttonSubmit=(Button) findViewById(R.id.buttonSubmit);
textViewShowName=(TextView) findViewById(R.id.textViewShowName);
textViewShowPassword=(TextView) findViewById(R.id.textViewShowPassword);

buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textViewShowName.setText(editTextName.getText().toString());
textViewShowPassword.setText(editTextPassword.getText().toString());
}
});



}
}


Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:






Comments