EditText in Android(Text Field in Android)
To create and use EditText in Android, you must know a few things about EditText.
a. EditText is used to take input from the user. i.e. text or any numeric data.
b. It must have an id and some input type.
– id is used to access the text field in java file by the reference of the resource file, i.e. R.java file.
– input type tells which type of data user can enter in the text field i.e. Text or number.
c. based on input type keyboard or keypad is shown on the screen.
d. findViewById(int) is a built-in method in android which allows the user to access XML layout content by its id.
- Creating a Plain Text field in Android studio is an easy task. You can just Drag and Drop Plain Text From the palette in XML layout of the Activity.
How to place an android text field or Plain Text Edit - Change the id and input type from the XML text panel, or you can double click on the EditText you dragged and dropped to show the id and text change dialog box.
XML Code:12345678910111213141516171819202122232425262728293031323334<?xml version="1.0" encoding="utf-8"?><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="com.example.muneeb.texteditexample.MainActivity"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/UserText"android:layout_alignParentTop="true"android:layout_alignParentRight="true"android:layout_alignParentEnd="true"android:layout_marginTop="179dp"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:hint="Enter Some Text"android:inputType="text" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/click_me"android:id="@+id/getText"android:layout_below="@+id/UserText"android:layout_centerHorizontal="true"android:layout_marginTop="38dp"android:onClick="getUserText"/></RelativeLayout>change id and the input type of TextEdit in android - You can also Display Hint that will be shown as the background water mark in EditText of Android Activity.
1android:hint="Enter Some Text"
- Place a Button and add its onClick, along with text on it. also, implement its button handler.
- Get the text from the text field you created in button onClick method in java by creating an instance of EditText.
1EditText usertext=(EditText) findViewById(R.id.UserText);
- Initialize a variable to store data of EditText, depending upon the input type of EditText. Use the instance of EditText in which you saved the accessed text field through R.java file. get the text .getText() From the instance of EditText and convert it to String or parse it if it is a numeric type variable.
- You must check whether the user entered anything in the text field of left it empty. So for that
1TextUtils.isEmpty(text)1234567if(TextUtils.isEmpty(text)){Toast.makeText(this, "You didn't Entered Anything!" + text, Toast.LENGTH_LONG).show();// if text field is empty}else{Toast.makeText(this, "You Entered: " + text, Toast.LENGTH_LONG).show();}
- And this code is placed in button click method. See Button in Android
1234567891011121314public void getUserText(View v){EditText usertext=(EditText) findViewById(R.id.UserText);String text=usertext.getText().toString();if(TextUtils.isEmpty(text)){Toast.makeText(this, "You didn't Entered Anything!" + text, Toast.LENGTH_LONG).show();// if text field is empty}else{Toast.makeText(this, "You Entered: " + text, Toast.LENGTH_LONG).show();}}
- Now the application will show a message in toast, “You didn’t Entered Anything”. If you leave the TextEdit empty.
button click on the Empty text field - If you enter something in EditText and click the button, application will display the text.
button click on text in EditText