Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The EditText
widget in Android is used for inputting and editing text. Let's discuss its usage with a simple example:
In your layout, say activity_main.xml
, define the EditText
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name"/> <Button android:id="@+id/buttonSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:layout_marginTop="16dp"/> </LinearLayout>
In this XML, we have defined an EditText
with a hint and a Button
below it.
In your Java activity, say MainActivity.java
, you can retrieve and manipulate the EditText
:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText editText; private Button buttonSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get references to the views editText = findViewById(R.id.editText); buttonSubmit = findViewById(R.id.buttonSubmit); // Set an OnClickListener for the button buttonSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editText.getText().toString(); Toast.makeText(MainActivity.this, "Hello, " + name + "!", Toast.LENGTH_SHORT).show(); } }); } }
In this Java code:
EditText
and the Button
from our XML layout.OnClickListener
on the button to retrieve the text from the EditText
and display it in a Toast
message when the button is clicked.This is a basic example, and there are numerous properties and methods available in the EditText
class to customize its behavior and appearance, such as input types, filters, and listeners for text changes.
Creating EditText in Android Studio with Java code:
EditText
in your layout XML file or dynamically in Java code.// XML layout <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter text" /> // Java code (inside an Activity or Fragment) EditText editText = findViewById(R.id.editText);
Handling User Input with EditText in Android using Java:
EditText
to allow users to input text, and handle the input using event listeners.// Java code EditText editText = findViewById(R.id.editText); editText.setOnEditorActionListener((v, actionId, event) -> { if (actionId == EditorInfo.IME_ACTION_DONE) { String userInput = editText.getText().toString(); // Handle user input return true; } return false; });
Example code for setting up EditText listeners in Java:
// Java code EditText editText = findViewById(R.id.editText); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) { // Before text changes } @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { // On text changes } @Override public void afterTextChanged(Editable editable) { // After text changes } });
Customizing EditText Appearance in Android with Java:
EditText
programmatically using Java.// Java code EditText editText = findViewById(R.id.editText); // Set background color editText.setBackgroundColor(Color.LIGHT_GRAY); // Set text color editText.setTextColor(Color.BLUE); // Set text size editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Using setText
and getText
methods with EditText in Java:
setText
and getText
methods.// Java code EditText editText = findViewById(R.id.editText); // Set text editText.setText("Hello, World!"); // Get text String userInput = editText.getText().toString();
InputType attributes in EditText with Java in Android:
EditText
to control the type of data users can input.// Java code EditText editText = findViewById(R.id.editText); // Set input type to number editText.setInputType(InputType.TYPE_CLASS_NUMBER); // Set input type to email editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
Setting up filters for input validation in EditText with Java:
// Java code EditText editText = findViewById(R.id.editText); // Set input filter for numeric input editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
Working with hint
and hintTextColor
in EditText using Java:
EditText
.// Java code EditText editText = findViewById(R.id.editText); // Set hint text editText.setHint("Enter text here"); // Set hint text color editText.setHintTextColor(Color.GRAY);