EditText is a fundamental UI element in Android that allows users to input text. This guide will help you read data from EditText using Java in Android Studio, perfect for beginner Android developers.
Step-by-Step Guide
1. Add EditText and Button in XML Layout
Create or open your XML layout file (e.g., activity_main.xml
) and add the following UI elements:

<EditText android:id="@+id/editTextName" 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" />
2. Access EditText and Button in Java Code
In your activity class (e.g., MainActivity.java
), declare and initialize the views:

import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { EditText editTextName; Button buttonSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the views editTextName = findViewById(R.id.editTextName); buttonSubmit = findViewById(R.id.buttonSubmit); // Set a click listener buttonSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Read text from EditText String name = editTextName.getText().toString(); // Display using Toast Toast.makeText(MainActivity.this, "Hello, " + name, Toast.LENGTH_SHORT).show(); } }); } }
3. Key Methods Explained
findViewById()
: Links the XML view to Java.getText().toString()
: Retrieves the text input from EditText.Toast.makeText()
: Displays a small message popup (optional).
Summary
Reading data from an EditText in Java using Android Studio is simple and essential. You:
- Declare EditText and Button
- Use
getText().toString()
to retrieve the text - Use it for displaying, processing, or saving
Now you’re ready to handle user input like a pro!