Android EditText Control

0
488

An EditText control can be defined as an overlay over the TextView control. It is configured in such a way that the EditText field can be edited. In fact, it is a subclass of the TextView base class but it possesses rich editing properties.

Also Read: Android TextView Control

List of EditText Attributes
In the below-provided table, we have listed out the major attributes of the EditText control but in case, you want to view the complete list of EditText attributes then check out the official Android document. You will also get to see all the associated methods which will provide you the ability to change the value of any of these attributes at the runtime.

The below-given list of attributes is inherited from the android.widget.TextView class.

S.No. Attribute Description
1 android:autoText * This attribute sets the field to be capable of accepting textual input. It also automatically corrects a few common spelling mistakes.
2 android:drawableBottom * This attribute supports a drawable value. This value appears below the text.
3 android:drawableRight * This attribute accepts a drawable value which appears to the right of the displayed text.
4 android:editable * This attribute sets the EditText to be capable of accepting an input value.
5 android:text * The text passed to this attribute is displayed on the screen.

The below given list of attributes is inherited from the android.view.View class.

S.No. Attribute Description
1 android:background * This attribute accepts the drawable value which is to be used as the background.
2 android:contentDescription * This attribute provides a brief description of the view’s content.
3 android:id * This attribute sets a unique id for the view.
4 android:onClick * If set, this attribute is used to invoke the view whenever the UI control is clicked.
5 android:visibility * This attribute sets the visibility or invisibility of the view.

 

The following example will help you to understand the creation of an Android application with the help of the Linear layout and EditText control.

Step 1: Start with the creation of an Android application on the Android Studio IDE. Save this newly created application as demo. Place it under the com.example.demo package as already explained in the Hello World Example chapter.

Step 2: Modify the main activity file, which is MainActivity.java. You can locate this file under the src/com.example.demo/ path. In this file, you need to add a click event along with the already existing fundamental lifecycle callback methods.

package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity
{
   EditText eText;
   Button btn;

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      eText = (EditText) findViewById(R.id.edittext);
      btn = (Button) findViewById(R.id.button);
      btn.setOnClickListener(new OnClickListener()
      {
         public void onClick(View v)
         {
            String str = eText.getText().toString();
            Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
            msg.show();
         }
      });
   }
}

Step 3: Modify activity_main.xml file. You can easily find this file by tracing the res/layout/ directory. In this file, you are required to include Android UI control.

<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=”.MainActivity” >

   <TextView
      android:id=”@+id/textView1″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:layout_alignParentLeft=”true”
      android:layout_alignParentTop=”true”
      android:layout_marginLeft=”14dp”
      android:layout_marginTop=”18dp”
      android:text=”@string/example_edittext” />

   <Button
      android:id=”@+id/button”
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:layout_alignLeft=”@+id/textView1″
      android:layout_below=”@+id/textView1″
      android:layout_marginTop=”130dp”
      android:text=”@string/show_the_text” />      

   <EditText
      android:id=”@+id/edittext”
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”
      android:layout_alignLeft=”@+id/button”
      android:layout_below=”@+id/textView1″
      android:layout_marginTop=”61dp”
      android:ems=”10″
      android:text=”@string/enter_text” android:inputType=”text” />
</RelativeLayout>

Step 4: In strings.xml, you need to define the new string constants to be used. You can find this file under the res/values/ path. 

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>demo</string>
   <string name=”example_edittext”>Example showing EditText</string>
   <string name=”show_the_text”>Show the Text</string>
   <string name=”enter_text”>text changes</string>
</resources>

Step 5: Make sure that the AndroidManifest.xml file remains as it is. The default content of this file is given below.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.demo” >      
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” > 

      <activity
         android:name=”com.example.demo.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>
      </activity>
   </application>
</manifest> 

Step 6: The final step is to run your demo application. For this, we are assuming that you have already created AVD during the environment setup. Open any of your project’s activity files. Now, locate the Run button from the toolbar. Click on it and the Studio will initiate the installation of your demo application on the AVD. If the setup and the application fall in right place then the Emulator window will display the final output of your Android application.