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

EditText widget in Android using Java with Examples

The EditText widget in Android is used for inputting and editing text. Let's discuss its usage with a simple example:

  • Layout Definition (XML):

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.

  • Java Code:

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:

  • We get a reference to the EditText and the Button from our XML layout.
  • We set an 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.

  1. Creating EditText in Android Studio with Java code:

    • Description: In Android, you can create an EditText in your layout XML file or dynamically in Java code.
    • 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);
      
  2. Handling User Input with EditText in Android using Java:

    • Description: Use an EditText to allow users to input text, and handle the input using event listeners.
    • Code:
      // 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;
      });
      
  3. Example code for setting up EditText listeners in Java:

    • Description: Set up event listeners to handle different actions such as text changes or keyboard actions.
    • Code:
      // 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
          }
      });
      
  4. Customizing EditText Appearance in Android with Java:

    • Description: Customize the appearance of an EditText programmatically using Java.
    • Code:
      // 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);
      
  5. Using setText and getText methods with EditText in Java:

    • Description: Set and retrieve text programmatically using setText and getText methods.
    • Code:
      // Java code
      EditText editText = findViewById(R.id.editText);
      
      // Set text
      editText.setText("Hello, World!");
      
      // Get text
      String userInput = editText.getText().toString();
      
  6. InputType attributes in EditText with Java in Android:

    • Description: Set the input type attributes for EditText to control the type of data users can input.
    • Code:
      // 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);
      
  7. Setting up filters for input validation in EditText with Java:

    • Description: Use input filters to perform input validation and restrict certain characters or patterns.
    • Code:
      // Java code
      EditText editText = findViewById(R.id.editText);
      
      // Set input filter for numeric input
      editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
      
  8. Working with hint and hintTextColor in EditText using Java:

    • Description: Set hint text and customize its appearance in an EditText.
    • Code:
      // Java code
      EditText editText = findViewById(R.id.editText);
      
      // Set hint text
      editText.setHint("Enter text here");
      
      // Set hint text color
      editText.setHintTextColor(Color.GRAY);