Buttons, TextView, EditText

Buttons, TextView, and EditText are among the most commonly used user interface (UI) components in Android app development. These components allow developers to display information, receive user input, and perform actions within an application. Understanding how to use these elements is essential for creating interactive and user-friendly Android applications.

Almost every Android application uses TextView, EditText, and Button components for communication between the user and the application.

What are Android UI Components?

UI (User Interface) components are visual elements that appear on an Android application’s screen.

They help users interact with the application.

Common UI components include:

  • TextView
  • EditText
  • Button
  • ImageView
  • CheckBox
  • RadioButton
  • RecyclerView

Among these, TextView, EditText, and Button are the most fundamental components.

What is TextView?

TextView is a UI component used to display text on the screen.

It allows developers to show information, labels, instructions, headings, and messages to users.

Basic TextView Example

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to Android Development" />

Output

Welcome to Android Development

The text is displayed on the application screen.

Common TextView Attributes

text

Defines the displayed text.

android:text="Hello Android"

textSize

Defines the text size.

android:textSize="20sp"

textColor

Defines the text color.

android:textColor="#000000"

textStyle

Defines text appearance.

android:textStyle="bold"

Possible values include:

  • normal
  • bold
  • italic

Example of Styled TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Student Registration"
    android:textSize="24sp"
    android:textStyle="bold"
    android:textColor="#0000FF" />

This creates a large blue heading.

Uses of TextView

TextView is commonly used for:

  • Titles
  • Headings
  • Labels
  • Instructions
  • Notifications
  • Status messages

Almost every Android screen contains at least one TextView.

What is EditText?

EditText is a UI component that allows users to enter and edit text.

It is commonly used in forms, login screens, registration pages, and search bars.

Basic EditText Example

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Your Name" />

Output

Enter Your Name

The hint disappears when the user starts typing.

Common EditText Attributes

hint

Displays placeholder text.

android:hint="Enter Email"

inputType

Defines the type of input.

Example:

android:inputType="text"

Other common values:

text
number
phone
textPassword
textEmailAddress

maxLength

Limits input length.

android:maxLength="50"

textColor

Sets text color.

android:textColor="#000000"

Example of Email Input Field

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Email"
    android:inputType="textEmailAddress" />

This field is optimized for email input.

Example of Password Field

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Password"
    android:inputType="textPassword" />

Characters entered by the user are hidden.

Accessing EditText in Java

XML:

<EditText
    android:id="@+id/edtName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Java:

EditText edtName =
        findViewById(R.id.edtName);

String name =
        edtName.getText().toString();

This retrieves user input from the EditText component.

Uses of EditText

EditText is commonly used for:

  • Login forms
  • Registration forms
  • Search fields
  • User feedback
  • Contact forms
  • Data entry screens

It is the primary component for user input.

What is Button?

A Button is a clickable UI component used to perform actions when pressed by the user.

Buttons trigger events and execute application logic.

Basic Button Example

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit" />

Output

[ Submit ]

The button appears on the screen and can be clicked.

Common Button Attributes

text

Defines button text.

android:text="Login"

id

Assigns a unique identifier.

android:id="@+id/btnLogin"

background

Defines button background.

android:background="#2196F3"

textColor

Defines text color.

android:textColor="#FFFFFF"

Example of Styled Button

<Button
    android:id="@+id/btnRegister"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Register"
    android:textColor="#FFFFFF" />

This creates a customized button.

Handling Button Clicks in Java

XML:

<Button
    android:id="@+id/btnClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me" />

Java:

Button btnClick =
        findViewById(R.id.btnClick);

btnClick.setOnClickListener(v -> {

    System.out.println("Button Clicked");

});

The code executes when the button is pressed.

Complete Example

XML Layout

<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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student Registration"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/edtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Name" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

Java Code

EditText edtName =
        findViewById(R.id.edtName);

Button btnSubmit =
        findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(v -> {

    String name =
            edtName.getText().toString();

    System.out.println(name);

});

This example creates a simple form where users enter their name and submit it.

Real-World Applications

TextView, EditText, and Button are used in:

  • Login systems
  • Registration forms
  • Banking applications
  • E-commerce apps
  • Social media platforms
  • Educational apps
  • Search systems
  • Contact forms

These components form the foundation of most Android user interfaces.

Common Beginner Mistakes

Missing Component IDs

Without IDs, components cannot be accessed in Java code.

Incorrect:

<Button
    android:text="Submit" />

Correct:

<Button
    android:id="@+id/btnSubmit"
    android:text="Submit" />

Forgetting getText()

Incorrect:

String name = edtName;

Correct:

String name =
        edtName.getText().toString();

Using Wrong Input Types

Always select the correct input type for better user experience.

Not Handling Empty Input

Validate user input before processing data.

Best Practices

When using TextView, EditText, and Button:

  • Use meaningful IDs
  • Validate user input
  • Use proper input types
  • Keep button labels clear
  • Use readable text sizes
  • Maintain consistent spacing

These practices improve usability and application quality.

Importance of TextView, EditText, and Button

These components are important because they:

  • Display information
  • Accept user input
  • Trigger application actions
  • Improve user interaction
  • Support form creation
  • Build interactive interfaces

They are essential building blocks of Android applications.

Conclusion

TextView, EditText, and Button are fundamental Android UI components that enable communication between users and applications. TextView displays information, EditText collects user input, and Button performs actions based on user interaction. Mastering these components is essential for creating functional, interactive, and professional Android applications, making them a core part of every Android developer’s skill set.

Home » Java for Android Apps > Android UI Design > Buttons, TextView, EditText