• Have Any Question?
  • contact@eitworld.com

TextField in Android





    • In android, when we want to take the input from the user then we have to use the EditText.
    • In android, textbox is called as edit text.
    • To create the text field in android by using the EditText widget.
    • Basically text fields are used to take the input from the users.
    • The text filed is available in Palette’s Text Field.
    • There are several types of text fields are available in the android which are shown in the diagram:

text-field

    • In this diagram, the types of text fields are shown which can be plain text, person name, password, password (numeric), address, multiline text, time, date, number, number (signed), number (decimal), AutoComplete text, MultiAutoComplete text.
    • The android text field provides the facility to take the email as input.
    • When we want to use the email as input then we have to develop the logic for the email but in android there are both options are available means we can use code to take the valid email as input, and we can also use the email text field.
    • When we use email text field then there is no need to code for valid email entry because email text field can only take the email as input.
    • If we want to take password as input then we have to use password text field.
    • If we want to take numeric input from user then we can use number, number (decimal) means floating point can be used, number (signed) means negative number can be input by user.
    • If we want to take pin code as input then we have to use pin code text.
    • Below is the demo example of the text field and the xml file is as follows:
<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/tvMessage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/hello_world” />

<EditText
android:id=”@+id/etInput”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/tvMessage”
android:layout_below=”@+id/tvMessage”
android:layout_marginLeft=”35dp”
android:layout_marginTop=”93dp”
android:ems=”10″ >

<requestFocus />
</EditText>

</RelativeLayout>

    • In this layout xml file, we have used relative layout and we have used android:id = “@+id/tvMessage” to provide the id for the text view or label which shows the message when we press enter button.
    • android:id = “@+id/etInput” is used to provide the id for text field. When we enter any text and press enter then the default text is changed and the text entered in text field is displayed on the screen.
    • The java file is shown as follows:
package com.example.helloandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
// declaration of EditText and TextView object
EditText input;
TextView message;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// now we have to register the text field and text view using the id
input = (EditText) findViewById(R.id.etInput);
message = (TextView) findViewById(R.id.tvMessage);

input.setOnKeyListener(new OnKeyListener() {

public boolean onKey(View arg0, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

if(event.getAction()== KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_ENTER)
{
message.setText(input.getText().toString());
}

return false;
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

    • In this java file, we have imported the following package:

text-field

    • Android.app.Activity used for activity.
    • Android.view.KeyEvent is used for key event when we press any key.
    • Android.view.View is used for providing the view.
    • Android.view.View.OnKeyListener is used when we click on key then it can identify and tells to system that which key is pressed by the user means it will give the indication to the system.
    • Android.widget.EditText is used for EditText so that we can use the text field in java file and can perform some action on it.
    • Android.widget.TextView is used for TextView so that we can use the text view in java file and can perform some action on it at runtime.
    • Now we have to declare the object of EditText and TextView to use the elements defined in the xml layout file.

text-field

    • Now we have to register these elements by using their id as follows:

text-field

    • Here we have assigned the etInput text field to input variable.
    • Here we have assigned the tvMessage text field to message variable.
    • Now we perform the some action on text field means when we press enter key after entering the text in the text field then that text will be displayed on the screen where the default message is displayed.
    • This message will change every time when we press enter key.
    • To perform this action we have to use following method:

text-field

    • To perform the action, we have to setOnKeyListener() method, for this we have already imported a package related to this method.
    • This method implements the onKey(View args, int keyCode, KeyEvent event) method. It is used to define the action which key is pressed while the widget has focus.
    • In this case the method is defined to listen for enter key and change the default message to entered text.
    • In this message.setText(input.getText().toString()) method is used to set the message when enter key is pressed.
    • Here input.getText() is used to get the text which we enter in the text field.
    • toString() converts that text into the string format.
    • Below is the output:

text-field

    • When launched

text-field

    when text is entered & pressed enter


Real Time Web Analytics