Android EditText Control With Example

In Android, EditText Control is a input field like HTML input for  text to a user because it is a user interface element. EditText is an overlay over TextView that allow him to be editable. EditText is a complete text editor. EditText is a subclass of TextView. So, In this Android tutorial, we are going to discuss each and every aspect of Android EditText.

EditText has a parent class called View. Being a subclass of View, EditText control can be used in our Android Studio App GUI ( Graphical User Interface ) inside a ViewGroup, or as the content view of an activity.

Android EditText Control

Android EditText Control

EditText Code IN XML:

The following code shows a use of EditText with an XML layout and code to modify the content of the text view:

   // EditText in XML 
 
<EditText 
        android:id="@+id/edittext_id"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="codekila" />

In above example, EditText has an id attribute whose value is edittext_id and layout_height and layout_width having the same value wrap_content which means it only covers the space equal to its text attribute text.

EditText Code IN JAVA:

The following code shows a use of EditText with a Java and code to modify the content of the EditText programmatically:

 // EditText in JAVA

EditText edittext= (EditText ) findViewById(R.id.edittext_id);
edittext.setText("codekila.com");

Now Let’s discuss Android EditText attributes one by one.

Android EditText Attributes

1. android:id – This is the id attribute which uniquely identifies the EditText control. It is used for get and set the properties of EditText Control in JAVA class.

Below is the example code in which we set the id attribute of a EditText to a edittext_id.

<!-- EditText Example --> 
<EditText 
android:id="@+id/edittext_id"   <!-- EditText id attribute -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"Codekila Android Tutorials"/>

2. android:layout_width – This is the layout_width attribute which defines the width of the EditText . Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_width attribute of a EditText to a wrap_content.

<!-- EditText Example --> 
<EditText 
android:id="@+id/edittext_id"   
android:layout_width="wrap_content"      <!-- EditText layout_width attribute -->
android:layout_height="wrap_content"
android:text:"Codekila Android Tutorials"/>

3. android:layout_height – This is the layout_height attribute which defines the height of the EditText . Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_height attribute of a EditText to a wrap_content.

<!-- EditText Example -->                      
<EditText 
android:id="@+id/edittext_id"   
android:layout_width="wrap_content"      
android:layout_height="wrap_content"         <!-- EditText layout_height attribute -->
android:text:"Codekila Android Tutorials"/>

4. android:text – This is the text attribute which is used to display text to a  user. We can set text in XML as well as in a JAVA class.

Below is the example code in XML where we set the text “Codekila Android Tutorials” in a EditText Control in XML.

<!-- EditText Example -->                      
<EditText 
android:id="@+id/edittext_id"   
android:layout_width="wrap_content"      
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"         
android:text:"Codekila.com Android Tutorials"/>        <!-- EditText text attribute -->

 

IN JAVA –

Let’s take an example in which we set the text of a EditText programmatically means in JAVA class.

// EditText in JAVA

EditText edittext= (EditText ) findViewById(R.id.edittext_id);
edittext.setText("codekila.com");

5. android:textSize – This is the textSize attribute which defines the size of the text and can be set in XML. We can set the textSize in sp (scale pixel) or either it can be in dp (density pixel). Recommended dimension type for text is “sp” for scaled-pixels (example: 12sp).

Let’s take an example in which we set the text size of a EditText in XML.

<EditText 
    android:id="@+id/edittext_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="codekila.com"
    android:textSize="12sp" />     <!-- EditText textSize-->

IN JAVA –

Let’s take an example in which we set the text size of a EditText programmatically means in JAVA class.

EditText edittext= (EditText )findViewById(R.id.edittext_id);
edittext.setTextSize(15);   //set 15sp size of text

6. android:textColor –  This is the textColor attribute which defines the color of the text and can be set in XML. Color of EditText value is in “#rgb”, “#ffff”

Let’s take an example in which we set the color of a EditText in XML.

<EditText 
    android:id="@+id/edittext_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="codekila.com"
    android:textSize="20sp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#006400"/><!--darkgreen color for text view--> />

 

IN JAVA –

Let’s take an example in which we set the textColor of a EditText programmatically means in JAVA class.

EditText edittext= (EditText )findViewById(R.id.edittext_id);
edittext.setTextColor(Color.GREEN); //set red color for text view

 

Android EditText Example

Now, we take a full example of Android EditText Control in an android studio. Below is the screenshot of the android EditText example which we are going to show you now step by step. In this example, we will create a EditText and Button when we click on Button We display a Toast (Message) to show that what is in EditText.

 

EditText Before Click Example

Step 1: Open Android Studio and Create a new project and name it androidEditText .

Step 2: Open res >  layout > xml (or) activity_main.xml and add below mention code. Here we will create a EditText in Relative Layout.

<?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:id="@+id/activity_main"
    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.codekila.androidedittext">

   <TextView
        android:id="@+id/textview_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20dp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_centerHorizontal="true"
        android:text="Android EditText Control Example"/>

    
    <EditText
        android:id="@+id/edittext_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/textview_id"
        android:layout_centerHorizontal="true"
        android:hint="Enter Name"/>

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/edittext_id"
        android:text="Submit"
        />


</RelativeLayout>

Step 3: Open app  > java  > package ( in my case com.codekila.androidedittext ) and open MainActivity.java and add the below mentioned code. Here we will change the text of EditText after the user clicks on EditText .

package example.codekila.edittextwexample;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText ;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // set the layout
       final EditText codeedittext =(EditText)findViewById(R.id.edittext_id);

        Button codebutton=(Button)findViewById(R.id.button_id);

        codebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, codeedittext.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }


}

Step 4: Below is the default content of AndroidManifest.xml  which is created at the time of creating a new android studio project. −

 // AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codekila.edittextexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Result:

Now run the app in Emulator and click on the EditText . You will see text will change to “Codekila.com After Click”.

EditText After Click

 

Conclusion:

Here we can see that how android EditText control used to input text on the EditText which is  user interface side.If you have any query related to  Android EditText Control With Example post feel free to comment on the comment box. Do not forget to share this post because someone else also needs this. And don’t forget to like our Facebook Page.

Also Read:

  1. Android Tutorial For Beginners
  2. Android TextView Control Full Tutorial With Example

Leave a Reply

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