Note: editText is similar to JTextField of JAVA Swing Package for building GUI Applications but it is already provided with rich functionality.
editText is a subclass of the textView.
public class EditText extends TextView
Creating editText using XML Code
editText in short is User Interface Element for entering and modify alphanumeric text. When you define an edit text widget, you must specify theR.styleable.TextView_inputType
attribute. For example, for plain text input set inputType to "text"<EditText android:id="@+id/plain_text_input" android:layout_height="wrap_content" android:layout_width="match_parent" android:inputType="text"/>
Programmatically we can retrieve and modify the value of the editText like.
EditText simpleEditText = (EditText) findViewById(R.id.editView1); simpleEditText.setText("I am a plain edtiTextField"); String editTextValue = simpleEditText.getText().toString();
This code also needs to import the following package in main Acitivity.java file
import android.widget.EditText;
Creating and Adding editText Programmatically in Android App:
We can also dynamically add the editText in our application using Java code. Below is the code snippet for dynamically creating the editText in an Android Apps.public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout linearLayout = findViewById(R.id.editTextContainer); // Create EditText final EditText editText = new EditText(this); editText.setHint(R.string.enter_something); editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); editText.setPadding(20, 20, 20, 20); // Add EditText to LinearLayout if (linearLayout != null) { linearLayout.addView(editText); } } }
The code which is yellow highlighted creates the required editText and then adds to LinearLayout to display on screen.
Some Important Attributes of editText
1. id: id is an attribute used to uniquely identify a text EditText. Below is the example code in which we set the id of a edit text.
<EditText android:id="@+id/simpleEditText" android:layout_height="wrap_content" android:layout_width="match_parent"/>
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
Below is the example code with explanation included in which we set the right gravity for text of a EditText.
<EditText android:id="@+id/simpleEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Username"/><!--set text in edit text-->
Similary there are number of other important attributes pertains to editText which may include.
1 | android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors. |
2 | android:drawableBottom This is the drawable to be drawn below the text. |
3 | android:drawableRight This is the drawable to be drawn to the right of the text. |
4 | android:editable If set, specifies that this TextView has an input method. |
5 | android:text This is the Text to display. |
Sr.No | Attribute & Description |
---|---|
1 | android:background This is a drawable to use as the background. |
2 | android:contentDescription This defines text that briefly describes content of the view. |
3 | android:id This supplies an identifier name for this view. |
4 | android:onClick This is the name of the method in this View's context to invoke when the view is clicked. |
5 | android:visibility This controls the initial visibility of the view. |